1

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();
    }
}
GameDroids
  • 5,584
  • 6
  • 40
  • 59
Ben
  • 17
  • 1
  • 4

3 Answers3

-1

1 - When ever you create a child class object, Child class constructor is called. child class constructor call parent class constructor.

2 - why parent class constructor called ?

As you must know that child class have all the properties of parent class. So when ever you create a child class object, all the property of parent class is available in child class except private.

3 - what does constructor call do ?

Constructor call assign default value to all its variable but do not create an object of parent class.

4 - how can we prove that no object is created of parent class?

as you know, We can inherit abstract class. and when ever child class is object is created it also call abstract class constructor.

and we know that we can not create an object of abstract class.

SSP
  • 2,650
  • 5
  • 31
  • 49
-2

In constructor RoundGlyph(int r) as a first row, before System.out.println java will implicitly call supper() constructor of the supper class Glyph(). see this post

Eugen
  • 877
  • 6
  • 16
-2

The whole object is created (ie both child and parent structure), then the constructors are called in parent-to-child order.

If the constructor of the parent calls a method that is overridden in a child class, the child class' method is called (following the overriding semantics); there is nothing special about being in a constructor.

Note that it is a very bad idea to call a non-final method in a constructor (this is one of the items in Joshua Bloch's "Effective Java") for exactly that reason: the child method overriding it may access child fields that have not been initialized yet.

eg

class Parent {
    Parent() {
        method();
    }
    void method() { System.out.println(); }
}

class Child extends Parent {
    String output = "hello";
    void method() { if (output.equals("hello")) System.out.println(output); }
}

The constructor of Parent will be called before output is initialized (due to superclass-first initialization), so the overriding method will run into a NullPointerException.

daniu
  • 14,137
  • 4
  • 32
  • 53