java 纯抽象类和接口

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

pure abstract class and interface

java

提问by user241924

Can anyone tell me what exactly the difference between an completely abstract class and an interface?

谁能告诉我完全抽象的类和接口之间到底有什么区别?

An Abstract class can also have all its methods as abstract. An interface has all its methods as abstract. What is the main difference between the two in this scenario?

Abstract 类也可以将其所有方法都设为抽象。接口的所有方法都是抽象的。在这种情况下,两者之间的主要区别是什么?

If there is difference between a pure abstract Class and interface? What is the use of interface? Where interface is being used we can make use of pure abstract class?

纯抽象类和接口是否有区别?接口有什么用?在哪里使用接口我们可以使用纯抽象类?

采纳答案by Damien

To complete the former answers :

要完成以前的答案:

An interface is a "contract". If a class implements an interface it have to propose all the services listed in the interface.

接口是一个“合同”。如果一个类实现了一个接口,它必须提出接口中列出的所有服务。

An abstract class is a skeleton. It defines a certain way its extended classes will work while letting them some free space (the abstract methods) to be unique.

抽象类是一个骨架。它定义了它的扩展类的某种工作方式,同时让它们有一些自由空间(抽象方法)是唯一的。

A pure abstract class doing the same thing as a interface but have the problem of unique extending so, for me, it have no interest

一个纯粹的抽象类和接口做同样的事情,但有唯一扩展的问题,所以对我来说,它没有兴趣

回答by sateesh

Every interface is implicitly abstract: Every method declaration in the body of interface is implicitly abstract and public.

每个接口都是隐式抽象的:接口主体中的每个方法声明都是隐式抽象和公共的。

An abstract class has methods that can contain implementation. Abstract methods can be either public, protected or default access (package visible). Unlike interfaces abstract classes can contain fields that are not static and final.

抽象类具有可以包含实现的方法。抽象方法可以是公共的、受保护的或默认访问(包可见)。与接口不同,抽象类可以包含非静态和最终的字段。

Also see:
Interfaces vs Abstract classesand the Java tutorial

另请参阅:
接口与抽象类Java 教程

回答by catwalk

In Java and C#, one can use multiple interfaces to derive from and only a single class to inherit from,

在 Java 和 C# 中,可以使用多个接口来派生,并且只能使用一个类来继承,

回答by Dhananjay Talekar

It's not a very theorotical explaination but, programatically it's all correct

这不是一个非常理论化的解释,但是,从编程上来说,这一切都是正确的

                                    Interface                   Abstract Class
Extend Class                            No                          Yes
Extend Abstract Class                   No                          Yes
Implement Interface                     Yes(Extend Interface)       Yes
Variables                               Public Static Final         Public/Protected/Private/static/final/transient/volatile
Contain Non-Public Method               No                          Public/Protected/*Private
Contain Abstract Method                 Yes                         Yes
Contain No-Body, Non-Abstract Method    Yes                         No
Contain Defined Method                  No                          Yes
Contain Main Method                     No                          Yes

*Abstract classes can have private methods, but not abstract private methods.

*抽象类可以有私有方法,但不能有抽象私有方法。

回答by switang

One reason to choose pure abstract over interface is to force sub classes to implement particular methods that are implemented by a super class.

选择纯抽象而不是接口的原因之一是强制子类实现由超类实现的特定方法。

For example (in Java),

例如(在 Java 中),

Say you want all extending classes to implement toString(), equals(), and hashCode().

假设您希望所有扩展类都实现 toString()、equals() 和 hashCode()。

You could create an interface called ForceSomeMethods for that contract, but those methods are implicitly implemented by Object.

您可以为该合约创建一个名为 ForceSomeMethods 的接口,但这些方法是由 Object 隐式实现的。

Making ForceSomeMethods a pure abstract class with toString(), etc as abstract methods, all subclasses will be forced to implement those methods.

使 ForceSomeMethods 成为纯抽象类,以 toString() 等作为抽象方法,所有子类都将被迫实现这些方法。

回答by Frank Grimm

An abstract class can provide an implementation, i.e. (public, protected, private) method bodies. An interface can just declare public method signatures. These methods have to be realized (in the form of method bodies) by classes implementing the interface.

抽象类可以提供一个实现,即(公共、受保护、私有)方法。接口只能声明公共方法签名。这些方法必须由实现接口的类来实现(以方法体的形式)。

回答by Wim Hollebrandse

I'm just going to address one point (mainly because the other questions have been addressed already):

我只想解决一点(主要是因为其他问题已经解决了):

"Where interface is being used we can make use of pure abstract class?"

“在使用接口的地方,我们可以使用纯抽象类吗?”

In theory, you could. However, you will lose flexibility and loose coupling to some extent. It's far more preferable to code to interfaces and pass those around, especially in Inversion of Control (IoC) scenarios and from an integration point of view, as this allows far greater extensibility.

理论上,你可以。但是,您会在某种程度上失去灵活性和松散耦合。对接口进行编码并传递这些接口要好得多,尤其是在控制反转 (IoC) 场景中以及从集成的角度来看,因为这允许更大的可扩展性。

回答by EightyOne Unite

Since the question is about pureabstract classes then I'd say the answer is going to be related to inheritance and scope. It's something I've wondered myself many times and this is what I've come up with.

由于问题是关于抽象类,那么我会说答案将与继承和范围有关。这是我自己想过很多次的事情,这就是我想出的。

Obviously the features related to multiple inheritance have been answered previously so I won't go in to any of that. Scope is a big one though.

显然,与多重继承相关的特性之前已经回答过,所以我不会深入讨论。范围是一个很大的范围。

In an interfaceyou can't define a member's access modifiers since they are implicitly public,...you are defining the publicinterface for it's eventual implementation. There's an important difference there since you candefine a protected abstract member in a pure abstract class.

接口中,您不能定义成员的访问修饰符,因为它们是隐式公共的,...您正在为其最终实现定义公共接口。有一个重要的区别,因为您可以在纯抽象类中定义受保护的抽象成员。

Inheriting from such a class definition would force the inheritor to implement the abstract member but scope it privately to consumers of the class (though it would have to be defined as protectedso unless the class was marked as sealedfurther inheritors would have access).

从这样的类定义继承将强制继承者实现抽象成员,但将其私有范围限定为该类的使用者(尽管必须如此定义,protected除非该类被标记为sealed进一步的继承者可以访问)。

In essence you can define a private interface using pure abstract classes. Whether that's a good idea is a different question altogether but one good use I've seen it used for is to enforce design patterns and standardize class designs.

本质上,您可以使用纯抽象类定义私有接口。这是否是一个好主意完全是一个不同的问题,但我见过它的一个很好的用途是强制执行设计模式和标准化类设计。

HTH

HTH

回答by Thomas L?tzer

There are three differences:

有以下三个区别:

  1. Interfaces can only declare public methods (i.e. no protected or package-private visible methods) and can not declare any fields
  2. Subclasses can only extend at most one abstract class, but can implement any number of interfaces
  3. The abstract class canalso have implementations for some or all of the methods
  1. 接口只能声明公共方法(即没有受保护或包私有的可见方法)并且不能声明任何字段
  2. 子类最多只能扩展一个抽象类,但可以实现任意数量的接口
  3. 抽象类也有一些或全部的方法的实现

回答by Dusk

You can use Interface for multiple inheritance, but you can't use abstract class for multiple inheritance.

可以使用接口进行多重继承,但不能使用抽象类进行多重继承。

All the methods in Interface is public by default, by in abstract class, only the methods which you've set as an abstract need to be declared public.

Interface 中的所有方法默认都是 public 的,在抽象类中,只有你设置为抽象的方法需要声明为 public。