Advantages of Abstraction and Polymorphism in Java
This question already has an answer here:
Simply saying: interface is a contract, abstract class is skeletal implementation. (Additionally, in Java interfaces are mostly used because it's not possible to extend multiple classes.)
Contract says what, implementation says how.
Example of interface: java.util.List
. It has all methods that any list should have: add()
, size()
, indexOf()
and so on.
Example of abstract class: java.util.AbstractList
. Though it has many abstract methods, some List
methods, that don't depend on the way elements are stored in the concrete list, are implemented there ( addAll()
, equals()
, toString()
and others). To create complete implementation, not all List
methods should be implemented, thus making implementor's work easier.
上一篇: 界面与抽象类的区别
下一篇: Java中抽象和多态性的优点