java 抽象类可以/可以代替接口吗?

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

Do/can abstract classes replace interfaces?

javaoopinterfaceabstract-class

提问by Registered User

In Java, you can create an abstract class that contains only abstract methods. On the other hand, you can create an interface that declares the same methods. That being the case, can you use abstract classes instead of interfaces?

在 Java 中,您可以创建一个仅包含抽象方法的抽象类。另一方面,您可以创建一个声明相同方法的接口。既然如此,能不能用抽象类代替接口呢?

回答by Bozho

Not always:

不总是:

  • a class can extend only oneclass
  • a class can implement more than oneinterface
  • 一个类只能扩展一个
  • 一个类可以实现多个接口

Sun docsmake a more detailed comparison:

Sun docs做了更详细的比较:

Abstract Classes versus Interfaces

Unlike interfaces, abstract classes can contain fields that are not static and final, and they can contain implemented methods. Such abstract classes are similar to interfaces, except that they provide a partial implementation, leaving it to subclasses to complete the implementation. If an abstract class contains only abstract method declarations, it should be declared as an interface instead.

Multiple interfaces can be implemented by classes anywhere in the class hierarchy, whether or not they are related to one another in any way. Think of Comparable or Cloneable, for example.

By comparison, abstract classes are most commonly subclassed to share pieces of implementation. A single abstract class is subclassed by similar classes that have a lot in common (the implemented parts of the abstract class), but also have some differences (the abstract methods).

抽象类与接口

与接口不同,抽象类可以包含非静态和最终的字段,并且它们可以包含已实现的方法。这种抽象类类似于接口,只是它们提供了部分实现,将它留给子类来完成实现。如果抽象类只包含抽象方法声明,则应将其声明为接口。

多个接口可以由类层次结构中任何位置的类实现,无论它们是否以任何方式相互关联。例如,想想 Comparable 或 Cloneable。

相比之下,抽象类最常被子类化以共享实现部分。单个抽象类由具有很多共同点(抽象类的实现部分)但也有一些差异(抽象方法)的相似类进行子类化。

回答by Paul Wagland

In some cases you can use an abstract class instead of interface. However, it is hardly ever a good idea to do so. In general you should use the rule:

在某些情况下,您可以使用抽象类而不是接口。但是,这样做几乎不是一个好主意。一般来说,您应该使用以下规则:

  1. Interfaces specify behaviour.
  2. Abstract classes specify implementation.
  1. 接口指定行为。
  2. 抽象类指定实现。

The other "problem" with using abstract classes is that you can then no longer implement mixins, that is you can implement multiple interfaces, however you can only extend one abstract class.

使用抽象类的另一个“问题”是你不能再实现混合,也就是说你可以实现多个接口,但是你只能扩展一个抽象类。

回答by seh

One point missing from the answers here is the idea of whowill be implementing the interface.

这里的答案中缺少的一点是谁将实现接口的想法。

If your component wants to returninstances of abstract types to its callers, where the concrete types are defined internallyand hidden from callers, use an interface. Conversely, if your component consumesor acceptsinstances of abstract types that its callersmust implement, abstract classesare usuallya better choice.

如果您的组件想要将抽象类型的实例返回给它的调用者,其中具体类型在内部定义并对调用者隐藏,请使用interface。相反,如果你的组件消耗接受抽象类型的实例,它的调用者必须实现的抽象类通常是更好的选择。

Anticipating evolution and maintaining binary compatibility tips the scales here. With an abstract class, you can add methods and, if you provide a base implementation, existingimplementations of the abstract class will continue to work fine. With an interface, adding a method breaks binary compatibility, for no existing implementation could possibly continue to compile properly without changing to define the new method.

预测演变和维护二进制兼容性提示了这里的规模。使用抽象类,您可以添加方法,如果您提供基本实现,则抽象类的现有实现将继续正常工作。对于接口,添加方法会破坏二进制兼容性,因为现有实现不可能在不更改以定义新方法的情况下继续正确编译。

The Apache Cactus projecthas a good discussionon how to resolve these obligations.

Apache的仙人掌项目具有良好的讨论,就如何解决这些义务。

回答by Adamski

To answer your question, yes you could use an abstract class (providing no implementation) instead of an interface but I'd consider this bad practice:

要回答您的问题,是的,您可以使用抽象类(不提供实现)而不是接口,但我认为这是一种不好的做法:

  • You've used up your "one-shot" at inheritance (without gaining any benefit).
  • You cannot inherit from multiple abstract classes but you can implement multiple interfaces.
  • 你已经用完了你在继承上的“一次性”(没有获得任何好处)。
  • 您不能从多个抽象类继承,但可以实现多个接口。

I would advocate the use of abstract classes more in situations where you wish to provide a partial implementation of a class, possibly delegating some behavior to concrete subclass implementations.

我会提倡在您希望提供类的部分实现的情况下更多地使用抽象类,可能会将某些行为委托给具体的子类实现。

回答by ddccffvv

  • A class in java can inherit from multiple interfaces, but only from one abstract class.

  • An interface cannot define any code, in an abstract class, you can define code (i.e. default behaviour of methods)

  • java中的一个类可以从多个接口继承,但只能从一个抽象类继承。

  • 一个接口不能定义任何代码,在一个抽象类中,你可以定义代码(即方法的默认行为)

回答by Guillaume

Abstract classes and interfaces are complementary.

抽象类和接口是互补的。

For instance when creating an API you will want to present interfaces to the client, so that you may always completely change the implementation whereas he does not have to change its code and the user does not rely on implementation when building using your API but just on methods contracts.

例如,在创建 API 时,您将希望向客户端呈现接口,以便您可以始终完全更改实现,而他不必更改其代码,并且用户在使用您的 API 构建时不依赖于实现,而只是依赖于实现方法合同。

Then you will have abstract classes partly implementing these interfaces, in order to

然后你将有抽象类部分实现这些接口,以便

  • share some common code, which might be used in all (or almost all) implementations for interface, which is obvious
  • provide default behaviour which could be overridden in 'real' implementations, for instance a toString() method using interfaces methods to create a textual representation of the implementation
  • preserve implementations compatibility after interface changes, for instance when you add a new method in your interface, you also add a default implementation in the abstract class so that implementations (for instance those made by the user) extending the abstract class still work without changes
  • 共享一些通用代码,这些代码可能会在所有(或几乎所有)接口实现中使用,这是显而易见的
  • 提供可以在“真实”实现中覆盖的默认行为,例如使用接口方法创建实现的文本表示的 toString() 方法
  • 在接口更改后保持实现兼容性,例如,当您在接口中添加新方法时,您还会在抽象类中添加一个默认实现,以便扩展抽象类的实现(例如用户创建的实现)仍然可以正常工作而无需更改

回答by fastcodejava

Interfaces are much cleaner and light weight. Abstract classes make you dependent on it heavily as you cannot extend any other classes.

接口更干净,重量更轻。抽象类使您严重依赖它,因为您无法扩展任何其他类。

回答by bertolami

Have a look at the interesting article "Why extends is evil" to get an idea about the differences between interface implementation and class inheritance (beside the obvious multi- single restrictions)

看看有趣的文章“为什么扩展是邪恶的”,以了解接口实现和类继承之间的区别(除了明显的多单限制)

回答by Himanshu Mohta

Abstract classes are the partial implementation of Abstraction while Interfaces are the fully implementation of Abstraction.Means in Abstract classes we can put methods declaration as well as method body. We can't create an object of Abstract classes(association) and reuse the class by inheritence(not by association). By default in interfaces all declared variables are static final and All methods are public.

抽象类是 Abstraction 的部分实现,而接口是 Abstraction 的完全实现。意思是在抽象类中,我们可以放置方法声明以及方法体。我们不能创建抽象类(关联)的对象并通过继承(而不是通过关联)重用该类。默认情况下,接口中所有声明的变量都是静态最终的,所有方法都是公共的。

For Example:In JDK there are only few abstract classes and HttpServlet is one of them which is used in Servlet.So we can't create object of HttpServlet and it can be used only by inheritence.

例如:在JDK中只有很少的抽象类,HttpServlet就是其中之一,在Servlet中使用。所以我们不能创建HttpServlet的对象,它只能通过继承使用。

回答by Darshan Patel

Main use of interface is when you create the reference of interface and call the method of particular class that's resolved at runtime. So it's always better idea to create reference of interface to call the method.

接口的主要用途是当您创建接口的引用并调用在运行时解析的特定类的方法时。所以创建接口的引用来调用方法总是更好的主意。