简体   繁体   中英

I'm having difficulties understanding how the “interface” function is useful in Java?

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;
    }
}

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM