How To Best Implement Constructor Overloading In Java?

Last updated on Sep 10,2019 4K Views

How To Best Implement Constructor Overloading In Java?

edureka.co

Advent of Java took the programming world by storm and the major reason for that is the number features it brought along. In this article we would be discussing Constructor Overloading in Java. Following pointers will be discussed in this article,

So let us get started then,

Constructor Overloading in Java

What is a Constructor?

A constructor is a block of code used to create object of a class.  Every class has a constructor, be it normal class or abstract class. A constructor is just like a method but without return type. When there is no any constructor defined for a class, a default constructor is created by compiler.

Example

public class Student{
//no constructor
private String name;
private int age;
private String std;
// getters and setters
public void display(){
System.out.println(this.getName() + " " + this.getAge() + " " + this.getStd());
}
public static void main(String args[]){
// to use display method of Student class, create object of Student
Student student = new Student(); // as we have not defined any constructor, compiler creates default constructor. so that
student.display();
}
}

In above program, default constructor is created by compiler so that object is created. It is a must to have constructor.

This brings us to the next it of this article on Constructor overloading In Java.

Need for other Constructors 

In above example Student object can be created with default constructor only. Where all other attributes of student is not initialized. But there can be certain other constructors, which is used to initialize the state of an object. for e.g –

public class Student{
//attributes
String name;
int age;
String std;
//Constructors
public Student(String name){ // Constructor 1
this.name = name;
}
public Student(String name, String std){ // Constructor 2
this.name = name;
this.std = std;
}
public Student(String name, String std, int age){ // Constructor 3
this.name = name;
this.std = std;
this.age = age;
}
public void display(){
System.out.println(this.getName() + " " + this.getAge() + " " + this.getStd());
}
public static void main(String args[]){
Student stu1 = new Student("ABC");
stu1.display();
Student stu2 = new Student("DEF", "5-C");
stu2.display();
Student stu3 = new Student("GHI", "6-C", 12);
stu3.display();
}
}

This brings us to the next it of this article on Constructor overloading In Java.

this() reference

this() reference can be used inside parameterized constructor to call default constructor implicitily. Please note, this() should be the first statement inside a constructor.

Example

public Student(){} // Constructor 4
public Student(String name, String std, int age){ // Constructor 3
this(); // will call the default constructor. *If it is not the first statement of constructor, ERROR will occur*
this.name = name;
this.std = std;
this.age = age;

Note

Thus we have come to an end of this article on ‘Constructor overloading in Java’. If you wish to learn more, check out the Java Training by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this blog  and we will get back to you as soon as possible.

Upcoming Batches For Java Certification Training Course
Course NameDateDetails
Java Certification Training Course

Class Starts on 4th May,2024

4th May

SAT&SUN (Weekend Batch)
View Details
Java Certification Training Course

Class Starts on 25th May,2024

25th May

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES
REGISTER FOR FREE WEBINAR UiPath Selectors Tutorial