2
 class Glyph {
      void draw() {
        System.out.println("glyph.draw()");
      }

    Glyph() {
        System.out.println("glyph() before draw");
        draw();
        System.out.println("glyph() after draw");
    }
 }

 class RoundGlyph extends Glyph {
    private int radius = 1;

    RoundGlyph(int r) {
        radius = r;
        System.out.println("roundGlyph.RoundGlyph:" + radius);
    }

    @Override
    void draw() {
        System.out.println("roundGlyph.draw:" + radius);
    }
}

public class PolyConstructors {
    public static void main(String[] args) {
      new RoundGlyph(3);
    } 
}

the output is :

glyph() before draw
**roundGlyph.draw:0**
glyph() after draw
roundGlyph.RoundGlyph:3

I am confused about the second result,why the code draw() in the parent class constructor,actually invoke the child draw method other than parent draw method?

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
side
  • 209
  • 2
  • 9

3 Answers3

2

So that draw() call is calling this.draw() where this is your instantiated object.

In this case, this refers to an object of type RoundGlyph which has overridden the Draw() method. In the context of a RoundGlyph object, there is no Draw method that prints "glyph.draw()" That's gone.

CollinD
  • 7,304
  • 2
  • 22
  • 45
2

This happens because of overriding.

What is overriding ?

Method overriding, in object oriented programming, is a language feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its superclasses or parent classes. The implementation in the subclass overrides (replaces) the implementation in the superclass by providing a method that has same name, same parameters or signature, and same return type as the method in the parent class.

How is it decided which method will be executed ?

The version of a method that is executed will be determined by the object that is used to invoke it.

Notice that the object decides which method is called.


In your case an object of RoundGlyph is used when draw() is called. Since the class RoundGlyph overrides the draw function, the draw inside RoundGlyph is invoked.

For further reading see this and this

Community
  • 1
  • 1
Ghazanfar
  • 1,419
  • 13
  • 21
  • so every time when I initialize `RoundGlyph ` object with `new RoundGlyph() `,It is also initialize `Glyph ` object too. – side Sep 04 '15 at 05:43
  • @side Yes, when a derived class object is constructed, the base class is also constructed. Read more about inheritance [here](https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html) – Ghazanfar Sep 04 '15 at 05:52
  • @side See [this](http://stackoverflow.com/questions/7173019/why-is-constructor-of-super-class-invoked-when-we-declare-the-object-of-sub-clas) for more information. – Ghazanfar Sep 04 '15 at 06:03
1

Whenever you create object of sub-class default constructor of parent call is called and then the sub-class constructor. And method calls are determined at runtime and it depends on the object being created.

If you create object of Glyph (new Glyph()) then its Glyph.draw() method will be called. And if you create object of RoundGlyph then its RoundGlyph.draw() method will be called.

If you want to call parent class method from child class overridden method then you will have to explicitly call (using super keyword) it from child class method.

@Override
void draw() {
    super.draw();
    System.out.println("roundGlyph.draw:" + radius);
}

For more detailed explanation and examples refer inheritance documentation.

Naman Gala
  • 4,670
  • 1
  • 21
  • 55
  • so every time when I initialize RoundGlyph object with new RoundGlyph() ,whether It will initialize Glyph object or not – side Sep 04 '15 at 05:52
  • You can think it this way, inheritance is inheriting properties and method of super class as well as adding some new functionality. You can see how your super class default constructor is called when you initialized you sub class object. – Naman Gala Sep 04 '15 at 05:55
  • @side, I have added documentation link for **inheritance** and **super** keyword. – Naman Gala Sep 04 '15 at 06:19