We can calculate the area of the circle 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 circle” as an example. Before moving to an example, now let us know the terminology used in this article.

## What is a Circle?

A “Circle” is a 2d-representation of a collection of points that are equidistant from a single particular point known as the “center”

## What is Radius of a Circle?

Radius of a circle is the distance between the centre point of circle and the collection of points that lie on circumference of the circle.

## What is Diameter of a Circle?

Diameter of a circle is twice distance between the centre point of circle and the collection of points that lie on circumference of the circle.

Diameter of a Circle : 2r
where


## Area of Circle

Area of Circle is the region covered by circle in a 2-d plane.

Area of Circle :    πr^2
where
π : A Greek mathematical Symbol = 3.14 or 22/7.


In the below example, we will achieve Method Overloading in Java using Area of Circle as an example by changing the data types of parameters.

### Algorithm

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

STEP 2 − Initialize 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 circle using the custom object created.

### Example 1

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

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

//Java Code to achieve Method Overloading in Java by Area of Circle
import java.io.*;
class Area {
// In this example area method is overloaded by changing the type of parameters.
double PI = Math.PI;
//Math.PI is a constant value in Java in the Math library.
double area = 0;
System.out.println("Area of the circle is :" + area);
}
public void areaOfCircle(float radius ) {
double area= 0;
System.out.println("Area of the circle is :" + area);
}
}
public class Main {
public static void main(String args[]) {
Area Object  = new Area();
}
}


### Output

Area of the circle is :153.93804002589985
Area of the circle is :38.48451000647496


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

Math.PI is a constant value in Java in Math library. It’s value in java is 3.141592653589793.

We can calculate the area of the circle using an alternative formula where we use diameter and implement method overloading in Java.

Deriving the alternative formula for the radius of Circle

Area of Circle = πr^2
Substituting ‘r=d/2’ value in the above equation.
Area of Circle = π〖(d/2)〗^2


Below is the implementation of Java Code using above formula

### Example 2: Using Diameter of Circle

In the below example, the method overloading is achieved by changing the types of parameters in “areaOfCircle” method. 14 is assigned to “diameter_1” variable which is of double type so the “areaofCircle” method with parameter type as double is executed. After that 7 is assigned to “diameter_2” variable which is of float type. So, when we call “areaOfCirlce function the function with float type parameter is executed.

//Java Code to achieve Method Overloading in Java by Area of Circle by alternative formula.
import java.io.*;
import java.util.*;
class Area {
// In this example area method is overloaded by changing the type of parameters.
double PI = Math.PI;
//Math.PI is a constant value in Java in Math library.
public void areaOfCircle(double diameter) {
double area = 0;
area = PI * (diameter/2)*(diameter/2);
System.out.println("Area of the circle is :" + area);
}
public void areaOfCircle(float diameter) {
double area= 0;
area = PI * (diameter/2)*(diameter/2);
System.out.println("Area of the circle is :" + area);
}
}
public class Main {
public static void main(String args[]) {
Area Object  = new Area();
double diameter_1 =  14;
float diameter_2 = 7;
Object.areaOfCircle(diameter_1);
Object.areaOfCircle(diameter_2);
}
}


### Output

Area of the circle is :153.93804002589985
Area of the circle is :38.48451000647496


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 Circle.

Updated on: 10-Apr-2023

596 Views 