Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constructor overloading in Java - best practice

There are a few topics similar to this, but I couldn't find one with a sufficient answer.

I would like to know what is the best practice for constructor overloading in Java. I already have my own thoughts on the subject, but I'd like to hear more advice.

I'm referring to both constructor overloading in a simple class and constructor overloading while inheriting an already overloaded class (meaning the base class has overloaded constructors).

Thanks :)

like image 832
Eyal Roth Avatar asked Jul 25 '09 14:07

Eyal Roth


People also ask

What are the rules for constructor overloading?

Constructor overloading in Java refers to the use of more than one constructor in an instance class. However, each overloaded constructor must have different signatures. For the compilation to be successful, each constructor must contain a different list of arguments.

How do you overload a constructor in Java?

In such cases, you can use the keyword “super” to call overridden constructors of the parent class. super(); --or-- super(parameter list); Example: If your constructor is like Demo(String Name,int a), you will specify super(“Java”,5).

Which of the following defines the best use of constructor overloading?

Which among the following best describes constructor overloading? Clarification: If more than one constructors are defined in a class with same signature, then that results in error. The signatures must be different. So that the constructors can be differentiated.

Why do we need constructor overloading in Java?

If we want to have different ways of initializing an object using different number of parameters, then we must do constructor overloading as we do method overloading when we want different definitions of a method based on different parameters.


1 Answers

While there are no "official guidelines" I follow the principle of KISS and DRY. Make the overloaded constructors as simple as possible, and the simplest way is that they only call this(...). That way you only need to check and handle the parameters once and only once.

public class Simple {      public Simple() {         this(null);     }      public Simple(Resource r) {         this(r, null);     }      public Simple(Resource r1, Resource r2) {         // Guard statements, initialize resources or throw exceptions if         // the resources are wrong         if (r1 == null) {             r1 = new Resource();         }         if (r2 == null) {             r2 = new Resource();         }          // do whatever with resources     }  } 

From a unit testing standpoint, it'll become easy to test the class since you can put in the resources into it. If the class has many resources (or collaborators as some OO-geeks call it), consider one of these two things:

Make a parameter class

public class SimpleParams {     Resource r1;     Resource r2;     // Imagine there are setters and getters here but I'm too lazy      // to write it out. you can make it the parameter class      // "immutable" if you don't have setters and only set the      // resources through the SimpleParams constructor } 

The constructor in Simple only either needs to split the SimpleParams parameter:

public Simple(SimpleParams params) {     this(params.getR1(), params.getR2()); } 

…or make SimpleParams an attribute:

public Simple(Resource r1, Resource r2) {     this(new SimpleParams(r1, r2)); }  public Simple(SimpleParams params) {     this.params = params; } 

Make a factory class

Make a factory class that initializes the resources for you, which is favorable if initializing the resources is a bit difficult:

public interface ResourceFactory {     public Resource createR1();     public Resource createR2(); } 

The constructor is then done in the same manner as with the parameter class:

public Simple(ResourceFactory factory) {     this(factory.createR1(), factory.createR2()); }  

Make a combination of both

Yeah... you can mix and match both ways depending on what is easier for you at the time. Parameter classes and simple factory classes are pretty much the same thing considering the Simple class that they're used the same way.

like image 113
Spoike Avatar answered Sep 29 '22 07:09

Spoike