super() method in java

Previous
Next

super():

  • super() is used to access Parent class constructor from the child class. It is used to initialize Parent object from Child.
  • We must use super() method inside the child class constructor only.
  • Call to super() method must be the first statement in the Child’s constructor. Calling(connecting) Parent constructor from Child is called ‘Constructor Chaining’ in Is-A relation.
  • In the process of child class object construction, it is not possible to invoke parent’s constructor explicitly to initialize non static variables of Parent class.
this()super()
It is used to invoke current class constructor.It is used to invoke Parent class constructor from Child.
It must be used only inside another constructor of same class.It must be used only inside Child’s constructor.
Call to this() must be the first statement.Call to super() must be the first statement.
To connect constructors in a single class.To connect constructors in Is-A relation.
Used to initialize an object via multiple constructors.Used to initialize parent class object in the process of child object creation.

Note: In the process of child class object construction, JVM implicitly creates Parent’s object first. For this construction, JVM uses super() method.

/*class Object
{
	Object()
	{
		Object creation.....
	}
}*/
class Parent extends Object
{
	Parent()
	{
		// super(); /* compiler added code */
		System.out.println("Parent's instantiation.....");		
	}
}
class Child extends Parent 
{
	Child()
	{
		// super(); /* compiler added code */
		System.out.println("Child's instantiation.....");
	}
	public static void main(String[] args) 
	{
		Child c = new Child();
	}
}

When we call super() method explicitly?

  • In the process of Child object creation it is not possible to invoke Parent class constructor explicitly to provide initialization.
  • We use super() method in the child class constructor to access Parent’s constructor. Hence we can initialize Parent object.
class Parent
{
	int a ;
	Parent(int a)
	{
		this.a = a ;
	}
}
class Child extends Parent 
{
	Child(int a)
	{
		super(a); /* calling Parent's arguments constructor */
	}
}
class MainClass
{
	public static void main(String[] args) 
	{
		Child obj = new Child(10);
	}
}

Initializing Parent and Child objects:

class Parent
{
	int a ;
	Parent(int a)
	{
		this.a = a ;
	}
}
class Child extends Parent 
{
	int b ;
	Child(int a, int b)
	{
		super(a); 
		this.b = b ;
	}
	void display()
	{
		System.out.println("Parent's a value : "+super.a);
		System.out.println("Child's b value : "+this.b);
	}
}
class MainClass
{
	public static void main(String[] args) 
	{
		Child obj = new Child(10,20);
		obj.display();
	}
}

Practice this code:

class Parent extends Object
{
	int a, b ;
	Parent(int a, int b)
	{
		this.a = a ;
		this.b = b ;
	}
}
class Child extends Parent
{
	int x, y;
	Child(int a, int b, int x, int y)
	{
		super(a, b);
		this.x = x ;
		this.y = y ;
	}
	void details()
	{
		System.out.println("x value : " + this.x);
		System.out.println("y value : " + this.y);
		System.out.println("a value : " + super.a);
		System.out.println("b value : " + super.b);
	}
}
class Inheritance 
{
	public static void main(String[] args) 
	{
		Child obj = new Child(10, 20, 30, 40);
		obj.details();
	}
}

Note: Compiler writes a default constructor only when we don’t define any constructor in the class. Hence we need to check the entire constructor is available while connecting them in Constructor Chaining process.

class Parent
{
}
class Child extends Parent
{
}
class Inheritance 
{
	public static void main(String[] args) 
	{
		new Child(); // invoke Default constructors of Parent and Child to create object.
	}
}

When we don’t define any constructor, then only compiler will add default constructor. The code as follows:

class Parent
{
	/*Parent()
	{
	}*/
}
class Child extends Parent
{
	/*Child()
	{
		super();
	}*/
}
class Inheritance 
{
	public static void main(String[] args) 
	{
		new Child(); 
	}
}

If we write any arguments constructor, compiler will not add default constructor(zero args).

class Parent
{
	Parent(int a)
	{
	}
}
class Child extends Parent
{
	/*Child()
	{
		super();  // Error : trying to invoke default constructor implicitly.
	}*/
}
class Inheritance 
{
	public static void main(String[] args) 
	{
		new Child(); 
	}
}
  • We can define all the required constructors explicitly in the program and these constructors can be connected using this() and super() in the process of constructor chaining.
  • We can connect only one constructor from another constructor because call to either this() or super() method must be the first statement in the constructor.
class Parent
{
	Parent()
	{
		this(10);
		System.out.println("Parent's zero args constructor");
	}
	Parent(int a)
	{
		System.out.println("Parent's args constructor");
	}
}
class Child extends Parent
{
	Child()
	{
		this(10);
		System.out.println("Child's zero args constructor");
	}
	Child(int a)
	{
		super();
		System.out.println("Child's args constructor");
	}
}
class Inheritance 
{
	public static void main(String[] args) 
	{
		new Child();  
	}
}

Write code to connect the constructors in following order.

class Parent
{
	Parent()
	{
		System.out.println("Parent's zero args constructor");
	}
	Parent(int a)
	{
		this();
		System.out.println("Parent's args constructor");
	}
}
class Child extends Parent
{
	Child()
	{
		super(10);
		System.out.println("Child's zero args constructor");
	}
	Child(int a)
	{
		this();
		System.out.println("Child's args constructor");
	}
}
class Inheritance 
{
	public static void main(String[] args) 
	{
		new Child(10);  
	}
}
  • Call to super() or this() must be the first statement in the constructor.
  • This is why we can connect only one constructor from another.
class Parent
{
	Parent()
	{
	}
}
class Child extends Parent
{
	Child()
	{
		super();
		this(10); // not allowed - must be the first statement
	}
	Child(int a)
	{
	}
}
class Inheritance 
{
	public static void main(String[] args) 
	{
		new Child();
	}
}
Previous
Next

Add Comment

Courses Enquiry Form