2
public class Circle {

    public static final double PI = 3.141592654;
    protected double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

    @Override
    public String toString() {
        return "Class = " + getClass().getSimpleName() + " (radius = " + radius + ")";
    }
}


public class PlaneCircle extends Circle {

    private double centerX, centerY;

    public PlaneCircle(double radius, double centerX, double centerY) {
        super(radius);
        this.centerX = centerX;
        this.centerY = centerY;
    }

    @Override
    public String toString() {
        return super.toString();
    }
}

Suppose the above two classes are in different files.

When I create an instance of PlaneCircle (in another java file) like the following two lines...

PlaneCircle planeCircle1 = new PlaneCircle(3, 6, 7);
System.out.println(planeCircle1.toString());

What I get in the Console output is

Class = PlaneCircle (radius = 3.0)

The toString() method in PlaneCircle calls super.toString(), and the toString() method in Circle should give "Circle" when I use the getClass().getSimpleName().

My question is, why the output is "PlaneCircle" instead of "Circle" in this case even though I have created an instance of the subclass (PlaneCircle)? Does this have anything to do with reflection?

2
  • The object on which the method is getting invoked matters. In this case, the object is PlainCircle and you will get PlanCircle in toString Aug 16, 2015 at 20:04
  • The constant PI is already defined in Math class. See Math.PI.
    – Tunaki
    Aug 16, 2015 at 20:05

3 Answers 3

4

planeCircle1 is an instance of PlaneCircle, which means getClass() would return PlaneCircle.class (i.e. the Class instance that represents the PlaneCircle class) and getClass().getSimpleName() would return that class's name - "PlaneCircle".

It doesn't matter that getClass().getSimpleName() is called from a method of the base class Circle, since when you call a method without an instance variable, you are calling it on the current instance (i.e. getClass() is the same as this.getClass(), and this is an instance of PlaneCircle in your code sample).

1

getClass() is also polymorphic - it returns a Class object that represents the instance it belongs to, in this case, a PlaneCircle. If you don't want this behavior, you could either hard code Circle:

@Override
public String toString() {
    return "Class = circle (radius = " + radius + ")";
}

Or statically access the Circle class:

@Override
public String toString() {
    return "Class = " + Circle.class.getSimpleName() + " (radius = " + radius + ")";
}
0

Since you are operating on an instance of PlaneCircle and not Circle, getClass.getSimpleName() will be called on your current instance of PlaneCircle. With the current setup you may not be able to print "Circle" using polymorphic behavior. At Runtime, methods of PlaneCircle will be called.

1
  • Did you test the code? My side still shows the same result with your code.
    – Roy
    Aug 16, 2015 at 20:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.