
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Area of Circle Using Method Overloading in Java
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.
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.
Area of Circle
Area of a 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. r : radius of circle.
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.
Steps to Find Area of circle Using Method Overloading
Below are the steps to calculate the area of circle using method overloading -
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.
Approaches
Below are the approaches to calculate the area of circle
Java Program to Calculate Circle Area with Method Overloading Based on Radius Type
In this example, we calculate the area of a circle using a basic formula and implement method overloading in Java.
Example
//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. public void areaOfCircle(double radius) { double area = 0; area = PI * radius * radius; System.out.println("Area of the circle is :" + area); } public void areaOfCircle(float radius ) { double area= 0; area = PI * radius * radius; System.out.println("Area of the circle is :" + area); } } public class Main { public static void main(String args[]) { Area Object = new Area(); float radius_1 = 7; Object.areaOfCircle(radius_1); double radius_2 = 3.5; Object.areaOfCircle(radius_2); } }
Output
Area of the circle is :153.93804002589985 Area of the circle is :38.48451000647496
Time Complexity: O(1)
Auxiliary Space: O(1)
Java Program to Calculate Circle Area Using Diameter with Method Overloading
We can calculate the area of the circle using an alternative formula where we use diameter and implement method overloading in Java.
Example
//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.