Super() method

What is the use of Super() Method in java ?
Whenever subclass inherit property of super class, subclass constructor should
make a call to the super class constructor either implicit(by compiler) or
explicit call is made by super() statement.
super() Method
  • super() Used to call super class constructor
  • super() Used inside constructor body only
  • It Must be first statement of constructor
  • Multiple super() statement not allowed
This is example of explicit constructor calling made by programmer using super()
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
 
package constructor;
 
 
 
class demo12
{
	demo12(int k)
	{
		System.out.println("running demo12 int arg constructor");
	}	
}
 
class Sample12 extends demo12
{
   Sample12(double d)
   {
 
           super(25);// call made to int arg constructor
	   System.out.println("running SAmple12 double arg constructor");
   }	
}
 
class Run12 extends Sample12
{
	Run12()
	{
		super(2.4); //call made to double arg constructor
		System.out.println("running Run12 constructor");
 
	}
 
}
 
 
public class ConstructorCallUsingSuper 
 
{
	public static void main(String[] args)
	{
 
		System.out.println("*****************************");	
		Run12 obj=new Run12();	
		System.out.println("*****************************");
 
	}
}
 
output:
*****************************
running demo12 int arg constructor
running SAmple12 double arg constructor
running Run12 constructor
*****************************

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
*****************************