简体   繁体   中英

Why does all the interface methods need to be implemented in a class implementing it in java

I know that it is the purpose of the interface and the class can be declared abstract to escape from it.

But is there any use for implementing all the methods that we declare in an interface? will that not increase the weight and complexity of the code if we keep on defining all the methods even it is not relevant for that class? why it is designed so?

The idea of an interface in Java is very much like a contract (and perhaps seen in retrospect this should have been the name of the concept)

The idea is that the class implementing the interface solemnly promises to provide all the things listed in the contract so that any use of a class implementing the interface is guaranteed to have that functionality available.

In my experience this facility is one of the things that makes it possible to build cathedrals in Java.

What you are critizing is exactly the goal interface achieve. If you don't want to implement an interface, don't declare your class implementing it.

will that not increase the weight and complexity of the code if we keep on defining all the methods even it is not relevant for that class?

When you program against an interface, you want the concrete object behind it to implement all its methods. If your concrete object doesn't need or cannot implement all interface method you probably have a design issue to fix.

When any piece of code receives an instance of an interface without knowing what class is behind it, that piece of code should be assured of the ability to call any method in an interface. This is what makes an interface a contract between the callers and the providers of the functionality. The only way to achieve that is to require all non-abstract classes implementing the interface to provide implementations for all its functions.

There are two general ways to deal with the need to not implement some of the functionality:

  • Adding a tester method and an implementation that throws UnsupportedOperationException , and
  • Splitting your interface as needed into parts so that all method of a part could be implemented.

Here is an example of the first approach:

public interface WithOptionalMehtods {
    void Optional1();
    void Optional2();
    boolean implementsOptional1();
    boolean implementsOptional2();
}

public class Impl implements WithOptionalMehtods {
    public void Optional1() {
        System.out.println("Optional1");
    }
    public void Optional2() {
        throw new UnsupportedOperationException();
    }
    public boolean implementsOptional1() {
        return true;
    }
    public boolean implementsOptional2() {
        return false;
    }
}

Here is an example of the second approach:

public interface Part1 {
    void Optional1();
}
public interface Part2 {
    void Optional2();
}
public Impl implements Part1 {
    public void Optional1() {
        System.out.println("Optional1");
    }
}

will that not increase the weight and complexity of the code if we keep on defining all the methods even it is not relevant for that class?

Yes you are right it will. That is why it is best practice in your coding to follow the Interface Segregation Principle which recommends not to force clients to implement interfaces that they don't use. So you should never have one "fat" interface with many methods but many small interfaces grouping methods , each group serving a specific behavior or sub-module.
This way clients of an interface implement only the needed methods without ever being forced into implementing methods they don't need.

It may depend on Liskov Substitution Principle

So, having A implements B means that you can use A when B is needed and, to make it work without problems, A must have at least the same methods of B .

Please keep in mind that mine is not a "proper" answer, as it's not based on official sources!

在实现接口时,我们可能不需要定义接口中声明的所有方法。我们可以定义一些我们不需要的方法,在体内没有任何内容。

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