如果一般来讲,一个Abstract class
会,当我们需要强制了一些功能上的Sub-Class
从Super-Class
,让子类有灵活地添加其他functinality需要。
如:
Let Car be the Abstract Super-Class, which has 4 Tyres, 1 Steering ,etc...
Now the Sub classes like Santro i10, Maruti-800, Mahindr Bolero etc are Sub classes,
but they need to have 4 Tyres, 1 Steering to be called a car, not they can have a radio
or not as an additional feature.
2.Interface
在java中被引入,因为没有多重继承。
3.Interface
是更多关于提供role
。
如:
Let Dog be the Super-Class
。
Wild Dogs and Pet Dogs are Sub-Classes.
Wild Behavior and Pet Behavior are Interfaces
Now as both Wild Dog and Pet Dog are dogs, but with different behavious. Then they must
implement the Wild Behavior or Pet Behavior respectively
可能的重复:http://stackoverflow.com/questions/2869222/use-of-java-interfaces-abstract-classes?rq=1 – blackcompe 2012-08-09 18:23:55
抽象类具有类似的优点。 *编程接口并不等同于使用'interface' *,它也可以用抽象类来完成。使用抽象类的主要原因是提供一些可以由其所有子类共享的实现。这里主要的限制是一次只能扩展一个类的能力。 – toniedzwiedz 2012-08-09 18:26:11