Java OOP – Inheritance (Part II)
August 10, 2020 1 min read

In the last article we discussed about 6 cases from Inheritance (Part I), one of the main concepts in OOP. Make sure that you have knowledge on those cases before reading of the today article.
Let’s go to the rest of the Interface lesson.
Case VII
When a sub class object is initiated in the main method, the sub class object must also automatically execute one of the constructors of the super class.

Output:

To call a super class constructor you can also use “super” keyword. If you don’t use “super” keyword compiler always runs the default constructor as above program. super() should be first statement inside any constructor. It can be used only inside constructor.

Output:

Case VIII
If you want to call sub class (current class) constructor, you can use “this” keyword. this() should be first statement inside any constructor. It can be used only inside constructor.

Output:

Case IX
Parameters used for the constructors of the super class should be compatible with the arguments you give to the super statement when you call it.

Output:

Case X
You can’t call by assigning super class object to a sub class reference inside the main method because sub class is a extended class. But you can call by assigning sub class object to a super class reference.

One of the main concepts of OOP, Inheritance is finished now. Let’s meet with the next article on the topic “Polymorphism”, another main concept of OOP.
Thank you.


