×

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

Generics vs Wildcard in Java

In generic programming, the question mark (?) is often referred to as the wildcard. It stands for a mysterious type. The wildcard can be used for many different contexts, such as...

4 minutes read.

Intersection Point of two linked list in Java

In this article, you will be very well acknowledged about how to achieve the intersection point of two linked list in Java. There are several approaches to obtain. Surely each...

8 minutes read.

What is advance Java?

The definition of advance inside the dictionary refers to a forward motion, development, or progress, and the definition of enhancing is something that improves things. To become experts in that...

3 minutes read.

Hollow Diamond Pattern in Java

Why are patterns important? Programmers frequently create Java pattern programs to practice coding and ace interviews. Interviewers frequently test candidates' logical reasoning and implementation by asking about pattern programs. Hollow Diamond Pattern The...

7 minutes read.

Java 9 Try With Resources

Java 9 provides the improvement in the try statement. It allows us to declare a try statement with duly declared resources. Whenever the user does not require the functionality with...

3 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.

Sierpinski Number in Java

The Sierpinski triangle—is it a fractal? The Sierpinski Triangle fractals. A self-similar fractal is the Sierpinski triangle. It is made of an equilateral triangle with its residual area successively reduced by...

3 minutes read.

How to Convert String to long in Java

How to Convert String to long in java It is used when you want to perform the mathematical operation on the String which contains long number then the conversion from String...

4 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 vs JavaScript

Java Vs JavaScript Java and JavaScript, both play an important role in the field of Computer Technology. Most people think JavaScript is a part of Java. But it is not entirely...

4 minutes read.

Java IO file not found exception

One of the exception classes offered by the java.io package is FileNotFoundException. An exception is raised when we attempt to access a file that isn't present in the system. It...

4 minutes read.

How to Call a Method in Java

In Java, a method is a collection of statements that perform a specific task or action.It can accept data with the help ofitsarguments. It  is also called a function. In order...

9 minutes read.

Java Math floor() Method

The floor() method of Math class returns the largest double value that is equal to a mathematical integer and is less than or equal to the argument. Syntax: public static double floor(double...

2 minutes read.

Binary Search Java

Binary search is a search mechanism for key elements from the given List/Array. In Binary search, the search mechanism is followed by dividing the array into parts; hence the search...

3 minutes read.

Creating a Jar file in Java

The JDK's jar (Java Archive) tool offers the ability to produce jar files that can be executed. If you double-click a jar file that is executable, it will call the...

2 minutes read.

Finding middle node of a linked list in Java

To find the middle node of a linked list we have various methods in Java. Method 1 In this method two pointers are used, one of which advances quickly, and the other of...

6 minutes read.

What are Array strings in Java?

In normal programming, An array is a group and a collection of identical forms of data that are stored in a sequential memory region and may be accessed using their...

4 minutes read.

Difference Between Java and PHP

The two most used programming languages are PHP and Java. Both of them have a lot of similarities and distinctions. Let's first grasp each of them individually before examining their...

3 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.

Reverse a String in Java

Reversing a string means that if we have a string called “what is your name”, the reversed format is “eman ruoy si tahw”. Reversing a string involves totally flipping the...

3 minutes read.