Top News

Why do we need default constructor in Java classes?

 

Default constructor is the no-argument constructor that is
automatically generated by Java if no other constructor is defined.


Java specification says that it will provide a default constructor if
there is no overloaded constructor in a class. But it does not say
anything about the scenario in which we write an overloaded
constructor in a class.


We need at least one constructor to create an object, that’s why Java
provides a default constructor.


When we have overloaded constructor, then Java assumes that we
want some custom treatment in our code. Due to which it does not
provide default constructor. But it needs default constructor as per
the specification. So it gives error

Post a Comment

Previous Post Next Post