java 接口优于抽象类的情况

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2971265/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 23:45:47  来源:igfitidea点击:

situation where interface is better than abstract class

javaoop

提问by JavaUser

Please tell me situation where interface is better than abstract class in Java

请告诉我接口比 Java 中的抽象类更好的情况

回答by Sujee

I think you have misunderstood the real meaning of interface and abstract class.

我想你误解了接口和抽象类的真正含义。

Interface is programming structure where you define your functions/services that you want to expose to public or other modules. Kind of a contract where you promise that you are providing some functionalities or services, but hiding the implementation so that implementation can be changed without affecting your contract.

接口是一种编程结构,您可以在其中定义要公开给公共或其他模块的功能/服务。一种合同,您承诺提供一些功能或服务,但隐藏实现,以便可以在不影响合同的情况下更改实现。

Abstract class is a partially implemented class and it has no real meaning other than serving as a parent for multiple child classes those with real meaning. Abstract class is special parent class that provides default functionalities to multiple child classes. it is created as abstract because of unavailability of suitable concrete parent class.

抽象类是一个部分实现的类,除了作为多个具有实际意义的子类的父类之外,它没有任何实际意义。抽象类是特殊的父类,为多个子类提供默认功能。由于没有合适的具体父类,它被创建为抽象类。

In a good design, you should always create an interface. But abstract class is optional. If you can not find a concrete parent class, create an abstract class and implement the interface then provide default implementations for those interface functions (if possible) otherwise mark them as abstract functions and leave the implementation to the child classes.

在一个好的设计中,你应该总是创建一个界面。但是抽象类是可选的。如果找不到具体的父类,请创建一个抽象类并实现接口,然后为这些接口函数提供默认实现(如果可能),否则将它们标记为抽象函数并将实现留给子类。

回答by Artefacto

  • A class can implement multiple interfaces, but it can only extend one abstract class.
  • Interfaces allow the creation of proxies that encapsulate a concrete class. This is used extensively by frameworks in order to intercept method calls to the concrete class (e.g., for starting a transaction before the method is executed or to write to the log).
  • 一个类可以实现多个接口,但只能扩展一个抽象类。
  • 接口允许创建封装具体类的代理。这被框架广泛使用,以拦截对具体类的方法调用(例如,用于在执行方法之前启动事务或写入日志)。

回答by dbyrne

Use an interface when you don't need to provide any default implementations.

当您不需要提供任何默认实现时,请使用接口。

Here is a nice link comparing the two: Interface vs. Abstract Class

这是比较两者的一个很好的链接:接口与抽象类

回答by Greg Hewgill

You can only have one direct abstract superclass. Therefore, interfaces are useful if you need to expose two or moreinterfaces.

您只能有一个直接抽象超类。因此,如果您需要公开两个或多个接口,则接口很有用。

回答by Amadan

Java doesn't have multiple inheritance; thus, you cannot have a class that implements two abstract classes at once. For instance, if you want a MouseListenerand an ActionListenerin a same class, you have to do it the interface way.

Java 没有多重继承;因此,您不能有一个同时实现两个抽象类的类。例如,如果您希望 aMouseListener和 anActionListener在同一个类中,则必须以接口方式进行。

回答by Stefan Ernst

In order to provide WebServices or do JMock tests you dont need the actual implementation, you just need an interface definition. Think about it, there is no need to return the implementation to a third party when all they need to do is call your API.

为了提供 Web 服务或进行 JMock 测试,您不需要实际的实现,您只需要一个接口定义。想想看,当他们需要做的只是调用您的 API 时,没有必要将实现返回给第三方。

回答by Govind Malviya

When you need to implements in any class because interface can be implements in any class or interface but abstract class cant do that e.g. in Applet always Applet class extend in our Applet Application in this case we cant extend the abstract class.

当您需要在任何类中实现时,因为接口可以在任何类或接口中实现,但抽象类不能这样做,例如在 Applet 中总是 Applet 类在我们的 Applet 应用程序中扩展,在这种情况下我们不能扩展抽象类。

回答by AbdullahC

An interface is better than a abstract class when you want multiple classes to implement that interface and when you don't have to inherit default behavior.

当您希望多个类实现该接口并且不必继承默认行为时,接口比抽象类更好。