OOPs
Java Program to Calculate Area Using Method Overloading
A Java program to calculate the area of different shapes using method overloading.
Problem Description
Write a Java program to calculate the area of a circle, rectangle, and triangle using method overloading.
Code
public class MethodOverloading {
public static void main(String[] args) {
MethodOverloading mo = new MethodOverloading();
System.out.println("Area of Circle: " + mo.area(5));
System.out.println("Area of Rectangle: " + mo.area(5, 10));
System.out.println("Area of Triangle: " + mo.area(10, 20.5));
}
// Area of Circle
public double area(double r) {
return 3.14 * r * r;
}
// Area of Rectangle
public int area(int l, int b) {
return l * b;
}
// Area of Triangle
public double area(double b, double h) {
return 0.5 * b * h;
}
}Output
Area of Circle: 78.5
Area of Rectangle: 50
Area of Triangle: 102.5Explanation
- Method Overloading: Multiple methods with the same name but different parameters (type or number).
- Compile-time Polymorphism: The compiler decides which method to call based on the arguments passed.
Java Program to Create Class and Objects with Methods
Learn object-oriented programming basics with this Java program that demonstrates class creation, object instantiation, and method calling with practical examples.
Java Program to Demonstrate Inheritance and Polymorphism
Comprehensive Java program demonstrating inheritance concepts including extends keyword, method overriding, and polymorphism with real-world examples.
