Can anyone elaborate the need of Interface, when we already have Abstract classes?
-
An interface describes _behaviors_ a class might have, without saying anything about _how_ it will exhibit those behaviors. An abstract class does the same, but also allows implementations. – Tim Biegeleisen Aug 27 '16 at 12:07
-
@TimBiegeleisen I know the difference you just told the same i told my elder brother who is a senior software developer then he asked: Why do we need Interface when same thing as Interface can be achieved by making all methods abstract in an abstract class? – Khubaib Shirani Aug 27 '16 at 13:04
-
@TimBiegeleisen I searched but couldn't get it. – Khubaib Shirani Aug 27 '16 at 13:05
-
2C# does not support multiple inheritance. If you use an abstract class as a stand-in for an interface, then everybody who wants to implement the interface must derive from your abstract class and cannot derive from any other class. – Raymond Chen Aug 27 '16 at 19:02
-
I reopened this question because the possible dupliate is about Java and [we should care about the language difference](http://meta.stackoverflow.com/questions/319542/possible-duplicate-question-in-another-programming-language). – Cheng Chen Aug 31 '16 at 06:16
2 Answers
You can achieve anything you want using only abstract classes instead of interfaces if you consider the syntax, but there is a semantic difference. An abstract class can have implemented methods which will be inherited in subclasses, so it can have defined behaviors, while interfaces can only have declared behaviors.
On project-management level there are cases when the leader decides that you should write declarations and you should not have the possibility to implement them. In that case they tell you to implement an interface and they will know that as long as the thing you create is an interface, all the methods will be forced to be implemented in classes which implement it. Also, if you encounter an abstract class, you might want to implement one of its methods, without knowing that it should not be implemented on that level of the hierarchy. So, interfaces are actually good to have a way to make sure that some behaviors are declared, but not defined.

- 64,414
- 37
- 100
- 175
An abstract class is better to use when its children share a lot of common pre-defined functionality but still differentiate in important ways (hence the need for abstract methods and why you can't instantiate an abstract class)
Inheritance is simply a "contract" on what a class implementing that interface must do.
Inheritance allows for greater polymorphism..

- 31
- 3