as we know,we can define allow multiple implement type in java programmering by two ways:interface and abstract.
althrouh interface exclusive method's implementation.but,using interface do not prevent you to provide help on implementation,throuht provide a abstrat skeletal implemantation for every important interface which you export.we can combine interface with abstract class.
in accordance with practice.skeletal implementation called as AbstractInterface.here skeletal implementation is the name of the implemented interface.for example.Collections Framework provide a skeletal implementation for every important collection interface,include,AbstractCollection,AbstractSet,AbstractList and AbstractMap,we also can call them as skeletalCollection,skeletalSet,skeletalList,skeletalMap.it is deep-rooted that the use of Abstract's usage.
ok,let use the implementation of HashSet to explain it.AbstractCollection implements collection interface.simple implementation in it body to simulate multiple inheritance.
public abstract class AbstractCollection<E> implements Collection<E> {
protected AbstractCollection() {
}
public abstract Iterator<E> iterator();
public abstract int size();
public boolean isEmpty() {
return size() == 0;
}
/**
* {@inheritDoc}
*
* <p>This implementation iterates over the elements in the collection,
* checking each element in turn for equality with the specified element.
*
* @throws ClassCastException {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
*/
public boolean contains(Object o) {
Iterator<E> it = iterator();
if (o==null) {
while (it.hasNext())
if (it.next()==null)
return true;
} else {
while (it.hasNext())
if (o.equals(it.next()))
return true;
}
return false;
}
}

本文探讨了Java中如何通过接口和抽象类实现多重继承,特别强调了抽象接口(skeletal implementation)如AbstractCollection、AbstractSet等在Collections Framework中的应用。通过实例剖析HashSet的AbstractCollection实现,揭示了如何通过抽象接口提供通用帮助并确保代码复用。
9808

被折叠的 条评论
为什么被折叠?



