I'm confused by this code example:
I'm confused why the child method of draw is called first. I`m confused because when RoundGlyph is instantiated in main, dosen't the super constructor instantiate the parent object before the child?
class Glyph {
void draw() {
System.out.println("test"); // method will be called once you create a Glyph object, because when we create a round glyph before creating a glyph the super constructor will be called
}
void print2() {
System.out.println("printed from print 2");
}
int y1;
Glyph() {
y1 = 5;
System.out.println("y1 = " + y1);
System.out.println("Glyph() before draw()");
draw();
System.out.println("Glyph() after draw()");
print2();
}
}
class RoundGlyph extends Glyph {
int radius = 1;
RoundGlyph(int r) {
System.out.println("radius in RoundGlyph=" + radius);
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);
//new Glyph();
}
}