如果一般來講,一個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