java 为什么所有的接口方法都需要在java中实现的类中实现
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12121714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does all the interface methods need to be implemented in a class implementing it in java
提问by Deepak
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?
但是实现我们在接口中声明的所有方法有什么用吗?如果我们继续定义所有方法,即使它与该类无关,这不会增加代码的重量和复杂性吗?为什么是这样设计的?
回答by Thorbj?rn Ravn Andersen
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)
Java 中的接口的想法非常像合同(也许回想起来,这应该是概念的名称)
The idea is that the class implementingthe interface solemnly promisesto provide all the things listed in the contract so that any useof a class implementing the interface is guaranteedto have that functionality available.
这个想法是实现接口的类郑重承诺提供合同中列出的所有内容,以便保证实现接口的类的任何使用都具有可用的功能。
In my experience this facility is one of the things that makes it possible to build cathedrals in Java.
根据我的经验,这个设施是使在 Java 中建造大教堂成为可能的因素之一。
回答by Heisenbug
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.
当你针对一个接口编程时,你希望它背后的具体对象实现它的所有方法。如果您的具体对象不需要或无法实现所有接口方法,您可能需要解决设计问题。
回答by dasblinkenlight
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.
- 添加一个测试方法和一个抛出的实现
UnsupportedOperationException
,以及 - 根据需要将您的界面拆分为多个部分,以便可以实现一个部分的所有方法。
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");
}
}
回答by Cratylus
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 practicein your coding to follow the Interface Segregation Principlewhich recommends not to force clients to implement interfacesthat they don't use. So you should never have one "fat" interface with many methods but many smallinterfaces 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.
是的,你是对的。这就是为什么在您的编码中遵循接口隔离原则是最佳实践的原因,该原则建议不要强制客户端实现他们不使用的接口。所以你永远不应该有一个带有许多方法的“胖”接口,而是许多小接口分组方法,每个组服务于特定的行为或子模块。
通过这种方式,接口的客户端只实现所需的方法,而不会被迫实现不需要的方法。
回答by Arjun
When implementing an Interface,we may not need to define all the method declared in the Interface.We can define the some methods,that we don't need,With nothing inside the body.
在实现一个接口时,我们可能不需要定义接口中声明的所有方法。我们可以定义一些我们不需要的方法,在体内没有任何东西。
回答by StepTNT
It may depend on Liskov Substitution Principle
这可能取决于Liskov 替换原则
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
.
因此,拥有A implements B
意味着您可以A
在B
需要时使用,并且要使其正常工作,A
必须至少具有与B
.
Please keep in mind that mine is not a "proper" answer, as it's not based on official sources!
请记住,我的不是“正确”的答案,因为它不是基于官方来源!