0

Ok, so my basic class hierarchy is as follows:

abstract public class Shape ---> public class Circle.

Here is the code for the shape and circle. No need to read, just here for reference; mostly getters and setters anyway. I would recommend looking at them last if my mistake isn't glaring.

abstract public class Shape {

    protected String color;
    protected boolean isFilled;

    public Shape() {
        color = "green";
        isFilled = true;
    }

    public Shape(String color, boolean isFilled) {
        this.color = color;
        this.isFilled = isFilled;
    }

    public String toString() {
        String filledString = (isFilled)? "is filled in." : "is not filled in.";
        return "A Shape of " + color + " color that " + filledString;
    }

    public String getColor() {
        return color;
    }

    public boolean getIsFilled() {
        return isFilled;
    }

    public boolean isFilled() {
        return isFilled;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public void setIsFilled(boolean isFilled) {
        this.isFilled = isFilled;
    }

    abstract public double getArea();
    abstract public double getPerimeter();
}

And the circle class.

public class Circle extends Shape {           // save as "Circle.java"
    protected double radius;

    public Circle() {
        radius = 1.0;
    }

    public Circle(double radius, String color, boolean isFilled) {
        super(color, isFilled);
        radius = this.radius;
    }

    // A public method for computing the area of circle
    @Override
    public double getArea() {
        return radius*radius*Math.PI;
    }

    @Override
    public double getPerimeter() {
        return 2 * Math.PI * radius;
    }

    public double getRadius() {
        return radius;
    }

    @Override
    public String toString() {
        return ("Radius: " + radius);
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }
}

My question has to do with the result of the following code execution:

public class TestShapes
{
    public static void main(String... args)
    {
        Shape s1 = new Circle(5.5, "RED", false);  // Upcast Circle to Shape
        System.out.println(s1);                    // which version? 
        System.out.println(s1.getArea());          // which version? 
        System.out.println(s1.getPerimeter());     // which version? 
        System.out.println(s1.getColor());
        System.out.println(s1.isFilled());
        // System.out.println(s1.getRadius());     // why doesn't this work?
    }
}

Note: Sorry for the large amount of code; I just don't know if there is something weird about the abstract class or something that is the real root of the misunderstanding.

I have no way to expain the output, which reads:

Radius: 0.0
0.0
0.0
RED
false

From this, it is clear that the first print command referred to the circle version of toString. However, I cannot explain why the next two print commands referred to the Shape version of getArea or getPerimeter; it seems like a contradiction! Both classes have an overridden method, so why would one refer to the circle method and one refer to the Shape method? Furthermore, if the toString() method worked on the circle, why won't the getRadius() method execute without an error?

louie mcconnell
  • 701
  • 3
  • 7
  • 20

1 Answers1

0

Your Circle constructor has a bug (which is causing you to not initialize radius). This

radius = this.radius;

should be

this.radius = radius;
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249