In abstract class we can make all methods abstract so that it can work like an interface, so why to use interface at all?? One of the reason I could come up was that we can implement multiple interface not extend multiple class.. Is there any design or performance related thing involved??
-
3You've answered the question yourself... – jlordo May 04 '13 at 13:53
-
1Write the question as the answer, accept it and you're done. – Maroun May 04 '13 at 13:54
-
http://stackoverflow.com/questions/10040069/abstract-class-vs-interface-in-java – Yaroslav Mytkalyk May 04 '13 at 13:54
-
An abstract class should be an Interface if doesn't have any code besides abstract method declaration. And what you can't do with an interface is creating protected methods. The Interface methods are always public, so an Interface defines something that really means "interface". – Yaroslav Mytkalyk May 04 '13 at 14:02
3 Answers
You already got the answer. Using Interfaces we can enforce multiple types of behaviours where as using classes will not work for you. For example, you can enforce a class to be IComparable as well as INumerable however it is not possible if you want to do it with classes.

- 4,339
- 2
- 27
- 51
You've already identified the one thing that interfaces allow that abstract classes don't allow. One class can't extend multiple abstract classes.
Is there any design or performance related thing involved??
There is no performance difference.
You could argue that the single inheritance restriction of abstract classes (in fact, all classes) makes this "a design thing" though. Certainly it would seriously restrict your use of polymorphism in an OO design.
(You could also argue that you can't follow the maxim of "programming to the interface" when you don't have interfaces. However, that is a weak argument ... a terminological quibble.)

- 698,415
- 94
- 811
- 1,216
Design wise it is preferred guidelines to use Interface
for you code behavior/contract/functionality definition (see List
interface) and use Abstract
class where you have atlease some reusable (via inheritance) method implementation .
Though having all abstract
method is possible, but in such cases an Interface
is preferred.

- 7,502
- 3
- 31
- 32