Java/J2EE and SOA (348 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Comprehensive Java Course (4 Blogs)
  • Java/J2EE and SOA (345 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

How To Best Implement Constructor Overloading In Java?

Last updated on Sep 10,2019 4K Views


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

  • Recursive constructor calling is invalid in java
  • If we have defined any parameterized constructor, then compiler will not create default constructor. and vice versa if we don’t define any constructor, the compiler creates the default constructor(also known as no-arg constructor) by default during compilation
  • Constructor calling must be the first statement of constructor in Java

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 27th April,2024

27th April

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

Class Starts on 18th May,2024

18th May

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

How To Best Implement Constructor Overloading In Java?

edureka.co