简体   繁体   English

Java-接口不能由抽象类完成吗?

[英]Java- What can be done by interfaces that can not be done by abstract class?

I know the difference between "interface" and "abstract class". 我知道“接口”和“抽象类”之间的区别。 But could you provide me the single example which can be built through the "interface" but not through the "abstract class" leaving the example of "multiple inheritance"? 但是,您能否提供一个可以通过“接口”构建但不能通过“抽象类”构建的单一示例,而仅留下“多重继承”的示例?

A simple example is with objects that represent the same entity but have different bevaviour. 一个简单的示例是表示相同实体但具有不同行为的对象。 Consider for instance the birds. 例如考虑鸟类。 Some birds can fly and some can't. 有些鸟会飞,有些鸟不会飞。 It would be wrong to define an abstract class Bird which forces all it's subclasses to implement a fly method. 定义abstract class Bird强制其所有子类实现fly方法是错误的。

So in such a case it's ok to define methods as eat() or sleep() as abstract in the abstract class Bird but not fly() since not all birds can fly. 因此,在这种情况下,可以在abstract class Bird中将方法定义为eat()sleep()作为抽象,但不能将fly()定义为fly()因为并非所有鸟都可以飞行。

Generally you would define an interface called for instace Flyable which would contain the definition of the fly() method, which would have to be overriden by classes implementing the interface. 通常,您将定义一个称为instace Flyable的接口,该接口将包含fly()方法的定义,而实现该接口的类必须将其覆盖。

In the end you would end up with something like: 最后,您将得到如下结果:

public abstract class Bird{
  public abstract void eat();
  public abstract void sleep();
}

public interface Flyable{
  void fly();
}

public class Eagle extends Bird implements Flyable{ 
 .... has to implement eat(), sleep() and fly()
}

public class Ostrich extends Bird{
... has to implement only eat() and sleep() since ostrich can't fly
}

100% percentage abstraction is called Interface. 100%的百分比抽象称为接口。

List is a interface. List是一个接口。 It jus jas method definition. 它是jas方法的定义。

Abstract Class : It has some method implementation. 抽象类:它具有一些方法的实现。

AbstractList is a abstract class . AbstractList是一个抽象类。 It has method implementation. 它具有方法实现。

Comparable interface in java.lang.Comparable can never be implemented as a class (ie Abstract class) This is because the compareTo method depends completely upon the class which imnplements it. java.lang.Comparable中的Comparable接口永远不能实现为一个类(即Abstract类),这是因为compareTo方法完全依赖于实现它的类。 And it is required that for all object comparisons, they should have implemented this method (The objects that are being compared) 并且要求所有对象比较都应该实现此方法(正在比较的对象)

Note: Comparable class is used to compare 2 objects for equality 注意:Comparable类用于比较2个对象是否相等

Whereas AbstractStringBuilder is better written as an abstract class is extended by both StringBuilder as well as StringBuffer. 鉴于StringStringer和StringBuffer都可以扩展抽象类,因此AbstractStringBuilder更好。 This way the common functionalities (like charAt, expandCapacity, indexOf )in both the classes can be put in this abstract class..This way code can be reused which cannot be in case of an interface 这样,可以将两个类中的通用功能(例如charAt,expandCapacity,indexOf)放在此抽象类中。这种方式可以重用代码,在接口情况下是不能的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM