Constructor Chaining

Constructor chaining
  •  Constructor chaining is phenomenon which take place during inheritance
    in which sub class constructor make a call to super class constructor,
    super class constructor call to its super class constructor.
  • Constructor chaining happen either implicit or explicit
Constructor chaining Example(implicit)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class demo33
{
 
	int k;
 
	demo33()
	{
		k=10;
		System.out.println("running demo33 constructor");
 
	}
}	
class Sample33 extends demo33
{
 
	double d;
	Sample33()
	{
		d=12.3;
		System.out.println("running Sample33 constructor");
	}
}
class Run33 extends Sample33
{
 
	Run33()
	{
 
		System.out.println("running run33 constructor");
	}
 
}
 
 
public  class ConstructorChaining 
{
 
	public static void main(String[] args)
	{
 
         System.out.println("*****************************");
         Run3 obj1=new Run3();//implicit call made by compiler
         System.out.println("*****************************");		
 
	}	
   }
 
output:
 
*****************************
running demo33 constructor
running Sample33 constructor
running run33 constructor
*****************************

Leave a Reply

Your email address will not be published. Required fields are marked *