Order of Execution of Constructors in Java Inheritance
Constructor in Java
There are several distinctions between a method and a function object() in Java. The name of the function object() is identical to the class name. There is no return type for a function object().
If a function object() isn't already declared in the application, Java will automatically build one. Whenever a class instance is formed, it is carried out.
Can not synchronize object () function, final, conceptual, static, or abstract. It is not revocable.
Java contains two different sorts of constructors.
standard function object()
function object() with parameters
Which is the inheritance order in Java for function object() execution?
Each class in a Java application that uses inheritance has its function, object(). As a result, the constructors begin to run after the object is initialized. It proceeds in a certain order by the class hierarchy. How things happen might vary depending on the inheritance type.
Different Java function object() execution order methods
1. order in which constructors are executed in single inheritance
OrderofExecutions1.java
The root class's function object() is called first in single-level inheritance.
ParentClasss .java
//this program is for the inheritance in java
//importing the required packages
import java.io.*;
import java.util.*;
class ParentClasss
{
// the default constructor
ParentClasss()
{
System.out.println("The execution of the parent class.");
}
}
// the child class
class ChildClasss extends ParentClasss
{
// the default constructor of the child class
ChildClasss()
{
System.out.println("The child class is execued.");
}
}
public class OrderofExecutions1
{
//mainsection of the program
public static void main(String ar[])
{
// an object is created for the child class
System.out.println("The order follows as:");
new ChildClasss();
}
}
Output
The order follows as:
The execution of the parent class.
The child class is execued.
The ParentClass function Object() is called first in the language above, followed by the ChildClass, after constructing an instance of the ChildClass.
2. The order in which constructors are executed under multilevel inherit
Every higher-class constructor is called when a new sample of the lowermost child class is generated under multidimensional inheritance.
OrderofExecutions2.java
//this program is for the inheritance in java
//importing the required packages
import java.io.*;
import java.util.*;
class Colleges
{
// the default constructor
Colleges()
{
System.out.println("Colleges constructor is executed");
}
}
class Departments extends Colleges
{
// the default constructor
Departments()
{
System.out.println("Departments constructor is executed");
}
}
class Students extends Departments
{
// the default constructor
Students()
{
System.out.println("Execution of the Students Constructors");
}
}
public class OrderofExecutions2
{
//main section of the program
public static void main(String ar[])
{
// an object is created for the Students
System.out.println("Execution sequence for constructors in multilayer inherit..");
new Students();
}
}
Output
Execution sequence for constructors in multilayer inherit..
Colleges constructor is executed
Departments constructor is executed
Execution of the Students Constructors
The code above creates a student's class object and calls the appropriate constructors for the colleges, departments, and students as needed.
3. Using this keyword to invoke the same class' function object()
Inheritance is not used in this situation. However, a single class may have several constructors so that this keyword can access these constructors.
OrderofExecutions3.java
//this program is for the inheritance in java
//importing the required packages
import java.io.*;
import java.util.*;
public class OrderofExecutions3
{
// the default constructor of the class OrderofExecutions3
OrderofExecutions3()
{
this("Ramu");
System.out.println("The default constructor is executed");
}
// constructor having the string as a parameter
OrderofExecutions3(String string)
{
System.out.println("The parameterised constructor is executed");
}
// main section of the program
public static void main(String arr[])
{
//an object is created for the class
System.out.println("The order of the constructors execution.");
OrderofExecutions3 object = new OrderofExecutions3();
}
}
Output
The order of the constructors execution.
The parameterised constructor is executed
The default constructor is executed
Even though the default function Object() is invoked during object creation, the parametric function Object() is called first in the method given. This occurs due to the standard constructor's initial line, including the keywords.
4. Using the super keyword to invoke the superclass function Object()
The super keyword allows a child class function Object() or method to call the base class function Object() or function.
ParentClasses.java
//this program is for the inheritance in java
//importing the required packages
import java.io.*;
import java.util.*;
class ParentClasses
{
int a1;
ParentClasses(int x1)
{
a1 = x1;
}
}
// the base class
class ChildClasses extends ParentClasses
{
int b1;
ChildClasses(int x1, int y1)
{
//the super keyword
super(x1);
b1 = y1;
}
// the method for determining the values of the a and b
void Shows()
{
System.out.println("The variable a1 value="+a1+"\n"+"The variable b1 value="+b1);
}
}
public class OrderofExecutions4
{
//main section of the program
public static void main(String arr[])
{
System.out.println("The order in which the constructor is executing is:");
ChildClasses d1 = new ChildClasses(791, 891);
d1.Shows();
}
}
Output
The order in which the constructor is executing is:
The variable a1 value=791
The variable b1 value=891