-2

I am new to Java.

So I was willing to know that when there are two inheritance classes present and it has 2 overridden methods and 2 constructors (default constructors).

So when subclass object is created, both default constructors are invoked according to order of derivation. But does it happen in case of methods that are overridden??

Bom Tomdabil
  • 61
  • 1
  • 10
Jay Desai
  • 231
  • 1
  • 2
  • 12

2 Answers2

0

The answer to you question is, No.

In case of constructors, the default constructor of the derived or child class implicitly calls super(). super() means, calling the constructor of the parent class. Hence when an object of the child class is created, a call goes to constructor of child class whose first line is an implicit call to super() (aka the constructor of the corresponding base class).Hence in the output, the statements of the parent class constructor are printed before those of the child class constructor.

In case of overriding, only the method you define in the child class is executed, when called using the child class object, because we are overriding the method which was defined in the parent class.

-1

When a method is overridden in Java, the method in the child class takes the place of the method in the parent class, so only the method in the child class is executed. See this information on inheritance.

Bom Tomdabil
  • 61
  • 1
  • 10