Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
I was going through the different using scenarios for Interface and Abstract class on MSDN

http://msdn.microsoft.com/en-us/library/scsyfw1d(v=vs.71).aspx

but got some confusion on below difference:

If you anticipate creating multiple versions of your component, create an abstract class. Abstract classes provide a simple and easy way to version your components. By updating the base class, all inheriting classes are automatically updated with the change. Interfaces, on the other hand, cannot be changed once created. If a new version of an interface is required, you must create a whole new interface.

I have confusion on this Particular Bold Sentence below:
on the other hand, cannot be changed once created. If a new version of an interface is required, you must create a whole new interface

Could you help explain?
Posted
Comments
Philippe Mori 2-Aug-14 8:41am    
By the way the reference is very old and it was probably more or less a copy of the rules that were used for COM (Active/X) development in those days. As far as I understand, in most cases, that requirement will not apply for stand-alone .NET application.

First of all, most questions on the "difference" make no sense. How would you define the "difference"? If you did not get it, please tell us: what's the difference between apple and Apple?
You should just learn how interfaces work and how base classes work. In essence, to be honest, you need learn how OOP works, because the question is at the very heart of OOP, so just answering it exactly would be pretty pointless. Just learn both things separately, make sure you understand the use both approached, and then ask further questions, if you have to.

As to the MSDN article you referenced, you should understand that, even though MSDN documentation became very good these days, some articles are quite misleading or incomplete, and some areas are not covered at all. Unfortunately, it happens to the topics which are supposed to cover first principle, something related to fundamental notions of computer sciences. So, my advice is simple: do yourself a favor and completely disregard this short and not informative article, in particular, the sentence you quoted in bold. It's almost gibberish, honestly. Let's just forget about it.

To be useful, your question should be different: not "what's the difference"? but something like: "how to decide where to use abstract classes and hierarchy of derived classes and where the interfaces, and where to combine these approaches?" That would make the question sensible, but not trivial, and, unfortunately, not so simple. Please see my past answers:
Doubts on Interfaces[^], actually, this answer references all other answers:
How to decide to choose Abstract class or an Interface[^],
Difference between abstract class and interface if they have same no of methods and var[^],
When we use abstract and when we use interface...?[^],
Interfaces and Polymorphism[^].

—SA
 
Share this answer
 
Abstract class is a combination of abstract and non abstract member and interface contain only abstract member.
 
Share this answer
 
Comments
Pete O'Hanlon 28-Jan-14 5:50am    
Err no. I can see what you are trying to say here, but you've not said what you think you said. I assume what you were trying to say was that an interface contains only the contract about what must be implemented, and provides no actual implementation.
With Interfaces , if you are planning to change something. The whole contract gets changed and you will have to change your implementations too. That's the reason you can have multiple version of components using interfaces. There's only one way inherit the newly created interface with the existing one.

With Abstract class, You could change the behavior or the implementation for multiple versions and can have multiple versions of components so the code won't break unless the method signature changes you have to make a change in all versions.
 
Share this answer
 
Comments
Philippe Mori 2-Aug-14 8:35am    
Well, in practice, interfaces that are used inside a single application are not mouch different that an abstract class with only abstract methods.

The versionning issues might apply in scecific cases like when interface are exposed through COM (Active/X) or are used by assemblies that are not recompiled with the application (for example, in your application has third-party plug-in support).

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