Constructor calling

Constructor calling
  •  whenever subclass inherit property of super class, subclass constructor should make a call to the super class constructor either implicit or explicit call is made by super() statement.
  • If compiler makes call than its called implicit call
  • If programmer makes call to super class constructor using super() statement than its called explicit call
Example of implicit Constructor call
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
55
56
class demo3
{
 
	int k;
 
	demo3()
	{
		k=10;
		System.out.println("running demo3 constructor");
 
	}
}	
class Sample3 extends demo3
{
	double d;
	Sample3()
	{
		d=12.3;
		System.out.println("running Sample3 constructor");
	}
}
 
class Run3 extends Sample3
{
 
	Run3()
	{
	 System.out.println("running run3 constructor");
	}
 
}
 
 
public  class ConstructorCall 
{
 
	public static void main(String[] args)
	{
 
         System.out.println("*****************************");
 
         Run3 obj1=new Run3();//implicit call made by compiler
 
         System.out.println("*****************************");		
 
	}
 
}
 
output:
 
*****************************
running demo3 constructor
running Sample3 constructor
running run3 constructor
*****************************

Leave a Reply

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