I created an interface 'Polygon' that stores the abstract methods: 'area' and 'perimeter'. However, I am not understanding how to effectively use the interface, when the classes that implement Polygon have different computations involved for calculating area and perimeter. In my opinion, I don't even need an interface 'Polygon' since it has no use in my code.
I've tried overriding the method 'area' in the Triangle class, but received the following error:
Triangle is not abstract and does not override abstract method area() in Polygon
since the Triangle area has constructors. I cannot modify the Polygon area method to have the same number of constructors needed for Triangle, because it will not then suit my Rectangle class.
public interface Polygon {
void area();
void perimeter();
}
class Triangle implements Polygon{
private double triangleArea;
private double trianglePerimeter;
public Triangle (){};
public void area(){}; //I've tried overriding method here but get a
compiler error since it is not identical to the Polygon method.
public double area(double base, double height){
triangleArea = base * height * (.5);
return triangleArea;
}
public class Project25 {
public static void main(String[] args) {
Triangle testTriangle = new Triangle();
testTriangle.area(2, 2);
testTriangle.printArea();
I've managed to obtain the answers I need in my code i.e. area and perimeter, but I need to know how to modify my code to utilize inheritance and polymorphism.