Java中的抽象类与接口[英] Abstract class vs Interface in Java

本文是小编为大家收集整理的关于Java中的抽象类与接口的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我被问到一个问题,我想在这里对答案进行评论.

Q:在哪种情况下,更合适地扩展抽象类而不是实现接口?

?

a:如果我们使用模板方法设计模式.

我正确吗?

我很抱歉,如果我无法清楚地陈述这个问题.
我知道抽象类和接口之间的基本区别.

1)当要求我们需要在每个子类中实现相同的功能以进行特定操作(实现方法)和其他某些其他操作(仅方法签名)

时,请使用抽象类.

2)如果需要将签名放置为相同(并且实现不同),请使用接口,以便可以遵守接口实现

3)我们可以扩展一个抽象类的最大值,但可以实现多个接口

重申以下问题:除了上面提到的情况外,还有其他方案,具体我们需要使用抽象类(一个人是一个是模板方法设计模式,仅基于此概念上是基于此的)?

接口与抽象类

在这两个之间选择真正取决于您想做的事情,但是对我们来说幸运的是,埃里希·伽玛(Erich Gamma)可以帮助我们一点.

一如既往地存在权衡,界面为您提供了基础类别的自由,抽象类使您可以自由以后添加新方法. - Erich Gamma

you 不必更改界面而无需更改代码中的许多其他内容,因此避免这种情况的唯一方法是创建一个全新的界面,这可能可能并不总是是一件好事.

Abstract classes应主要用于密切相关的对象. Interfaces更好地为无关类提供共同的功能.

推荐答案

何时使用接口

一个接口允许某人从头开始实现您的接口或在其他代码中实现您的接口,其原始或主要目的与您的接口完全不同.对他们来说,您的界面只是偶然的,必须添加到其代码中才能使用您的软件包.缺点是接口中的每种方法都必须公开.您可能不想暴露一切.

何时使用抽象类

相比之下,

抽象类提供了更多的结构.它通常定义一些默认实现,并为完整实现提供了一些工具.捕获是,使用它的代码必须将您的类用作基础.如果其他想要使用您的软件包的程序员已经独立开发了自己的班级层次结构,那可能会非常不便.在Java中,类只能从一个基类继承.

何时使用

您可以提供两全其美的界面和抽象类.实施者可以忽略您的抽象类,如果他们选择的话.这样做的唯一缺点是通过其接口名称调用方法比通过其抽象类名称调用它们稍慢.

其他推荐答案

重申问题:除此之外还有其他情况 上面提到的位置,我们需要使用抽象类 (一个人是模板方法设计模式在概念上是基于 这只是)

是的,如果您使用JAXB.它不喜欢接口.您应该使用抽象类或使用仿制药来解决此限制.

来自个人博客 >:

接口:

  1. 类可以实现多个接口
  2. 接口根本无法提供任何代码
  3. 接口只能定义公共静态最终常量
  4. 接口无法定义实例变量
  5. 添加一种新方法对实施类具有连锁反应(设计维护)
  6. JAXB无法处理接口
  7. 接口不能扩展或实现抽象类
  8. 所有接口方法均为public

通常,应使用接口来定义合同(要实现的目标,而不是如何实现合同).

抽象类:

  1. 课堂最多可以扩展一个抽象类
  2. 抽象类可以包含代码
  3. 抽象类可以定义静态和实例常数(最终)
  4. 抽象类可以定义实例变量
  5. 现有抽象类代码的修改对扩展类(实施维护)具有连锁反应
  6. 在抽象类中添加新方法对扩展类没有连锁反应
  7. 抽象类可以实现接口
  8. 抽象类可以实现私人和受保护的方法

摘要类应用于(部分)实现.它们可以是限制应实施API合同的方式的手段.

其他推荐答案

当您有所有类都具有相同结构但功能完全不同的方案时,使用接口.

当您有所有类具有相同结构但功能相同的方案时,使用抽象类.

看一篇文章:> http://shoaibmk.blogspot.com/2011/09/abstract-class-is-is-class-which-cannot-be.html

本文地址:https://www.itbaoku.cn/post/627459.html

问题描述

I was asked a question, I wanted to get my answer reviewed here.

Q: In which scenario it is more appropriate to extend an abstract class rather than implementing the interface(s)?

A: If we are using template method design pattern.

Am I correct ?

I am sorry if I was not able to state the question clearly.
I know the basic difference between abstract class and interface.

1) use abstract class when the requirement is such that we need to implement the same functionality in every subclass for a specific operation (implement the method) and different functionality for some other operations (only method signatures)

2) use interface if you need to put the signature to be same (and implementation different) so that you can comply with interface implementation

3) we can extend max of one abstract class, but can implement more than one interface

Reiterating the question: Are there any other scenarios, besides those mentioned above, where specifically we require to use abstract class (one is see is template method design pattern is conceptually based on this only)?

Interface vs. Abstract class

Choosing between these two really depends on what you want to do, but luckily for us, Erich Gamma can help us a bit.

As always there is a trade-off, an interface gives you freedom with regard to the base class, an abstract class gives you the freedom to add new methods later. – Erich Gamma

You can’t go and change an Interface without having to change a lot of other things in your code, so the only way to avoid this would be to create a whole new Interface, which might not always be a good thing.

Abstract classes should primarily be used for objects that are closely related. Interfaces are better at providing common functionality for unrelated classes.

推荐答案

When To Use Interfaces

An interface allows somebody to start from scratch to implement your interface or implement your interface in some other code whose original or primary purpose was quite different from your interface. To them, your interface is only incidental, something that have to add on to the their code to be able to use your package. The disadvantage is every method in the interface must be public. You might not want to expose everything.

When To Use Abstract classes

An abstract class, in contrast, provides more structure. It usually defines some default implementations and provides some tools useful for a full implementation. The catch is, code using it must use your class as the base. That may be highly inconvenient if the other programmers wanting to use your package have already developed their own class hierarchy independently. In Java, a class can inherit from only one base class.

When to Use Both

You can offer the best of both worlds, an interface and an abstract class. Implementors can ignore your abstract class if they choose. The only drawback of doing that is calling methods via their interface name is slightly slower than calling them via their abstract class name.

其他推荐答案

reiterating the question: there is any other scenario besides these mentioned above where specifically we require to use abstract class (one is see is template method design pattern is conceptually based on this only)

Yes, if you use JAXB. It does not like interfaces. You should either use abstract classes or work around this limitation with generics.

From a personal blog post:

Interface:

  1. A class can implement multiple interfaces
  2. An interface cannot provide any code at all
  3. An interface can only define public static final constants
  4. An interface cannot define instance variables
  5. Adding a new method has ripple effects on implementing classes (design maintenance)
  6. JAXB cannot deal with interfaces
  7. An interface cannot extends or implement an abstract class
  8. All interface methods are public

In general, interfaces should be used to define contracts (what is to be achieved, not how to achieve it).

Abstract Class:

  1. A class can extend at most one abstract class
  2. An abstract class can contain code
  3. An abstract class can define both static and instance constants (final)
  4. An abstract class can define instance variables
  5. Modification of existing abstract class code has ripple effects on extending classes (implementation maintenance)
  6. Adding a new method to an abstract class has no ripple effect on extending classes
  7. An abstract class can implement an interface
  8. Abstract classes can implement private and protected methods

Abstract classes should be used for (partial) implementation. They can be a mean to restrain the way API contracts should be implemented.

其他推荐答案

Interface is used when you have scenario that all classes has same structure but totally have different functionality.

Abstract class is used when you have scenario that all classes has same structure but some same and some different functionality.

Take a look the article : http://shoaibmk.blogspot.com/2011/09/abstract-class-is-class-which-cannot-be.html