Java Program to Find Area of Square Using Method Overloading


We can calculate the area of the square in Java using Method Overloading. “Method Overloading” is a feature in Java that allows one to write more than one method in the same class using the same method name. It will enable us to declare more than one method with the same names but with different signatures i.e., the number of parameters in the method may be different or the datatype of parameters may be different. Method Overloading helps us to increase the readability of the code so that we can use the same method in different ways.

Now, let us achieve Method Overloading in Java by considering the “area of a square” as an example.

Area of Square

Area of a square is defined region occupied by it in a 2-d plane. We can find area of Square by performing product of side*side.

Area of Square =  s*s
where	 
s: side of square                              

In the below examples, we will achieve Method Overloading in Java using the area of a square as an example by changing the data types of parameters.

Algorithm

STEP 1 − Write a custom class to find the area of the square.

STEP 2 − Initialize a pair of two variables of different data types in the main method of the public class.

STEP 3 − Create an object of a custom class in the main method of the public class.

STEP 4 − Call the specific method to find the area of the square using the custom object created.

Example

In this example, we calculate the area of a square using a basic formula and implement method overloading in Java.

The method overloading is achieved by changing the types of parameters in “areaOfSquare” method. Now, when a user gives input as integer type parameter values to the areaOfSquare method then the first areaOfSquare method is called of the Area class and the output is printed. If the user gives double type input parameters, then the second areaofSquare method is called and executed.

//Java Code to achieve Method Overloading in Java by Area of Square.
import java.io.*;
class Area {
   // In this example area method is overloaded by changing the type of parameters.
   public void areaOfSquare(int side) {
      int area = 0;
      area = side * side;
      System.out.println("Area of the square is :" + area);
   }
   public void areaOfSquare(double side) {
      double area= 0;
      area = side*side;
      System.out.println("Area of the square is:" + area);
   }
}
public class Main {
   public static void main(String args[]) {
      Area Object  = new Area();
      int side_1= 3;
      Object.areaOfSquare(side_1);
      double side_2 = 4.5;
      Object.areaOfSquare(side_2);
   }
}

Output

Area of the square is :9
Area of the square is:20.25                             

Time Complexity: O(1) Auxiliary Space: O(1)

Example

In this example, we calculate the area of a square using a Math.pow() function and implement method overloading in Java.

The method overloading is achieved by changing the types of parameters in “areaOfSquare” method. Now, when a user gives input as integer type parameter values to the areaOfSquare method then the first areaOfSquare method is called of the Area class and the output is printed. If the user gives double type input parameters, then the second areaofSquare method is called and executed.

//Java Code to achieve Method Overloading in Java by Area of Square.
import java.io.*;
class Area {
   // In this example area method is overloaded by changing the type of parameters.
   public void areaOfSquare(int side) {
      int area = 0;
      area =(int) Math.pow(side,2);
      System.out.println("Area of the square is :" + area);
   }
   public void areaOfSquare(double side) {
      double area= 0;
      area = Math.pow(side,2);
      System.out.println("Area of the square is:" + area);
   }
}
public class Main {
   public static void main(String args[]) {
      Area Object  = new Area();
      int side_1= 3;
      Object.areaOfSquare(side_1);
      double side_2 = 4.5;
      Object.areaOfSquare(side_2);
   }
}

Output

Area of the square is :9
Area of the square is:20.25                            

Time Complexity: O(1) Auxiliary Space: O(1)

Thus, in this article, we have learned how to implement Method Overloading in Java by changing the datatype of parameters using the example of finding the area of a square.

Updated on: 11-Apr-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements