Java program to find area of rectangle using method overloading
We can use method overloading to 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
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); } }
Output
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

What happens when you turn off Find My on iPhone? Find My iPhone helps you locate a lost or stolen device. When enabled, Find My iPhone lets you track your device's location on a map, plays sounds, and helps you find your device. Find My also includes an Activation Lock to prevent anyone from using your iPhone. When you turn off Find My iPhone, you lose all these features, which may make recovering a lost Apple device difficult. While Find My iPhone is very useful, you should disable it when you want to sell, donate, trade in your phone, or send it for battery replacement or any other service. Doing this will ensure that no one can access information about you

Use the Array.IndexOf function in C# to find the index of an element in an array. In a C# program, when we need to find the index of an element in an array, we can use the Array.IndexOf function. The Array.IndexOf function finds the specified element within the specified array range and returns the index of its first occurrence. If the element is not found, -1 is returned. The following is a sample code that demonstrates how to use the Array.IndexOf function to find an element in an array.

Hard drive serial numbers and MAC addresses are important identifiers in computer hardware and are very useful in managing and maintaining computer systems. This article will introduce how to find the hard disk serial number and MAC address. 1. Find the hard drive serial number. The hard drive serial number is a unique identifier used by the hard drive manufacturer to identify and track the hard drive. In different operating systems, the method of finding the hard drive serial number is slightly different. Windows: Open Command Prompt (search for "cmd" in the Start menu) and enter the following command and press Enter: wmicdisk

Apple's Find My app allows you to locate your iPhone or other device to prevent it from being lost or forgotten. While Find My is a useful tool for tracking devices, you may want to disable it if you're concerned about privacy issues, don't want to drain your battery, or for other reasons. Fortunately, there are several ways to turn off Find My on iPhone, all of which we will explain in this article. How to Turn off Find My on iPhone [4 Methods] You can turn off Find My on iPhone in four ways. If you used Method 1 to turn off Find, you can do this from the device you want to disable it on. To proceed with methods 2, 3, and 4, the iPhone that you want to turn off Find Finder should be powered off or

The Go language does not support method overloading because its design philosophy emphasizes simplicity, concurrency, and type safety. Method overloading can introduce name conflicts, complex type systems, and code confusion. To compensate for this, the Go language provides functions that allow the creation of functions with the same name but different parameter types in the same package, similar to the functionality of method overloading.

The glob() function in PHP is used to find files or directories and is a powerful file operation function. It can return the path of a file or directory based on a specified pattern match. The syntax of the glob() function is as follows: glob(pattern, flags) where pattern represents the pattern string to be matched, which can be a wildcard expression, such as *.txt (matching files ending with .txt), or a specific file path. flags is an optional parameter used to control the function

How to Check the Serial Number of a Computer Hard Drive With the development of computer technology, computer hard drives have become an indispensable part of our lives. Whether it is storing important files or installing operating systems and software, you need to rely on the hard disk to complete it. Understanding some basic information about the computer hard drive, such as the hard drive's serial number, can help us better manage and maintain the computer system. So, how to check the serial number of a computer hard drive? This article will introduce several common methods. Method 1: Use the command line tool that comes with Windows system Windows system

In this problem, we are given an array aar[] containing n unsorted integer values and an integer val. Our task is to find the start and end index of an element in an unsorted array. For occurrences of an element in the array, we will return, "start index and end index" (if found twice or more in the array). "Single index" (if found) "Element does not exist" if not present in the array. Let us take an example to understand the problem, Example 1Input:arr[]={2,1,5,4,6,2,3},val=2Output:startingindex=0,endingindex=5 explains that element 2 appears twice, The first time appears at index = 0, the second
