Answer:
The java program for the given scenario is as follows.
import java.util.*;
//interface with method area
interface ObjectWithTwoParameters
{
double area (double d1, double d2);
}
class RectangleClass implements ObjectWithTwoParameters
{
//overriding area()
public double area (double d1, double d2)
{
return d1*d2;
}
}
class TriangleClass implements ObjectWithTwoParameters
{
//overriding area()
public double area (double d1, double d2)
{
return (d1*d2)/2;
}
}
class CylinderClass implements ObjectWithTwoParameters
{
public double area (double d1, double d2)
{
return ( 2*3.14*d1*d1 + d2*(2*3.14*d1) );
}
}
public class Test
{
public static void main(String[] args)
{
//area displayed for all three shapes
ObjectWithTwoParameters r = new RectangleClass();
double arear = r.area(2, 3);
System.out.println("Area of rectangle: "+arear);
ObjectWithTwoParameters t = new TriangleClass();
double areat = t.area(4,5);
System.out.println("Area of triangle: "+areat);
ObjectWithTwoParameters c = new CylinderClass();
double areac = c.area(6,7);
System.out.println("Area of cylinder: "+areac);
}
}
OUTPUT
Area of rectangle: 6.0
Area of triangle: 10.0
Area of cylinder: 489.84
Explanation:
1. The program fulfils all the mentioned requirements.
2. The program contains one interface, ObjectWithTwoParameters, three classes which implement that interface, RectangleClass, TriangleClass and CylinderClass, and one demo class, Test, containing the main method.
3. The method in the interface has no access specifier.
4. The overridden methods in the three classes have public access specifier.
5. No additional variables have been declared.
6. The test class having the main() method is declared public.
7. The area of the rectangle, triangle and the cylinder have been computed as per the respective formulae.
8. The interface is similar to a class which can have only declarations of both, variables and methods. No method can be defined inside an interface.
9. The other classes use the methods of the interface by implementing the interface using the keyword, implements.
10. The object is created using the name of the interface as shown.
ObjectWithTwoParameters r = new RectangleClass();