Constructors in Inheritance - Different scenarios explained
In this article, I will be covering how constructors work in inheritance. How Constructor invocation works in Inheritance Constructor invocation works slightly differently when inheritance is involved. When an object of the sub-class is created, it implicitly invokes the c onstructor in the base class . Once the code in the base class constructor is completed, control returns to the sub class constructor and the code is the sub-class constructor is executed. There are different scenarios in which the invocation differs slightly. Let’s explore these scenarios Scenario 1 – Base class has a constructor When the base-class has a constructor, creating an object of the sub-class results in automatic invocation of the base class constructor. Consider the following code snippet: public class Animal{public Animal() {System.out.println("In Animal constructor");}}public class Cat extends Animal{public Cat() {System.out.println("In Cat constructor");}} Here, The Animal class