In: Computer Science
How to identify when to use super() in constructor for java language in a easy way?
-------------------- I Used Online Java Compiler --------------------
The super keyword mainly refers to the objects of parent class.
Inorder to use super keyword you need to know the Inheritance concept.
Your question is "when to use super() in constructor for java language"
I mainly use super keyword for two reasons
1. To get parent class variables and methods from child
class.
2. To reuse as well as reduce repeatation code
In java, If we are using inheritance concept between two or more classes then by default with or without specifying super(); will execute parent class default constructor, NOT parameterised constructor.
I will demostrate now,
CASE 1:
In the below example, Inside main method I created only the object of AnotherChild class even though the two parent class default constructor is called.
CASE 2:
POINT TO REMEMBER is that after compiled, Compiler knows the hierarchy of all parent and child classes. And execution order goes like parent class to child class, Parent class constructor is called first then its child then it's child ...likewise.
In the above example, AnotherChild class object is
calling it's parameterised constructor ("John Wick"),
So inside AnotherChild parameterised constructor I am calling
super(name); , So it call it's parent class parameterised
constructor, In this case Child(String name) constructor is called
and before it enter into child class its parent class default
constructor is called.
So that is the reason output come like this
Parent Class Constructor
Child Class Constructor : John Wick
I can demonstrate use of super() keyword using further more example,
Consider Person class which contain variables id and name and one parameterised constructor which store the values to id and name. Here "this" keyword is used to distinguish between parameters and it's class variables. In this case both parameters name and class variable names are same.
Employee class inherits Person class, So person class
variables and methods can be accessable by child class.
Employee class contains one variable salary, one method and one
parameterised constructor.
Create one object of Employee class and use parameterised constructor to pass the employee details and then display.
Inside Employee parameterised constructor, I am calling super(id,name) which initialise the id and name in the Person class which can also be used from Employee class.
The above example is very simple, The real use comes when so many COMMON variables and methods come into picture. I hope you got the idea.
------------------ CODE ------------------
class Person
{
int id;
String name;
Person (int id, String name)
{
this.id = id;
this.name = name;
}
}
class Employee extends Person
{
float salary;
Employee (int id, String name, float salary)
{
//Reusing parent parameterised constructor using super(parameter ,
parameter)
//id and name is passing to the parent class variable
super (id, name);
//Only salary is specifying here but still we get all the
variables(id,name) of parent class
//because of inheritance
this.salary = salary;
}
void Display ()
{
//Even though id and name is not specified in this class, we can
use these variables from parent class
//If child class Employee also specified id and name
//then we need to use
//super.id and super.name instead of just printing id and
name
//System.out.println ("Employee ID : "+super.id);
//System.out.println ("Employee Name : "+super.name);
System.out.println ("Employee ID : "+id);
System.out.println ("Employee Name : "+name);
System.out.println ("Employee Salary : "+salary);
}
}
public class Main
{
public static void main (String[]args)
{
//Calling the parameterised constructor
Employee emp = new Employee(1001, "John Wick", 60000f);
//Display the values
emp.Display();
}
}
Please check the below explanations also:
We can use the parent class variable or method from child class using super keyword.
In other case, If parent and child class have same variable name, then use of super keyword is to distinguish which variable belong to parent class and child class
-------------------- CODE --------------------
class Parent
{
int noOfBabies = 10;
}
//Child class inherit parent class using extends
keyword
class Child extends Parent
{
int noOfBabies = 4;
void Print()
{
System.out.println("Number of Babies : " + noOfBabies);
System.out.println("Number of Babies : " + super.noOfBabies);
}
}
public class Main
{
public static void main(String[] args)
{
Child child = new Child();
child.Print();
}
}
Below screenshot demonstrate the parent class and child class METHOD can be distinguishable using super keyword.
-------------------- CODE --------------------
class Parent
{
int noOfBabies = 10;
void Print()
{
System.out.println("Number of Babies : " + noOfBabies);
}
}
//Child class inherit parent class using extends
keyword
class Child extends Parent
{
int noOfBabies = 4;
void Print()
{
System.out.println("Number of Babies : " + noOfBabies);
}
void DisplayAll()
{
//Calling child class method
Print();
//Calling parent class method using super
super.Print();
}
}
public class Main
{
public static void main(String[] args)
{
Child child = new Child();
child.DisplayAll();
}
}