java 扩展抽象类和实现接口的组合

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

combination of extend abstract class and implement interface

javaoopinterfaceabstract-class

提问by Srujan Kumar Gulla

Possible Duplicate:
Why does ArrayList have “implements List”?

可能的重复:
为什么 ArrayList 有“实现列表”?

I have gone through several articles Diff b/w I vs ACand questions on SO about when to use an interface vs Abstract Class.

我已经阅读了几篇文章Diff b/w I vs AC以及关于何时使用接口 vs 抽象类的问题。

When I stumbled upon ArrayList Class, ArrayList class extends AbstractList Class implements List interface. AbstractList and List has common methods like add(), remove() etc.

当我偶然发现 ArrayList 类时,ArrayList 类扩展了 AbstractList 类实现了 List 接口。AbstractList 和 List 有常用的方法,如 add()、remove() 等。

Edit:In other words, if AbstractList implements List why have Arraylist class implement List

编辑:换句话说,如果 AbstractList 实现 List 为什么有 Arraylist 类实现 List

When is this kind of combination inheritance behavior design preferred when both have common methods?

当两者都有共同的方法时,这种组合继承行为设计何时更受欢迎?

public class ArrayList<E> extends AbstractList<E>
        implements List<E>

采纳答案by Tapas Bose

Sometime I create an abstract BaseClasswhich implements an interface IBaseClassand create ChildClasswhich extends the BaseClass. What I mean is:

有时我会创建一个抽象BaseClass来实现一个接口IBaseClass并创建ChildClass一个扩展BaseClass. 我的意思是:

public interface IBaseClass {

}

public abstract class BaseClass implements IBaseClass {

}

public class ChildClass extends BaseClass {

}

Now Say I have an interface IChildClassand ChildClassimplements it.

现在说我有一个接口IChildClassChildClass实现它。

public class ChildClass extends BaseClass implements IChildClass {

}

And say the IBaseClasshas a method someMethod. Now if you want to use the instance of ChildClassby the implementing interface IChildClasslike:

并说IBaseClass有一个方法someMethod。现在,如果您想ChildClass通过实现接口使用 的实例,IChildClass例如:

IChildClass obj = new ChildClass();

Then you cannot call obj.someMethod(). Then you need to do this:

那么你不能调用obj.someMethod(). 然后你需要这样做:

public interface IChildClass extends IBaseClass {

}

You can find in Java 1.2 documentationthe the interface java.util.Listdoesn't extends java.util.Collection, which it does in the latest version. java.util.Listwas the supreme interface at that time and java.util.AbstractListimplements that Listalso the ArrayList. AbstructListin turns extends the AbstractCollection. AbstractCollectionwas then supreme class. Currently AbstractCollectionimplements Collection. So it is clear that this design pattern has been followed to keep the Java's ever expandable pattern in mind.

您可以在Java 1.2 文档中找到接口java.util.List没有扩展java.util.Collection,它在最新版本中没有扩展。java.util.List是当时最高的接口,并且java.util.AbstractList实现ListArrayList. AbstructList依次扩展AbstractCollection. AbstractCollection那时是至高无上的阶级。目前AbstractCollection实现Collection. 所以很明显,遵循这种设计模式是为了牢记 Java 的可扩展模式。

回答by Peter Lawrey

The implements Listis redundant in this case. It is possibly to make it clear this call implements List without have to navigate the class hierarchy.

implements List是在这种情况下,多余的。可能要明确此调用实现 List 而不必导航类层次结构。

回答by NPE

AbstractList<E>also implements List<E>, so they are the same methods.

AbstractList<E>也实现了List<E>,所以它们是相同的方法。