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 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
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.
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.
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); } }
Area of the rectangle is :12 Area of the rectangle is:24.75
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!