开发者

Can someone give me an idea of when the abstract class and interfaces has to be used? [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

When to use an interface instead of an abstract class and vice versa?

开发者_开发技巧

Can someone give me an idea of when the abstract class and interfaces has to be used?

IF possible supporting with a real time example?

Thanks in Advance.


An interface is a complete abstraction of some role. You use an interface when you want to ensure certain related behaviours in a type.

An abstract class is more of a default behaviour / infrastructure provider for a collection of classes with some commonality. You use an abstract class when you have some idea of how a collection of classes should behave but would like to refine and augment the behaviour specifically in the individual classes that are extending the abstract class.


abstract classes are designed with implementation gaps for sub-class to fill in.

interfaces are syntactically similar to classes but they lack instance variables & methods.

abstract classes can also have both abstract methods & non-abstract methods. where as in interface methods are abstract only, & variables are implicitly static&final.

see the links for more detail

http://codeofdoom.com/wordpress/2009/02/12/learn-this-when-to-use-an-abstract-class-and-an-interface/

http://www.sap-img.com/java/when-we-go-for-abstract-and-interface.htm

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜