Category: 

What Is a Default Constructor?

Article Details
  • Written By: Jessica Susan Reuter
  • Edited By: Shereen Skola
  • Last Modified Date: 04 September 2016
  • Copyright Protected:
    2003-2016
    Conjecture Corporation
  • Print this Article
Free Widgets for your Site/Blog
Although Stonehenge is the most famous, there are over 1,000 ancient stone circles standing in the British Isles.   more...

September 26 ,  1960 :  The first televised US Presidential debate took place.  more...

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.

Ad

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.

Ad

You might also Like

Recommended

Discuss this Article

Post your comments

Post Anonymously

Login

username
password
forgot password?

Register

username
password
confirm
email