Calculate the area of Rectangle Using Constructor Overloading rules

Example of Constructor Overloading in Java program

This is a java program to calculate the area of Triangle.
This program is helpful to understand the constructor overloading. Here many constructors are called with the same name as same as of the class name 'Rectangle'.

public class Rectangle
{
    //Declaring global variables
    int length;
    int breadth;
    double area;
 

Declaring global variables is accessible from wherever you want. Here many constructors are called and they need to be stored in the same variable so the global variables are created. The variables 'length', 'breadth', and 'area' stores the length breadth and the area of the rectangle respectively.

   
 public Rectangle() //Non-Parameterised Constructor
    {
         this.length=4;
         this.breadth=2;
    }

The constructors with no any parameters are called Non-Parameterised Constructor. Because of no parameters, the variables needed to perform calculation are initialized and stored some values.
Keyword "this" is a reference variable in Java that refers to the current object.

The features of 'this' keywords are:-
  • It helps to pass an argument in the method call.
  • It can be passed as an argument in the constructor call.
  • It helps to return the current class instance variables.
  • It can be used to initialize the variable inside the respected Constructor or Method.
  • 'this' keyword can be used to get the handle of the current class.


public Rectangle(int length, int breadth) //Parameterised constructor having multiple arguments
    {
         this.length=length;
         this.breadth=breadth;
    }

The constructors with parameter(s) are called Parameterised Constructor. Here two parameters 'int length' and 'int breadth' are passed which accept the values as an argument when the constructor is called from any other method(function).


 public Rectangle(int length) //Parameterised constructor having single arguments
    {
         this.length=length; 
    }

Here only one parameter is passed which accepts the value as an argument when the constructor is called from any other method(function). The same named constructor 'Rectangle' inside the same class 'Rectangle' is defined with the different number of parameters and is all about Constructor Overloading.


public static void main(String []args)
    {
         // 1. Creating obj1 and calling non-paramaterised Constructor
         Rectangle obj1= new Rectangle(); 
         obj1.area= obj1.length*obj1.breadth; //Processing
         // 2. Creating obj2 and calling parameterised Constructor having multiple arguments
         Rectangle obj2= new Rectangle(4, 6); 
         obj2.area= obj2.length*obj2.breadth; //Processing
         //3. Creating obj3 and calling parameterised Constructor
         Rectangle obj3= new Rectangle(5); 
         obj3.area= obj3.length*obj3.length; //Processing
         //Printing Result
         System.out.println("Area of triangle  "+obj1.area); //1
         System.out.println("Area of triangle  "+obj2.area); //2
         System.out.println("Area of triangle  "+obj3.area); //3
    }
}

Output:

       Area of triangle  8.0
       Area of triangle  24.0


       Area of triangle  25.0

Comments

Popular posts from this blog

Java Program to make a comparison between numbers

Java program using Scanner Class

Java program to print 'Hello World'