Click here to Skip to main content
14,880,383 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am a C# developer but confused as why we cannot use a pure abstract class instead of interface.

I am trying to find out the advantage of interface in terms of memory or much deeper concepts and not like we can implement more than one interface or we cannot declare variables inside interface... etc.

What I have tried:

I created an abstract class and an interface with just one method. Then I created two classes, one inheriting the abstract class and another implementing the interface.
Posted
Updated 9-Oct-19 21:57pm

In C#, a class can only derive from one base class - which can be abstract or concrete. If none is specified, the class derives directly from Object. MS reasoned that adding multiple inheritance added too much complexity to C# while providing too little benefit.
This gives us a problem which is solved via Interfaces - a class can also implement as many Interfaces as it wants (using the same syntax as derivation).
There is some "blurring" going on between abstract classes and interfaces in the latest C# version, now that Interfaces can provide default method implementations, but it's still not quite the same as "full derivation" which is still only possible with a single class.

Quote:
Yes. I knew that. Consider, I have only one interface with few methods. In that case, I can always create a pure abstract class and get my job done. Isn't it?
Then there must be some other reason too why to use interface and not pure abstract class or vice-versa.
It could be related to builds or implementation or security whatever.
I am thinking from all aspects actually.
Please let me know if you also can find out some other points.
Thanks a lot.


There are no other points - it is purely about Single Inheritance.
Think about it: you have a class which has to inherit from SqlDataReader for example. That means the class definition must be:
C#
public MyClass : SqlDataReader
   {
   ...
   }

Suppose you also want that class to be usable in foreach loops: that means it must implement the IEnumerator interface*. Since C# does not allow multiple inheritance,without interfaces you would have to choose between deriving from SqlDataReader and IEnumerator. But, because you can implement as many Interfaces as you want, you can have your cake and eat it:
C#
public MyClass : SqlDataReader, IEnumerator
   {
   ...
   }
Without having the extra complexity (and there is a load of that) that multiple inheritance requires.


* Over simplification, I know - just go with it.
   
v2
Comments
Member 11072126 10-Oct-19 2:38am
   
Hi, thanks for your response. But, i know about the multiple inheritence thing and i know that interface can solve it.
I wanted to know anything except multiple inheritence.
OriginalGriff 10-Oct-19 3:16am
   
That is why interfaces exist - because C# does not allow multiple inheritance.
The "default methods" feature of interfaces is new, and is intended to allow an interface to be expanded without breaking existing code, not as a substitute for abstract classes.
Member 11072126 10-Oct-19 3:28am
   
Yes. I knew that. Consider, I have only one interface with few methods. In that case, I can always create a pure abstract class and get my job done. Isn't it?
Then there must be some other reason too why to use interface and not pure abstract class or vice-versa.
It could be related to builds or implementation or security whatever.
I am thinking from all aspects actually.
Please let me know if you also can find out some other points.
Thanks a lot.
OriginalGriff 10-Oct-19 3:47am
   
Answer updated.
Maciej Los 10-Oct-19 3:59am
   
And also upvoted!
:laugh:
You may well use an abstract class instead of an interface. No one prevents you doing that and there possibly will be no harm to our wonderful world.
However, as Griff already explained, in C# you cannot have two base classes, so, if your clss should provide, say, two different behaviours, then it either
  • inherits from an abstract class AND impements an interface
or
  • implements two interfaces

Symmetry considerations could make you to opt for the secondo choice.

Now, while it is clear you need just one behaviour (hence one abstract base class), Mummy MS has to think in large, and provide means to all developers needing to implement multiple behaviours (hence the interfaces).
   
Comments
Maciej Los 10-Oct-19 3:59am
   
5ed!
CPallini 10-Oct-19 4:05am
   
You are too good, Maciej, thank you very much!
Member 11072126 10-Oct-19 5:12am
   
Thanks @CPallini for the response. Even Griff also told the same.
CPallini 10-Oct-19 6:02am
   
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900