0

I've looked up the "interface" function several places, but nowhere does it seem to actually explain the benefits of using it, or why I should use interfaces when writing my own future programs.

I finished the exercise below, where I used an interface named "Form" to describe the methods "circumference" and "area". Then I have 3 classes "Circle", "Rectangle" and "Square" where the variables from each form are input and calculated to finally retrieve the circumference and area of each form.

My problem is that after I finished the exercise, I'm struggling to really see the point of implementing this "Form" interface. I feel like I could have just ignored using an interface and then simply through inheritance, make each of the classes inherit the circumference and area methods and then just create objects for each of the forms at the end when compiling?

How did the interface make things easier for me?

public class FormCompiling {
    public static void main(String[] args) {
        Form[] f = {new Circle(1.5), new Rectangle(2.0,3.0), new Square(2.5)};
        System.out.println("Area:");
        for(int i = 0 ; i<f.length; i++) {
            System.out.println(f[i].area());
        }
    }
}

public interface Form {
    public double circumference();
    public double area();
}

public class Circle implements Form {
    double radius = 0;
    double area = 0;
    double circumference = 0;

    Circle(double radius) {
        this.radius = radius;
    }

    @Override
    public double circumference() {
        circumference = 2 * radius * Math.PI;
        return circumference;
    }

    @Override
    public double area() {
        area = radius * radius * Math.PI;
        return area;
    }
}

public class Rectangle implements Form {
    double length = 0;
    double width = 0;
    double area = 0;
    double circumference = 0;

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

    @Override
    public double circumference() {
        circumference = (2 * length) + (2 * width);
        return circumference;
    }

    @Override
    public double area() {
        area = length * width;
        return area;
    }
}

public class Square extends Rectangle implements Form {

    Square(double length) {
        super(length, length);
        this.length = length;
    }

    @Override
    public double circumference() {
        circumference = 4 * length;
        return circumference;
    }

    @Override
    public double area() {
        area = length * length;
        return area;
    }
}
WoeIs
  • 1,083
  • 1
  • 15
  • 25
  • 1
    Write a method that computes the sum of the areas of many forms. What type would you use for the argument of such a method? What would you use if Square, Rectangle and Circle did not implement the Form interface? If you introduce a new Triangle class, will you have to change anything to this method in the first case? And in the second case? – JB Nizet Nov 22 '18 at 19:23
  • Possible duplicates: https://stackoverflow.com/questions/2586389/when-should-i-use-an-interface-in-java https://stackoverflow.com/questions/4052621/the-purpose-of-interfaces-continued https://stackoverflow.com/questions/20463/what-is-the-point-of-interfaces-in-php/24436493#24436493 (PHP, but same applies to java) – omajid Nov 22 '18 at 19:43

2 Answers2

0

Yes, you could have a parent class with empty methods (or some of them implemented).. and you'd get a similar result. However, you would not get compile errors if you forgot to implement some of the methods.

An interface "forces" you to follow some contract; a parent class does not. In fact, an interface is a contract. Whoever decides to implement it, has to implement the contract.

  • interface = the "what" = contract = specification
  • class = the "how" = implementation
Luís Soares
  • 5,726
  • 4
  • 39
  • 66
0

The biggest use case for interfaces in Java is to facilitate Polymorphism. Polymorphism means having a single class behave like multiple other classes. Multiple inheritance is not supported in Java so you can't extend more than one class, but you can implement multiple interfaces. There are many useful docs about this topic, here is an official one from oracle to get you started: https://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html

Interfaces are also useful as a "social contract" between a designer of a class/program and the developer who implements it. By implementing an interface you promise that your implementation follows specific behaviors. This allows for consistency amongst different implementations, which is especially useful when trying to collaborate with many developers on a single, large project.

NMerkl
  • 41
  • 4