0

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.

Kayvan Tehrani
  • 3,070
  • 2
  • 32
  • 46
CSrook
  • 15
  • 1
  • 4
  • 1
    Shouldn't `void area()` return the area? And why does `Triangle` not have a constructor with parameters `base` and `height`? –  Sep 01 '19 at 05:27
  • 1
    You should also apply `@Override` to the methods where you attempt to override methods to ensure that you have the match you expect. (Your comment explaining the cause of the error is not correct.) – chrylis -cautiouslyoptimistic- Sep 01 '19 at 05:30
  • I did not add a constructor with parameters since the line 'testTriangle.area(2, 2);' in the main method sets the base and height. – CSrook Sep 01 '19 at 05:33
  • That's bad usage of methods. What is the area of a `Triangle` if you never call `area(double double)`? –  Sep 01 '19 at 05:34
  • https://www.guru99.com/java-class-inheritance.html . You have think of it like everything that the father has the children will have. If you know all shapes have an area method. You code that the father that all the children will have it by default, and specific methods that are only for that child are code to the inheritor. If that makes sense. – LatheElHafy Sep 01 '19 at 05:45
  • [This might help](https://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface) – Sweeper Sep 01 '19 at 06:09

3 Answers3

0

The purpose of an interface is to be an abstract view of the common features of the objects.

In case of polygons, you've already identified some things they have on common, i.e. all polygons have an area, and they all have a perimeter.

The abstract view would be to get those common values:

public interface Polygon {
    double getArea();
    double getPerimeter();
}

Of course, you can only get those values if the polygon is fully defined, e.g. for a triangle the base and the height might be enough to calculate the area, but not the perimeter. Instead, you usually use the lengths of the 3 sides.

public class Triangle implements Polygon {
    private final double a;
    private final double b;
    private final double c;

    public Triangle(double a, double b, double c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }

    @Override
    public double getArea() {
        // Using Heron's Formula
        double p = (a + b + c) / 2;
        return Math.sqrt(p * (p - a) * (p - b) * (p - c));
    }

    @Override
    public double getPerimeter() {
        return a + b + c;
    }
}

A rectangle is even easier.

public class Rectangle implements Polygon {
    private final double width;
    private final double height;

    public Rectangle(double width, double height) {
        this.width = width;
        this.height = height;
    }

    @Override
    public double getArea() {
        return width * height;
    }

    @Override
    public double getPerimeter() {
        return (width + height) * 2;
    }
}

As you can see, since the inputs for calculating area are different, depending on the type of polygon, the area method cannot take a common set of parameters, therefore they must be embedded in the polygon object, so we can get a common area method.

Andreas
  • 154,647
  • 11
  • 152
  • 247
  • Hello Andreas, can you explain why we should implement an interface at all since we can just code the methods in each class? I see no use for the interface; however, my project required we create one. Regards. – CSrook Sep 01 '19 at 20:07
  • @JavaMan Interface allows code to treat polygon the same regardless of their difference. If you need to print the area of a polygon, you only need to write the code once, regardless of the polygon shape. If you didn't have an interface, you'd need a copy of that code for every shape. – Andreas Sep 01 '19 at 20:26
  • @JavaMan [Why do we need interfaces in Java?](https://stackoverflow.com/q/3528420/5221149) – Andreas Sep 01 '19 at 20:26
  • @JavaMan [When should I use an interface in java?](https://stackoverflow.com/q/2586389/5221149) – Andreas Sep 01 '19 at 20:26
  • @JavaMan [Why interface really needed when normal class can do the same work](https://stackoverflow.com/q/23646745/5221149) – Andreas Sep 01 '19 at 20:27
  • @JavaMan Find more using web search, e.g. [`java why interface`](https://www.google.com/search?q=java+why+interface) – Andreas Sep 01 '19 at 20:28
0

I think,

  1. Methods in Polygon interface should return values (area and perimeter).
  2. Each shape class that implements Polygon should have its own fields and appropriate constructor to initialize them. (length and height for example), which will be used for implementation of area and perimeter.

So,

  1. Class Triangle should have a constructor,

    public Triangle (final Double base, final Double height) {
       this.base = base;
       this.height = height;
    };
    
  2. And your area method should be like,

     @Override
     public Double area() {
      return base * height * (0.5d);
     }
    
  3. And you should use it something like below,

    final Polygon polygon = new Triangle(13, 212);
    final Double area = polygon.area();
    final Double perimeter = polygon.perimeter();
    
Amit Patil
  • 710
  • 9
  • 14
0

The first problem I see is that you didn't declare the perimeter method in the class that implements the interface, so that would cause a compiler error right there. As to what others said, you're returning a value on both of those methods, so they shouldn't be void methods.

Also, why did you use an overloaded method in the Triangle class?

Nathan777
  • 79
  • 8