Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should we always include a default constructor in the class?

I have been asked this question by a colleague that should we always include a default constructor in a class? If so, why? If no, why not?

Example

public class Foo {      Foo() { }      Foo(int x, int y) {         ...     }   } 

I am also interested to get some lights on this from experts.

like image 656
Moon Avatar asked Sep 11 '10 18:09

Moon


People also ask

Should you always have a default constructor?

If your class is able to provide sane defaults for all fields that comprise a valid state for objects of that class, then a default constructor is most likely a good idea. Also, some libraries require the existence of a default constructor for certain operations.

Does a class always has a default constructor?

The compiler automatically provides a no-argument, default constructor for any class without constructors.

Why does a class need to have a default constructor?

What is the significance of the default constructor? They are used to create objects, which do not have any specific initial value. Is a default constructor automatically provided? If no constructors are explicitly declared in the class, a default constructor is provided automatically by the compiler.

What happens if you don't declare a default constructor?

By default, when constructor is not declared then default constructor plays role. Even if we declare or not, default constructor is called while program is executed. A default constructor will only be automatically generated by the compiler if no other constructors are defined. Regardless of any inheritance.


2 Answers

You have to keep in mind that if you don't provide an overloaded constructor, the compiler will generate a default constructor for you. That means, if you just have

public class Foo {  }  

The compiler will generate this as:

public class Foo {      public Foo() { }   }  

However, as soon as you add the other constructor

public class Foo {      public Foo(int x, int y)     {          // ...      }   }  

The compiler will no longer automatically generate the default constructor for you. If the class was already being used in other code which relied on the presence of a default constructor, Foo f = new Foo();, that code would now break.

If you don't want someone to be able to initialize the class without providing data you should create a default constructor which is private to be explicit about the fact that you are preventing instances from being constructed with no input data.

There are times, however, when it is necessary to provide a default constructor (whether public or private). As was previously mentioned, some types of serialization require a default constructor. There are also times when a class has multiple parameterized constructors but also requires "lower level" initialization, in which case a private default constructor can be used which is chained in from the parameterized constructors.

public class Foo {    private Foo()    {       // do some low level initialization here    }     public Foo(int x, int y)       : this()    {       // ...    }     public Foo(int x, int y, int z)       : this()    {       // ...    } } 
like image 125
Scott Dorman Avatar answered Oct 25 '22 07:10

Scott Dorman


Some things (like serialisation) require a default constructor. Outside of that, however, a default constructor should only be added if it makes sense.

For example, if the Foo.X and Foo.Y properties are immutable after construction then a default constructor doesn't really make sense. Even if it were to used for an 'empty' Foo, a static Empty accessor would be more discoverable.

like image 36
Richard Szalay Avatar answered Oct 25 '22 08:10

Richard Szalay