Constructor Overloading Tutorial

One of the really cool features of Java is the ability to overload constructors. What is an overloaded constructor? Basically, an overloaded constructor is multiple constructors all with different signatures. The signature consists of the constructor name followed by the parameter list enclosed in parenthesis. In this tutorial I am going to add in a new instance variable to my Box class. This new instance variable will hold a String value that will specify the unit of measurement. This tutorial will build on the Box class from my Constructors Part 4 tutorial.



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md ConstructorOverloading
C:\Java>cd ConstructorOverloading
C:\Java\ConstructorOverloading>Notepad ConstructorOverloading.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.



class ConstructorOverloading {
    public static void main(String args[]) {
	
        Box b = new Box(); 
        b.setLength(10);
        b.setHeight(2);		
        b.setWidth(5);
        System.out.println("The volume of box b is: " + b.calculateVolume());

        Box c = new Box(4, 8, 3);
        System.out.println("The volume of box c is: " + c.calculateVolume());

        Box d = new Box(7, 5, 6, "inches");
        System.out.println("The volume of box d is: " + d.calculateVolume() + " cubic " + d.getUnitOfMeasurement());

    }
}

class Box {
    private int length = 0; 
    private int height = 0; 
    private int width = 0;
    private String unitOfMeasurement; 

    // Default constructor - Don't forget to always include this.
    Box() {
        super(); 
    }

    // First Constructor created
    Box(int lengthParam, int heightParam, int widthParam) {
        length = lengthParam;
        height = heightParam;
        width = widthParam;
    } 
    
    // New Constructor
    Box(int lengthParam, int heightParam, int widthParam, String unitOfMeasurementParam ) {
        length = lengthParam;
        height = heightParam;
        width = widthParam;
        unitOfMeasurement = unitOfMeasurementParam;
    }      

    void setLength (int lengthParam) {
        length = lengthParam;
    }
    int getLength () {
        return length;
    }

    void setHeight (int heightParam) {
        height = heightParam;
    }
    int getHeight () {
        return height;
    }

    void setWidth (int widthParam) {
        width = widthParam;
    }
    int getWidth () {
        return width;
    }

    void setUnitOfMeasurement (String unitOfMeasurementParam) {
        unitOfMeasurement = unitOfMeasurementParam;
    }
    String getUnitOfMeasurement () {
        return unitOfMeasurement;
    }

    // create a method to get the volume of the box
    int calculateVolume() {
       return (length * height * width);	
    }
}

Now switch back to the command prompt (CMD) and type in javac ConstructorOverloading.java and press Enter.
Now type in java ConstructorOverloading and press Enter.


C:\Java\ConstructorOverloading>javac ConstructorOverloading.java
C:\Java\ConstructorOverloading>java ConstructorOverloading
The volume of box b is: 100
The volume of box c is: 96
The volume of box d is: 210 cubic inches


Final thoughts

Let's consider what we would have to do if Java did not allow constructor overloading. We couldn't simply modify the existing class because that would 'break' any code that was using the Box object already. We would have to create an entirely new class with a different name that would contain 80% duplicate code. The new class would have the constructor with the length, width, and height; plus the new String unitOfMeasurement parameter. Now we would have two classes that performed almost identical operations - that is not good. When I create a new class, I fully realize that the class will most likely evolve over time. The ability to overload a constructor allows us to seamlessly expand our classes without breaking code that is already in use.


Tutorials