Skip to content

Commit cff1a59

Browse files
committed
feat: Add MethodOverloadingForAreaCal to compute areas using overloaded methods
WHAT the code does: Defines a MethodOverloadingForAreaCal class with two overloaded static area methods: - area(double radius) computes the area of a circle using πr². - area(double length, double breadth) computes the area of a rectangle. main() demonstrates usage by calculating: - Circle area with radius 5 - Rectangle area with length 10 and breadth 4 WHY this matters: Shows practical application of method overloading in Java. Provides a clean API design: same method name "area" for different shapes, differentiated by parameter signature. Demonstrates how the compiler resolves the correct overload based on the number and type of arguments. Highlights reusability and readability benefits of overloading when modeling related operations. HOW it works: main() defines input values for circle and rectangle. Calls area(radius), which executes the single-argument overload and applies the circle formula. Calls area(length, breadth), which executes the two-argument overload and applies the rectangle formula. Results are printed with descriptive messages. Tips and gotchas: Overloading improves readability but should not be confused with overriding (runtime polymorphism). If inputs are ambiguous (e.g., int literals without suffix), compiler may choose a less obvious overload. Always prefer descriptive method signatures when overloaded methods could become confusing in larger APIs. For extensibility, consider using separate shape classes with an area() method to leverage polymorphism. Use-cases: Educational program for method overloading. Quick utility for computing areas of different shapes. Foundation for object-oriented refactoring into a Shape hierarchy. Short key: class-methodoverloadingforareacal overloaded-area circle-rectangle. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 10b384a commit cff1a59

File tree

1 file changed

+3
-6
lines changed

1 file changed

+3
-6
lines changed

Section10Methods/src/MethodOverloadingForAreaCal.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
public class MethodOverloadingForAreaCal {
2-
static double area(double radius)
3-
{
2+
static double area(double radius) {
43
return Math.PI*radius*radius;
54
}
6-
static double area(double length, double breadth)
7-
{
5+
static double area(double length, double breadth) {
86
return length*breadth;
97
}
10-
118
public static void main(String[] args) {
12-
// Calculate the area of a circle with radius 5
9+
// Calculate the area of a circle with radius 5.
1310
double radius = 5.0;
1411
double circleArea = area(radius);
1512
System.out.println("Area of the circle with radius " + radius + " is: " + circleArea);

0 commit comments

Comments
 (0)