In inheritance the Methods are called in sequence of Bottom to Top(Child to Base) and Constructors are called from Top to Bottom (Base to Child). Lets see through an example.
Lets assume there are 2 classes , Base and Child with one constructor each and one common(overridden) method in each of them.
public class ABBA { public static void main(String[] args) { Base x = new Base(); Base y = new Child(); Child z = new Child(); System.out.println("Object of base class"); x.Print(); System.out.println("Object of child class by giving reference of base class"); y.Print(); System.out.println("Object of Child class"); z.Print(); } } class Base { Base(){ System.out.println("Base Constructor"); } public void Print() { System.out.println("Base Method"); } } class Child extends Base { Child(){ System.out.println("Child Constructor"); } public void Print() { System.out.println("Child Method"); } }
Output :
Base Constructor
Base Constructor
child Constructor
Base Constructor
child Constructor
Object of base class
Base Method
Object of child class by giving reference of base class
Child Method
Object of Child class
Child Method
Lets understand the program. So when there is a base and child , there can be 3 possibilities of object creation :
1) Creating object of child class
2) Creating object of base class
3) Creating object of child class by giving reference of base class.
What will happen :
1) In all the 3 scenarios the base constructor will be called for sure since we have inherited base class and constructor is called from Top to Bottom , that is from base to child.
2) When we will create object of base class then only the Base constructor is called. The child constructor is never called.
3) When we create object of child class in scenario 1 and 3 above the base and child constructors will be called.
4) When object of base class is called by giving reference of base class then only method of base class will be called.
5) In case of child class object(scenario 1 and 3), if the method is called which is overridden by child class then only child class method is called. Whenever we create object of child class then only the overridden methods are called. Method calling works from bottom to top.
6) If we call a method which is not present in child class then base class method will be called.
7) If a method is not present in base class and present in child class , then it will give error when we try to access it in scenario 3 (Creating object of child class by giving reference of base class)
8) In scenario 1 and 3 , only the overridden method will be called. Base class method which are overrided will never be called.
7) If a method is not present in base class and present in child class , then it will give error when we try to access it in scenario 3 (Creating object of child class by giving reference of base class)
8) In scenario 1 and 3 , only the overridden method will be called. Base class method which are overrided will never be called.
No comments:
Post a Comment