Home> Java> javaTutorial> body text

Java program to find area of rectangle using method overloading

王林
Release: 2023-08-19 20:45:17
forward
1186 people have browsed it

Java program to find area of ​​rectangle using method overloading

We can usemethod overloadingto calculate the area of a rectangle in Java. "Method overloading" is a feature in Java that allows multiple methods to be written in the same class with the same method name. This will allow us to declare multiple methods with the same name but different signatures, i.e. the number of parameters in the methods may be different or the data types of the parameters may be different. Method overloading helps us increase the readability of our 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 rectangle”as an example.

Area of rectangle

Area of a rectangle is defined region occupied by ait in a 2-d plane. We can find the area of rectangle by performing the product of length and breadth of rectangle.

Area of Rectangle = lb where l: length of rectangle. b: breadth of rectangle
Copy after login

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

Algorithm

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

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 rectangle using the custom object created.

Example

In this example, we use a basic formula to calculate the area of a rectangle and implement method overloading in Java.

Method overloading is achieved by changing the type of parameters in the "areaOfRectangle" method. Now, when the user passes a parameter value of integer type as input to the areaOfRectangle method, the first areaOfRectangle method of the Area class is called and outputs the result. If the user enters a parameter of double type, the second areaOfRectangle method is called and executed.

//Java Code to achieve Method Overloading in Java by Area of Rectangle. import java.io.*; class Area { // In this example area method is overloaded by changing the type of parameters. public void areaOfRectangle(int length, int breadth) { int area = 0; area = length *breadth; System.out.println("Area of the rectangle is :" + area); } public void areaOfRectangle(double length, double breadth) { double area= 0; area = length *breadth; System.out.println("Area of the rectangle is:" + area); } } public class Main { public static void main(String args[]) { Area Object = new Area(); int length_1 = 3; int breadth_1 = 4; Object.areaOfRectangle(length_1, breadth_1); double length_2 = 4.5; double breadth_2 = 5.5; Object.areaOfRectangle(length_2, breadth_2); } }
Copy after login

Output

Area of the rectangle is :12 Area of the rectangle is:24.75
Copy after login

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

The above is the detailed content of Java program to find area of rectangle using method overloading. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!