Constructor (ctor)

alpharithms fallback 1

Constructors are subroutines that are called during the creation of a new class-based object in object-oriented-programming. These sub-routines are responsible for defining the newly-instantiated object with state.

Constructors may accept arguments, have no return type, and are not implicitly inherited. Constructors initialize an object’s data members with values calculated from arguments, by using defaults, or by other programmer-defined routines.

Constructors are often abbreviated as ctor, particularly in abstract model representations like UML diagrams.

Below is an example of a constructor for an Example class in Java:

public class Example {
    
    // Data members
    private int number;
    
    // Constructor
    public Example(int number){
        this.number = number;
    }
    
}

Constructor Overloading

Constructors can, in most languages, be overloaded such that multiple combinations of arguments can be used to initialize a new class object on instantiation. This allows programmers to set defaults for cases where inputs may not be necessary. Below is an example of constructor overloading in Java:

public class Example {

    // Data members
    private int number;

    // Constructor with argument
    public Example(int number){
        this.number = number;
    }

    // Constructor without argument
    public Example(){
        this.number = 1234;
    }

}

In the second example, another constructor form has been added that allows the creation of an Example class object without providing an argument for number. In this example, the code specifies a default value of 1234 that will be set to the newly-created class object’s number field.

Constructor Chaining

Constructor chaining is an approach of programming constructor methods that can be used during inheritance or during constructor overloading. It provides a means for programmers to avoid redundancy in setting class data members and blends seamlessly with overloading.

public class Example {

    // Data members
    private int number;
    private String name;

    // Default Constructor
    public Example(int number){
        this.number = number;
        this.name = "Example";
    }

    // Chaining Constructor
    public Example(){
        this(1234);
    }
}

In the example above, a name field has been added to the class and is hard-coded. The number value can still be set dynamically during the instantiation of a new class object. The other constructor method—the one requiring no arguments—instantiates a new class object by chaining with the default constructor. In this context, constructor chaining means calling one constructor method from within another.

The benefit here is that the Chaining Constructor method does not have to bother setting the default value for the name field—it lets the default constructor handle that. This helps avoid having to code this class as follows:

public class Example {

    // Data members
    private int number;
    private String name;

    // Constructor
    public Example(int number){
        this.number = number;
        this.name = "Example";
    }

    // Constructor without argument
    public Example(){
        this.number = 1234;
        this.name = "Example";  // redundant
    }

}

The reduction of redundancy and lines of code isn’t great in this contrived example. However, more complex application design can benefit greatly by using constructor chaining, particularly in cases of inheritance and polymorphism.

Default Constructors

Default constructors are those that can be called without any arguments. Most languages have a system by which default constructor behavior is defined. For example, in Java, classes that do not explicitly define a constructor have a default constructor generated at compile time by referencing a class’s superclass. In such case where a user-defined superclass, or an extended Java class, does not explicitly define a default constructor, the Java compiler will continue to traverse the hierarchical tree—all the way to the default Object model.

Zαck West
Full-Stack Software Engineer with 10+ years of experience. Expertise in developing distributed systems, implementing object-oriented models with a focus on semantic clarity, driving development with TDD, enhancing interfaces through thoughtful visual design, and developing deep learning agents.