At EasyTechJunkie, we're committed to delivering accurate, trustworthy information. Our expert-authored content is rigorously fact-checked and sourced from credible authorities. Discover how we uphold the highest standards in providing you with reliable knowledge.

Learn more...

What Is a Default Constructor?

Jessica Susan Reuter
Jessica Susan Reuter

A default constructor is a specialized method that creates and initializes an object in object-oriented programming. There are multiple kinds of constructors, but a default constructor must meet one of two specific criteria, in addition to creating and initializing an object. It must either have no parameters, in which case it could also be called a nullary constructor, or it must assign default values to all parameters it has the ability to set. Some computer languages implicitly specify a default nullary constructor if the programmer doesn't specify a constructor at all. Once any type of constructor is written, the default nullary constructor is no longer specified, and must be explicitly written if needed.

For computer languages that allow object-oriented inheritance, default constructors in subclasses must be precisely connected to constructors in superclasses. Subclass constructors always must call superclass constructors before doing anything specific to the subclass. In some languages, like Java®, a subclass constructor implicitly calls a superclass default nullary constructor. If a superclass default nullary constructor doesn't exist, the subclass object can't be built properly. Subclasses can still have either type of default constructor in this case, but an available superclass constructor must be called correctly, perhaps by passing it default values unique to the subclass.

Man holding computer
Man holding computer

The implicit generation of a default nullary constructor has the advantage of reducing the number of methods a programmer must specify and document, but also has the disadvantage of possibly making code maintenance and comprehension more difficult. Some programmers consider the omission of a default constructor to be bad practice, while others do not. There is nothing that prevents an individual programmer from adhering to either of these programming methods, so including a default nullary constructor in code is largely a stylistic choice on the part of the programmer. If the constructor is included, most schools of thought dictate that it should be commented and documented just as any other constructor or method would be.

Any default constructor can have access modifiers attached to it, making the constructor public, protected, or private. This gives the constructor specific abilities based on the class in which it is specified. A public default constructor can be used to create a new object from another class, while a protected constructor can only be called from its own class and any subclasses. Protected default constructors are seen often in an inheritance hierarchy. Private constructors are usually employed only in self-contained classes, where some type of factory method is available to call it, rather than the programmer.

Discuss this Article

Post your comments
Forgot password?
    • Man holding computer
      Man holding computer