×

Constructor Chaining and Constructor Overloading in Java

Constructor Chaining

Constructor chaining and constructor overloading are two confusing terms. Let's first understand constructor chaining.
Constructor chaining is the process of calling one constructor from another constructor using the same object. To call the constructor, we use either this or super keyword. 'this' keyword is used in the case when a call is made from the same class whereas 'super' keyword is used in a case when a call is made for the super i.e. parent class constructor.

A constructor can be called explicitly or automatically. this () and this (parameter) is used for calling same class non-parameterized and parameterized constructor respectively. Similarly super () and super (parameter) is used for calling parameterized and non-parameterized constructor of the superclass.

Why constructor chaining is needed

In order to perform multiple tasks within the same constructor, rather than using a separate constructor for each task, constructor chaining is used. It makes the program more readable.

Example:

public class A
{
public A()
{
System.out. println ("this is non parameterized constructor of A class");
}
public A(String name)
{
System.out.println ("this is parameterized constructor of A class called by"+name);
}
}
class B extends A
{
public B()
{
this(" javatpoint ");
System.out.println ("this is non parameterized constructor of child class");
}
public B(String string)
{
super ("javatpoint");
System.out.println ("this is parameterized constructor of child class");
}




public static void main(String args[])
{
B b = new B ();
}
}

Output:

this is parameterized constructor of A class called by javatpoint.
this is parameterized constructor of child class.
this is non-parameterized constructor of child class.

Constructor Overloading

As methods are overloaded in Java, in the same way, constructors can also be overloaded. Constructor overloading is having more than one constructor within different parameters lists. Then the question is how the compiler will distinguish b/w them? Actually, Compiler can distinguish b/w these constructors with the help of parameters and data types.

Example:

class Employee
{
int eid;
String name;
long phoneno ;
public Employee(int x, String y, long z) {
eid =x;
name = y;
phoneno=z;
}
public Employee(int a, String b) {
eid = a;
name = b;
}
void show()
{
System.out.println (eid+" "+name+" "+phoneno);
}
public static void main(String args[])
{
Employee e1 = new Employee(123,"Rahul");
Employee e2 = new Employee(231," Shikha ", 3789329);
e1.show ();
e2.show ();
}
}

Hence the constructor overloading and constructor chaining are the two different concepts. The above mentioned example will help you to understand them in better way.


Related Topics

Java For Loop

A for loop is used to execute a set of statements for a fixed number of times. It takes the following form: for (initialization; condition; update) { statements; } The for loop defines...

2 minutes read.

Selection Sort in Java

Selection Sort in Java Selection sort is also a simple sorting algorithm that works by repeatedly finding the minimum element from the unsorted portion of the input array, and placing it...

5 minutes read.

All the important string methods in Java

What is a String? Strings are a bundle of different characters that are normally used in Java programming language. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.

Determine the Upper Bound of a Two-Dimensional Array in Java

Multi-Dimensional Array Multi-faceted exhibits in Java are regular and can be named various clusters. Information in a two-layered bunch in Java is put away in 2D even structure. A two-layered collection...

3 minutes read.

Dutch National Flag Problem in Java

Dutch National Flag (DNF) is a programming issue that Edsger Dijkstra put up. The white, red, and blue hues make up the Dutch flag. The goal is to haphazardly set...

6 minutes read.

Java vs Go

Java: Java is an object oriented programming language. It is also known as multi threaded language. It was designed by James gosling in the year 1995. We can also say that...

4 minutes read.

Java Throw and Throws Keyword

Exceptions in Java enable us to construct high-quality programs where faults are checked at compile time rather than run time and where we may define unique exceptions that make code...

6 minutes read.

Creation of Multi Thread in java

What are threads in Java? We can use threads to facilitate parallel processing. Threads are helpful when you wish to execute several pieces of code concurrently. A thread is a small process...

3 minutes read.

Java Transient Keyword

An object in Java can be turned into a stream of bytes using serialization. The data of the instance and the kind of data saved in that instance are both...

3 minutes read.

Java file Reader

File Reader: It is used to read the data from files. This class inherits the properties from Input Stream Reader Class. File Reader is for reading characters from the file. Input Stream: Java.io...

4 minutes read.

Undo and Redo Operations in Java

Undo and redo operation are the most widely used operation while dealing with file. In this section, we will discuss how to implement undo and redo operation in Java. Undo Redo...

2 minutes read.

How to Convert char to int in Java

How to Convert char to int in Java When two variables of different types are involved in the single expression, Java compiler uses built-in library function to convert the variable to...

3 minutes read.

Java Math with Methods and Examples

Java Math class contains various methods for performing math operations like min(), max(), avg() and various trigonometric functions like sin(), cos(), tan() etc. Methods: The java.lang.Math class contains various methods for performing...

5 minutes read.

MessageDigest in Java

The compression of mathematical numbers is accomplished using hash functions. In almost every data security application, hash functions are employed. The hashing, often called has values, returns a value called...

3 minutes read.

Java Extends keyword

Extends Extends is a keyword which is completely depended on the concept of the inheritance of the java programming language.To understand about of the keyword, we need to learn the concept...

3 minutes read.

Awesome explanation of Strings in Java

What do you mean by String? Strings are a collection of characters that are commonly used in Java programming. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.

Java InputStreamReader

What is InputStreamReader?An InputStreamReader is a converter between byte and character streams: It reads bytes and converts them to characters with the help of a charset. The charset it uses can...

4 minutes read.

Multithreading in Java

Multithreading is a specialized form of multitasking. It is responsible for executing more than one task at a time of a single program, and each task is a separate thread. A program...

8 minutes read.

Camel Case in Java

Java names its classes, interfaces, methods, as well as variables using camel-case syntax. If the name consists of two words, the second word will always begin with a capital letter,...

3 minutes read.

Difference between String and Char Array in Java

We are heading to examine some significant differences between String and Character arrays. Both char arrays and String hold the series of characters and are utilised as a cluster of...

3 minutes read.