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
combination of extend abstract class and implement interface
提问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 BaseClass
which implements an interface IBaseClass
and create ChildClass
which 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 IChildClass
and ChildClass
implements it.
现在说我有一个接口IChildClass
并ChildClass
实现它。
public class ChildClass extends BaseClass implements IChildClass {
}
And say the IBaseClass
has a method someMethod
. Now if you want to use the instance of ChildClass
by the implementing interface IChildClass
like:
并说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.List
doesn't extends java.util.Collection
, which it does in the latest version. java.util.List
was the supreme interface at that time and java.util.AbstractList
implements that List
also the ArrayList
. AbstructList
in turns extends the AbstractCollection
. AbstractCollection
was then supreme class. Currently AbstractCollection
implements 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
实现List
了ArrayList
. AbstructList
依次扩展AbstractCollection
. AbstractCollection
那时是至高无上的阶级。目前AbstractCollection
实现Collection
. 所以很明显,遵循这种设计模式是为了牢记 Java 的可扩展模式。
回答by Peter Lawrey
The implements List
is 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>
,所以它们是相同的方法。