-2

I have three classes. Class A is titled Circle. Class B is titled ColorfulCircle. Class C is titled ColorfulBouncingCircle. I need to inherit the methods public double getCenterY(), public double getCenterX, and public void setCenterCoordinates(double centerX, double centerY) from Class “A” Circle so that I can use them in Class “C” ColorfulBouncingCircle.

Circle looks like this:

class Circle {

    private double centerX, centerY;

    public double getCenterX() {
        return centerX;
    }

    public double getCenterY() {
        return centerY;
    }

    public void setCenterCoordinates(double centerX, double centerY) {
        this.centerX = centerX;
        this.centerY = centerY;
    }
}

There are the exact methods I need to be able to use in ColorfulBouncingCircle so that I can use the centerX and centerY variables in code over there.

I just need to know exactly what do I type in to the ColorfulBouncingCircle class so that I can use them.

Thank you.

Shekhar Rai
  • 2,008
  • 2
  • 22
  • 25
  • You can just use `extends` keyword to inherit your parent class. `class ColorfulBouncingCircle extends Circle { }` that's it! `ColorfulBouncingCircle cbCircle = new ColorfulBouncingCircle(); cbCircle.setCenterCoordinates(123, 321);` – Shekhar Rai Dec 07 '19 at 07:08
  • Have a look at the detail explanation on this link https://stackoverflow.com/questions/10839131/implements-vs-extends-when-to-use-whats-the-difference – Sunil Dec 07 '19 at 07:41
  • that is basic concept of inheritance. go through about inheritance in java. I recommend https://www.geeksforgeeks.org/inheritance-in-java/ – Muhammad Azam Dec 07 '19 at 08:06

1 Answers1

0

Extend the Circle class from ColorfulBouncingCircle class.

public class ColorfulBouncingCircle extends Circle {

}

You can then call getCenterX(), getCenterY() and setCenterCoordinates() from the object of ColorfulBouncingCircle class.

Amith Raj
  • 36
  • 4