0% found this document useful (0 votes)
41 views6 pages

Unit-III Session 23 Constructors in Subclass

The document discusses the order of execution of constructors in Java inheritance. It explains that in single inheritance, the base class constructor is executed first before the derived class constructor. In multilevel inheritance, constructors are executed from topmost base class down to the bottom most derived class. Constructors can also call other constructors of the same class using the this keyword or call parent class constructors using the super keyword.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views6 pages

Unit-III Session 23 Constructors in Subclass

The document discusses the order of execution of constructors in Java inheritance. It explains that in single inheritance, the base class constructor is executed first before the derived class constructor. In multilevel inheritance, constructors are executed from topmost base class down to the bottom most derived class. Constructors can also call other constructors of the same class using the this keyword or call parent class constructors using the super keyword.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

19CST102 & Object Oriented Programming

SNS COLLEGE OF TECHNOLOGY


(An Autonomous Institution)
COIMBATORE – 35
DEPARTMENT OF COMPUTER SIENCE AND ENGINEERING

UNIT 3

Order of Execution of Constructors in Java


Inheritance
Constructors in Java
A constructor in Java is similar to a method with a few differences. Constructor has the
same name as the class name. A constructor doesn't have a return type.

A Java program will automatically create a constructor if it is not already defined in the
program. It is executed when an instance of the class is created.

A constructor cannot be static, abstract, final or synchronized. It cannot be overridden.

Java has two types of constructors:

1. Default constructor
2. Parameterized constructor

What is the order of execution of constructor in Java


inheritance?
While implementing inheritance in a Java program, every class has its own constructor.
Therefore the execution of the constructors starts after the object initialization. It
follows a certain sequence according to the class hierarchy. There can be different
orders of execution depending on the type of inheritance.

Different ways of the order of constructor execution in


Java
1. Order of execution of constructor in Single
inheritance
In single level inheritance, the constructor of the base class is executed first.

Mrs.S.R.Janani, AP/CSE Page 1


19CST102 & Object Oriented Programming

OrderofExecution1.java

1. /* Parent Class */
2. class ParentClass
3. {
4. /* Constructor */
5. ParentClass()
6. {
7. System.out.println("ParentClass constructor executed.");
8. }
9. }
10.
11. /* Child Class */
12. class ChildClass extends ParentClass
13. {
14. /* Constructor */
15. ChildClass()
16. {
17. System.out.println("ChildClass constructor executed.");
18. }
19. }
20.
21. public class OrderofExecution1
22. {
23. /* Driver Code */
24. public static void main(String ar[])
25. {
26. /* Create instance of ChildClass */
27. System.out.println("Order of constructor execution...");
28. new ChildClass();
29. }
30. }

Output:

Order of constructor execution...


ParentClass constructor executed.
ChildClass constructor executed.

Mrs.S.R.Janani, AP/CSE Page 2


19CST102 & Object Oriented Programming

In the above code, after creating an instance


of ChildClass the ParentClass constructor is invoked first and then the ChildClass.

2. Order of execution of constructor in Multilevel


inheritance
In multilevel inheritance, all the upper class constructors are executed when an
instance of bottom most child class is created.

OrderofExecution2.java

1. class College
2. {
3. /* Constructor */
4. College()
5. {
6. System.out.println("College constructor executed");
7. }
8. }
9.
10. class Department extends College
11. {
12. /* Constructor */
13. Department()
14. {
15. System.out.println("Department constructor executed");
16. }
17. }
18.
19. class Student extends Department
20. {
21. /* Constructor */
22. Student()
23. {
24. System.out.println("Student constructor executed");
25. }
26. }
27. public class OrderofExecution2
28. {

Mrs.S.R.Janani, AP/CSE Page 3


19CST102 & Object Oriented Programming

29. /* Driver Code */


30. public static void main(String ar[])
31. {
32. /* Create instance of Student class */
33. System.out.println("Order of constructor execution in Multilevel inheritance...");
34. new Student();
35. }
36. }

Output:

Order of constructor execution in Multilevel inheritance...


College constructor executed
Department constructor executed
Student constructor executed

In the above code, an instance of Student class is created and it invokes the
constructors of College, Department and Student accordingly.

3. Calling same class constructor using this keyword


Here, inheritance is not implemented. But there can be multiple constructors of a
single class and those constructors can be accessed using this keyword.

OrderofExecution3.java

1. public class OrderofExecution3


2. {
3. /* Default constructor */
4. OrderofExecution3()
5. {
6. this("CallParam");
7. System.out.println("Default constructor executed.");
8. }
9. /* Parameterized constructor */
10. OrderofExecution3(String str)
11. {
12. System.out.println("Parameterized constructor executed.");
13. }
14. /* Driver Code */
15. public static void main(String ar[])

Mrs.S.R.Janani, AP/CSE Page 4


19CST102 & Object Oriented Programming

16. {
17. /* Create instance of the class */
18. System.out.println("Order of constructor execution...");
19. OrderofExecution3 obj = new OrderofExecution3();
20. }
21. }

Output:

Order of constructor execution...


Parameterized constructor executed.
Default constructor executed.

In the above code, the parameterized constructor is called first even when the default
constructor is called while object creation. It happens because this keyword is used as
the first line of the default constructor.

4. Calling superclass constructor using super keyword


A child class constructor or method can access the base class constructor or method
using the super keyword.

OrderofExecution4.java

1. /* Parent Class */
2. class ParentClass
3. {
4. int a;
5. ParentClass(int x)
6. {
7. a = x;
8. }
9. }
10.
11. /* Child Class */
12. class ChildClass extends ParentClass
13. {
14. int b;
15. ChildClass(int x, int y)
16. {
17. /* Accessing ParentClass Constructor */

Mrs.S.R.Janani, AP/CSE Page 5


19CST102 & Object Oriented Programming

18. super(x);
19. b = y;
20. }
21. /* Method to show value of a and b */
22. void Show()
23. {
24. System.out.println("Value of a : "+a+"\nValue of b : "+b);
25. }
26. }
27.
28. public class OrderofExecution4
29. {
30. /* Driver Code */
31. public static void main(String ar[])
32. {
33. System.out.println("Order of constructor execution...");
34. ChildClass d = new ChildClass(79, 89);
35. d.Show();
36. }
37. }

Output:

Order of constructor execution...


Value of a : 79
Value of b : 89

Mrs.S.R.Janani, AP/CSE Page 6

You might also like