Here is my code
class Glyph {
void draw() { System.out.println("Glyph.draw()"); }
Glyph(int i) {
System.out.println("Glyph() before draw()");
draw();
System.out.println("Glyph() after draw()");
}
}
class RoundGlyph extends Glyph {
private int radius = 1;
RoundGlyph(int r) {
super(r);
radius = r;
System.out.println("RoundGlyph.RoundGlyph(), radius = " + radius);
}
void draw() {
System.out.println("RoundGlyph.draw(), radius = " + radius);
}
}
public class PolyConstructors {
public static void main(String[] args) {
new RoundGlyph(5);
}
}
Output:
Glyph() before draw()
RoundGlyph.draw(), radius = 0
Glyph() after draw()
RoundGlyph.RoundGlyph(), radius = 5
Why is the constructor of the superclass Glyph
calling the draw method of the subclass RoundGlyph
? How can I call the draw method of the superclass from the constructors of the superclass? I tried this but it didn't work....