Java what is constructor chaining

Java Constructor Chaining – Types & Example | Tutorials

Java Constructor Chaining – Calling one constructor from another constructor is generally referred to as constructor chaining. This can be achieved in two ways.

in some cases we use only this(), in some other cases, we use only super(). Sometimes we use both (but not directly).

Normal Java constructor calling (without chaining)

When we create an object, its constructor is invoked and the code in that constructor is executed. By default, the default constructor (a constructor without arguments) is invoked when we create an object.

If we specify any arguments along with object creation, then the corresponding (argumented) constructor will be executed.

System . out . println ( «constructer with two arguments type double and int » + «(» + x + «,» + y + «)» ) ;

System . out . println ( «constructer with two arguments type int and double» + «(» + x + «,» + y + «)» ) ;

System . out . println ( «constructer with three arguments two of type int and one is double» + «(» + x + «,» + y + «,» + z + «)» ) ;

Constructor chaining with this()

Constructor calling (with chaining):

Suppose we want to call both the constructors ( the default constructor and the argumented constructor ) with an object creation, then we call one constructor from another constructor so that both will be executed.

This mechanism of invoking more than one constructor (one constructor calling another constructor) with a single object creation is known as constructor chaining.

The method this() is used to call one constructor from another constructor.

Example for this():

In the above example, when an object is created, the control goes to the argumented constructor first (as the object is created with an int argument) and found this() as the first statement.

With this(), control goes to the default constructor and executes the code in that (prints A) and then comes back to execute the code in the argumented constructor (prints B).

Читайте также:  Create static map java

More constructors involved in chaining

If we want to call the default constructor then this() is used without any arguments. If we want to call any argumented constructor then this() is used with corresponding arguments.

In the following example, we have 3 constructors involved in the chaining.

In the above example, we are creating an object without arguments. So the default constructor is invoked first. From there (default constructor) the method this() is used with two arguments like “this(10,20)”.

  • So the corresponding constructor (with 2 arguments) will be invoked and control goes there. In that constructor (2-args) again this() is used with one argument like “this(x)”.
  • So the control goes to the 1-argumented constructor of the class to print B. From there it will come back to 2-args constructor to print C. from there it will come back to default constructor to print A.
Recursive constructor invocation is not allowed

We can call any number of constructors in this way. But an already invoked constructor should not be called again in the sequence . It means recursion is not allowed in constructor chaining. so the following code is invalid.

The this() should be the first statement in a constructor if it exists. We cannot write this() after any statement. So the following code is invalid.

Constructor Chaining with super()

We can call a constructor of the current class only with this(). When inheritance is involved, and a child class object is created, an object of the parent class is also created as part of the child class object.

  • As parent object is created (along with child object) its constructor should also be invoked. In this case also, by default, the default constructor of the parent class is invoked with child class constructor.
  • If we want any argumented constructor of the parent class to be called with a child class constructor then we can exclusively invoke that using super().
  • Using super() without arguments will call the default constructor of the parent class.
  • To call (invoke) any argumented constructor of the parent class we should invoke super() with corresponding arguments.
Читайте также:  Java bufferedimage to image file

Note: when a child class object is created, even though the control comes to child class constructor first, it will not execute the code in child class constructor.

From child class constructor it will go to the parent class constructor and executes it. then only it comes back to execute the child class constructor. So the parent class constructor is executed before the child class constructor.

In the above example, we have created 3 objects of Emp (child class). For each object creation, a parent class constructor and a child class constructor are invoked.

For the first statement, “new Emp()”, control goes to default constructor of child class (Emp) and there is no specification of which version of the parent class constructor to call.

So the default constructor of the parent class (Person) is invoked. After executing the parent class constructor the control comes back to execute the actual code of child class constructor. This part prints AX.

  • For the second statement, “new Emp(10)”, control goes to the 1-argumented constructor of Emp and found super() without arguments there.
  • So in this case also the default constructor of Person is executed and then, after coming back, the code in Emp(int) is executed. This part prints AY.
  • For the third statement, “new Emp(20,30)”, control goes to the 2-argumented constructor of Emp and found super(x). so it will go to the 1-argumented constructor of Person.

Hence code in Person(int) is executed and then, after coming back, the code in Emp(int, int) is executed. This part prints BZ.

More chaining with super():

Suppose three classes are involved (say A, B and C where A is a parent to B and B is a parent to C) and third class (C) object is created, then control goes to the constructor of C

But before executing the statements in that it goes to the constructor of B. Even in B, before executing statements of B, it goes to the constructor of A.

After executing the code in A constructor, the control comes back to execute the code in B’s constructor. Once that execution is over, the control comes to execute code in C.

Читайте также:  Php fatal errors list

Источник

Java what is constructor chaining

Learn Latest Tutorials

Splunk tutorial

SPSS tutorial

Swagger tutorial

T-SQL tutorial

Tumblr tutorial

React tutorial

Regex tutorial

Reinforcement learning tutorial

R Programming tutorial

RxJS tutorial

React Native tutorial

Python Design Patterns

Python Pillow tutorial

Python Turtle tutorial

Keras tutorial

Preparation

Aptitude

Logical Reasoning

Verbal Ability

Company Interview Questions

Artificial Intelligence

AWS Tutorial

Selenium tutorial

Cloud Computing

Hadoop tutorial

ReactJS Tutorial

Data Science Tutorial

Angular 7 Tutorial

Blockchain Tutorial

Git Tutorial

Machine Learning Tutorial

DevOps Tutorial

B.Tech / MCA

DBMS tutorial

Data Structures tutorial

DAA tutorial

Operating System

Computer Network tutorial

Compiler Design tutorial

Computer Organization and Architecture

Discrete Mathematics Tutorial

Ethical Hacking

Computer Graphics Tutorial

Software Engineering

html tutorial

Cyber Security tutorial

Automata Tutorial

C Language tutorial

C++ tutorial

Java tutorial

.Net Framework tutorial

Python tutorial

List of Programs

Control Systems tutorial

Data Mining Tutorial

Data Warehouse Tutorial

Javatpoint Services

JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.

  • Website Designing
  • Website Development
  • Java Development
  • PHP Development
  • WordPress
  • Graphic Designing
  • Logo
  • Digital Marketing
  • On Page and Off Page SEO
  • PPC
  • Content Development
  • Corporate Training
  • Classroom and Online Training
  • Data Entry

Training For College Campus

JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week

Like/Subscribe us for latest updates or newsletter RSS Feed Subscribe to Get Email Alerts Facebook Page Twitter Page YouTube Blog Page

Источник

Java what is constructor chaining

  • Introduction to Java
  • The complete History of Java Programming Language
  • C++ vs Java vs Python
  • How to Download and Install Java for 64 bit machine?
  • Setting up the environment in Java
  • How to Download and Install Eclipse on Windows?
  • JDK in Java
  • How JVM Works – JVM Architecture?
  • Differences between JDK, JRE and JVM
  • Just In Time Compiler
  • Difference between JIT and JVM in Java
  • Difference between Byte Code and Machine Code
  • How is Java platform independent?
  • Decision Making in Java (if, if-else, switch, break, continue, jump)
  • Java if statement with Examples
  • Java if-else
  • Java if-else-if ladder with Examples
  • Loops in Java
  • For Loop in Java
  • Java while loop with Examples
  • Java do-while loop with Examples
  • For-each loop in Java
  • Continue Statement in Java
  • Break statement in Java
  • Usage of Break keyword in Java
  • return keyword in Java
  • Object Oriented Programming (OOPs) Concept in Java
  • Why Java is not a purely Object-Oriented Language?
  • Classes and Objects in Java
  • Naming Conventions in Java
  • Java Methods
  • Access Modifiers in Java
  • Java Constructors
  • Four Main Object Oriented Programming Concepts of Java
  • Inheritance in Java
  • Abstraction in Java
  • Encapsulation in Java
  • Polymorphism in Java
  • Interfaces in Java
  • ‘this’ reference in Java

Источник

Оцените статью