0

Im a new programmer and I want to know when its best practice to use overloaded constructors and what makes it different from single primary constructor.

3 Answers3

3

The short answer is: you should use overloading whenever you need it.

As a real-life example, take a look at the JLabel API: https://docs.oracle.com/javase/8/docs/api/javax/swing/JLabel.html

JLabel has quite a few constructors: one that just takes a String, one that takes a String and an icon, one that only takes an icon, and one that doesn't take any arguments at all.

You would use each constructor when you want to construct that kind of JLabel: one that displays a String, one that displays a String and an icon, one that only displays an icon, or one that doesn't display anything yet (until you call one of its setter functions).

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
0

Constructor overloading is useful when you want to allow user to create objects in multiple different ways.For example to be able to create a simple Student class object in following different ways:

  new Student(45); // given student id
  new Student("name"); // given student name
  new Student(45,"name"); // given student registration id and name

This helps ease the task of creating objects according to our requirements. You can link this concept with various java API's as the provide a no of different ways to initialize an object of a class.

Also you can combine the Construstor Overloading with Constructor chaining. Here is an examlple:

 public Student (int id){
     this(id,"ANY-DEFAULT-NAME"); // calls the constructor of same class with 2 params
 }

 public Student (String name){
     this(ANY-DEFAULT-ID,name);// calls the constructor of same class with 2 params
 } 

 public Student (int id,String name){
     // here you can initialize the instance variables of the class.
 } 
varun
  • 1,473
  • 1
  • 9
  • 15
0

You can overload a constructor based in your needs. For example,let's say that you have a simple class called Dog, that have some attributes like: Name,Breed, Birthday, Owner and skin color.

public class Dog {

  private String name;
  private String breed;
  private Date birthday;
  private String owner;
  private String skinColor;

  /*Getters and Setters*/
  ...
}

If you instance a object of type Dog and want to set a all or some of the attributes' values, you'll have to call all the setters methods of the object, but with the constructor, you can save that step passing the values directly every moment you instance the object.

Example:

public class Dog {

  private String name;
  private String breed;
  private Date birthday;
  private String owner;
  private String skinColor;

  public Dog(String name, String breed,Date birthday,String owner,String skinColor){
    this.name = name;
    this.breed = breed;
    this.birthday = birthday;
    this.owner = owner;
    this.skinColor = skinColor;
  }

  /*Getters and Setters*/
  ...
 }



Dog myDog = new Dog("Jose", "Chiguagua",new Date(),"Jhon","Brown");

If you want only instance the object with the name only, you can do it too. A good practice is, if you have an object with attributes that is necessary to fill in some point, provide the default constructor, if you do not provide it, you will always need to pass some values for instance a object. This give flexibility to the programmer.

mgilsn
  • 145
  • 6