Commit cff1a59
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
1 file changed
+3
-6
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
3 | | - | |
| 2 | + | |
4 | 3 | | |
5 | 4 | | |
6 | | - | |
7 | | - | |
| 5 | + | |
8 | 6 | | |
9 | 7 | | |
10 | | - | |
11 | 8 | | |
12 | | - | |
| 9 | + | |
13 | 10 | | |
14 | 11 | | |
15 | 12 | | |
| |||
0 commit comments