Thursday 24 January 2019

Constructor Chaining in Java – Changing Order & Using Super () Keyword

1. Objective

In our previous tutorial, we discussed Copy Constructor and Constructor Overloading in Java in detail. Here, we will learn about Constructor Chaining in Java with examples. Moreover, we will check what happens if we change the order of constructors and Java constructor chaining to other class using super() keyword.
So, let’s start Constructor Chaining in Java.
Constructor Chaining in Java
Constructor Chaining in Java

2. What is Constructor Chaining in Java?

Java constructor chaining is a method of calling one constructor with the help of another while considering the present object.
It can be done in 2 ways –
  • Within same class: It can be done using this() keyword for constructors in the same class.
  • From base class: By using super() keyword to call a constructor from the base class.
Constructor chaining happens through legacy. A subclass constructor’s undertaking is to call superclass’ constructor first. This guarantees formation of subclass protest begins with the introduction of the information individuals from the superclass. There could be any quantities of classes in a legacy chain. Each constructor calls up the chain till class at the best is come to.
  • This procedure is utilized when we need to play out various errands in a solitary constructor as opposed to making a code for each assignment in a solitary constructor we make a different constructor for each undertaking and make their chain which makes the program clearer.
Constructor Chaining In Java
Constructor Chaining In Java
  1. class Temp
  2. {
  3. Temp()
  4. {
  5. this(5);
  6. System.out.println("The Default constructor");
  7. }
  8. Temp(int x)
  9. {
  10. this(5, 15);
  11. System.out.println(x);
  12. }
  13. Temp(int x, int y)
  14. {
  15. System.out.println(x * y);
  16. }
  17. public static void main(String args[])
  18. {
  19. new Temp();
  20. }
  21. }
Output-
75
5
The Default constructor

a. What happens if we change the order of constructors?

  1. class Temp
  2. {
  3. Temp()
  4. {
  5. System.out.println("default");
  6. }
  7. Temp(int x)
  8. {
  9. this();
  10. System.out.println(x);
  11. }
  12. Temp(int x, int y)
  13. {
  14. this(5);
  15. System.out.println(x * y);
  16. }
  17. public static void main(String args[])
  18. {
  19. new Temp(8, 10);
  20. }
  21. }
Output-
default
5
80

b. Constructor Chaining to other class using super() keyword

  1. class Base
  2. {
  3. String name;
  4. Base()
  5. {
  6. this("");
  7. System.out.println("No-argument constructor of" +
  8. " base class");
  9. }
  10. Base(String name)
  11. {
  12. this.name = name;
  13. System.out.println("Calling parameterized constructor"
  14. + " of base");
  15. }
  16. }
  17. class Derived extends Base
  18. {
  19. Derived()
  20. {
  21. System.out.println("No-argument constructor " +
  22. "of derived");
  23. }
  24. Derived(String name)
  25. {
  26. super(name);
  27. System.out.println("Calling parameterized " +
  28. "constructor of derived");
  29. }
  30. public static void main(String args[])
  31. {
  32. Derived obj = new Derived("test");
  33. }
  34. }
Output-
Calling parameterized constructor of a base
Calling parameterized constructor of derived
So, this was all about Constructor Chaining in Java Tutorial. Hope you like our explanation.

No comments:

Post a Comment

40 Latest Interview Questions and Answers on Spring, Spring MVC, and Spring Boot

  40 Latest Interview Questions and Answers on Spring, Spring MVC, and Spring Boot 1. What is Tight Coupling? When a class (ClassA) is depen...