Tuesday, July 12, 2016

Constructor Overloading

The overload process can also be applied to the constructor of a class. This is because constructor also is a method that returns a class type (himself). A class can has more than one constructor. To demonstrate it, consider the following class definition:

class Box {
   double length;
   double width;
   double height

   //Constructor for class Box
   Box (double l, double w, double h) {
      length = l;
      width = w;
      height = h;
   }

   //Method for calculate box volume
   double hitungVolume() {
      return(length * width * height);
   }
}

You can see clearly that the constructor in the class Box above has three parameters. This means, every initialization of object Box, we also had to pass three arguments into it. Thus, we were not able to make the object box in the following manner:

Box k = new Box();

This is because the default constructor on the class Box is no longer valid, because it is replaced by the constructor we define above. To overcome this, we can do constructor overloading, as shown in the program code below.

class Box {
   double length;
   double width;
   double height;

   //Define constructor without parameter
   Box() {
      length = 0;
      width = 0; 
      height = 0;
   }

   //Define constructor with one parameter
   Box(double side) {
      length = side;
      width = side;
      height = side;
   }

   //Define constructor with three parameters
   Box(double l, double w, double h) {
      length = l;
      width = w;
      height = h;
   }

   double calculateVolume() {
      return(length * width * height);
   }
}

class DemoOverloadConstructor {

   public static void main(String[] args) {

      Box b1, b2, b3;

      b1 = new Box();
      b2 = new Box (10);
      b3 = new Box (4, 3, 2);

      //Display volume from each Box object
      System.out.println("Volume b1 = " b1.calculateVolume());
      System.out.println("Volume b2 = " b2.calculateVolume());
      System.out.println("Volume b3 = " b3.calculateVolume());
   }
}

The result will be given is as follows:

Volume k1 = 0.0
Volume k2 = 1000.0
Volume k3 = 24.0

It was clear in the above program that we define three constructors for class Box. First, with no parameter. Here, all data contained in the class Box will be initialized with a value of 0. Second, have one parameter, that is used to create cube object (cube is a geometry that has same length, width, and height). Third, has three parameters, are used to create box with different value of length, width, and height. Actually, we can create a cube object by using a third constructor, as long as we fill the same value to the existing three arguments.

In the above example, we pass the argument with a value of 10 in the second constructor. This causes the volume obtained from the object is 1000.0, which is derived from 10 x 10 x 10. For the third constructor, we pass arguments to the value of 4, 3 and 2 so that the volume obtained was 24.0.

2 comments:

  1. Thank You for Sharing valuable information with us.

    E-Commerce is the fastest-growing platform which is becoming more demandable day by day this is a tremendous form of the vibrant sector that is bringing several opportunities to the new generation to become more skilled and professional. Taking up this great advantage in the E-Commerce sector would need the right path and the right skillset you need to learn the latest technologies and high-demand skills such as building payment gateways and developing an accessible and easy website along with the basics of E-commerce that‘s why Aptech learning official Pro E-Commerce training program in Dwarka, Janakpuri, and Gurgaon which will help to make a successful career in this booming industry. Java Programming Course helps to become an expert in Java web application development. It helps you to bring efficiency in collecting and analyzing the data through Java programming in this course you will learn effective web UI/UX design and Secure web applications by using the advanced technologies of the Java program.

    For more details please visit our website : Aptech Dwarka

    ReplyDelete