Constructor chaining
Constructor chaining is phenomenon in which sub class constructor makes a call to its super class constructor and superclass constructor makes a call to its superclass constructor. It can be done implicit or explicit.**Note: 1. if constructor chaining will not happen then the inheritance will not happen at the level of class.Constructor never be inherited but it play major role/rules to achieve inheritance.Example:
Constructor Chaining Example:
class A{A(){System.out.println(" 1st construtor A() is exicuted");}void m1(){System.out.println("m1() of A is exicuted");}}class B extends A{B(){System.out.println("2nd construtor B() is exicuted ");}void m2(){System.out.println(" finally method m2() of B is exicuted ");}}class TestABCD{public static void main(String[] args) {B ref2 = new B();ref2.m2();}}Output1st construtor A() is exicuted2nd construtor B() is exicutedfinally method m2() of B is exicuted
No comments:
Post a Comment