Lably
Skip to content

Constructor (object-oriented programming)

Special function called to create an object

  • 43 min read
  • 4 chapters

Learn it on Lably

A constructor resembles an instance method, but it differs from a method in that it has no explicit return type, it is not implicitly inherited and it usually has different rules for scope modifiers. Constructors often have the same name as the declaring class. They have the task of initializing the object's data members and of establishing the invariant of the class, failing if the invariant is invalid. A properly written constructor leaves the resulting object in a valid state. Immutable objects must be initialized in a constructor.

Most languages allow overloading the constructor in that there can be more than one constructor for a class, with differing parameters. Some languages take consideration of some special types of constructors. Constructors, which concretely use a single class to create objects and return a new instance of the class, are abstracted by factories, which also create objects but can do so in various ways, using multiple classes or different allocation schemes such as an object pool.

Types

Parameterized constructors

Constructors that can take at least one argument are termed as parameterized constructors. When an object is declared in a parameterized constructor, the initial values have to be passed as arguments to the constructor function. The normal way of object declaration may not work. The constructors can be called explicitly or implicitly. The method of calling the constructor implicitly is also called the shorthand method.

<span id="mwIw"></span><span class="k" id="mwJA">class</span><span class="w" id="mwJQ"> </span><span class="nc" id="mwJg">Point</span><span class="w" id="mwJw"> </span><span class="p" id="mwKA">{</span> <span class="k" id="mwKQ">private</span><span class="o" id="mwKg">:</span> <span class="w" id="mwKw"> </span><span class="kt" id="mwLA">int</span><span class="w" id="mwLQ"> </span><span class="n" id="mwLg">x</span><span class="p" id="mwLw">;</span> <span class="w" id="mwMA"> </span><span class="kt" id="mwMQ">int</span><span class="w" id="mwMg"> </span><span class="n" id="mwMw">y</span><span class="p" id="mwNA">;</span> <span class="k" id="mwNQ">public</span><span class="o" id="mwNg">:</span> <span class="w" id="mwNw"> </span><span class="n" id="mwOA">Point</span><span class="p" id="mwOQ">()</span><span class="w" id="mwOg"> </span><span class="o" id="mwOw">=</span><span class="w" id="mwPA"> </span><span class="k" id="mwPQ">default</span><span class="p" id="mwPg">;</span> <span class="w" id="mwPw"> </span><span class="n" id="mwQA">Point</span><span class="p" id="mwQQ">(</span><span class="kt" id="mwQg">int</span><span class="w" id="mwQw"> </span><span class="n" id="mwRA">x</span><span class="p" id="mwRQ">,</span><span class="w" id="mwRg"> </span><span class="kt" id="mwRw">int</span><span class="w" id="mwSA"> </span><span class="n" id="mwSQ">y</span><span class="p" id="mwSg">)</span><span class="o" id="mwSw">:</span> <span class="w" id="mwTA"> </span><span class="n" id="mwTQ">x</span><span class="p" id="mwTg">{</span><span class="n" id="mwTw">x</span><span class="p" id="mwUA">},</span><span class="w" id="mwUQ"> </span><span class="n" id="mwUg">y</span><span class="p" id="mwUw">{</span><span class="n" id="mwVA">y</span><span class="p" id="mwVQ">}</span><span class="w" id="mwVg"> </span><span class="p" id="mwVw">{}</span><span class="w" id="mwWA"> </span><span class="c1" id="mwWQ">// Parameterized constructor</span> <span class="p" id="mwWg">};</span>
<span id="mwXQ"></span><span class="n" id="mwXg">Point</span><span class="w" id="mwXw"> </span><span class="n" id="mwYA">p</span><span class="w" id="mwYQ"> </span><span class="o" id="mwYg">=</span><span class="w" id="mwYw"> </span><span class="n" id="mwZA">Point</span><span class="p" id="mwZQ">(</span><span class="mi" id="mwZg">0</span><span class="p" id="mwZw">,</span><span class="w" id="mwaA"> </span><span class="mi" id="mwaQ">50</span><span class="p" id="mwag">);</span><span class="w" id="mwaw"> </span><span class="c1" id="mwbA">// Explicit call.</span> <span class="n" id="mwbQ">Point</span><span class="w" id="mwbg"> </span><span class="nf" id="mwbw">p2</span><span class="p" id="mwcA">(</span><span class="mi" id="mwcQ">0</span><span class="p" id="mwcg">,</span><span class="w" id="mwcw"> </span><span class="mi" id="mwdA">50</span><span class="p" id="mwdQ">);</span><span class="w" id="mwdg"> </span><span class="c1" id="mwdw">// Implicit call.</span>

Default constructors

If the programmer does not supply a constructor for an instantiable class, Java compiler inserts a default constructor into the code. This constructor is known as default constructor. It would not be found in source code (the .java file) as it would be inserted into the code during compilation and exists in .class file. The behavior of the default constructor is language dependent. It may initialize data members to zero or other same values, or it may do nothing at all. In Java, a "default constructor" refer to a nullary constructor that is automatically generated by the compiler if no constructors have been defined for the class or in the absence of any programmer-defined constructors (e.g. in Java, the default constructor implicitly calls the superclass's nullary constructor, then executes an empty body). All fields are left at their initial value of 0 (integer types), 0.0 (floating-point types), false (boolean type), or null (reference types), etc.

<span id="mwiQ"></span><span class="k" id="mwig">class</span><span class="w" id="mwiw"> </span><span class="nc" id="mwjA">Point</span><span class="w" id="mwjQ"> </span><span class="p" id="mwjg">{</span> <span class="k" id="mwjw">private</span><span class="o" id="mwkA">:</span> <span class="w" id="mwkQ"> </span><span class="kt" id="mwkg">int</span><span class="w" id="mwkw"> </span><span class="n" id="mwlA">x</span><span class="p" id="mwlQ">;</span> <span class="w" id="mwlg"> </span><span class="kt" id="mwlw">int</span><span class="w" id="mwmA"> </span><span class="n" id="mwmQ">y</span><span class="p" id="mwmg">;</span> <span class="k" id="mwmw">public</span><span class="o" id="mwnA">:</span> <span class="w" id="mwnQ"> </span><span class="n" id="mwng">Point</span><span class="p" id="mwnw">(</span><span class="kt" id="mwoA">int</span><span class="w" id="mwoQ"> </span><span class="n" id="mwog">x</span><span class="w" id="mwow"> </span><span class="o" id="mwpA">=</span><span class="w" id="mwpQ"> </span><span class="mi" id="mwpg">0</span><span class="p" id="mwpw">,</span><span class="w" id="mwqA"> </span><span class="kt" id="mwqQ">int</span><span class="w" id="mwqg"> </span><span class="n" id="mwqw">y</span><span class="w" id="mwrA"> </span><span class="o" id="mwrQ">=</span><span class="w" id="mwrg"> </span><span class="mi" id="mwrw">0</span><span class="p" id="mwsA">);</span><span class="w" id="mwsQ"> </span><span class="c1" id="mwsg">// Default constructor.</span> <span class="p" id="mwsw">};</span>

Copy constructors

Like C++, Java also supports "Copy Constructors". But, unlike C++, Java does not create a default copy constructor even if no such copy constructor is specified. Copy constructors define the actions performed by the compiler when copying class objects. A Copy constructor has one formal parameter that is the type of the class (the parameter may be a reference to an object). It is used to create a copy of an existing object of the same class. Even though both classes are the same, it counts as a conversion constructor. While copy constructors are usually abbreviated copy ctor or cctor, they have nothing to do with class constructors used in .NET using the same abbreviation.

Conversion constructors

Conversion constructors provide a means for a compiler to implicitly create an object belonging to one class based on an object of a different type. These constructors are usually invoked implicitly to convert arguments or operands to an appropriate type, but they may also be called explicitly.

Move constructors

In C++, move constructors take an Rvalue reference to an object of the class, and are used to implement ownership transfer of the parameter object's resources.

Sign in to keep reading

3 more chapters are free with a Lably account.

More in Computing and communication