Click here to Skip to main content
15,884,032 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
What is the difference between an abstract class and an interface? Where and why would one be more beneficial over the other?
Posted
Updated 5-Feb-13 12:50pm
v2

CSS
Interface : should be used if you want to imply a rule on the components which may or may not be
related to each other

Abstract Class : should be used where you want to have some basic or default behavior or
implementation for components related to each other

Pros:
Interface:

    Allows multiple inheritance
    provides abstraction by not exposing what exact kind of object is being used in the context
    provides consistency by a specific signature of the contract


Abstract Class:

    faster then interface
    has flexibility in the implementation (you can implement it fully or partially)
    can be easily changed without breaking the derived classes


Cons:
Interface :

    Must implement all the contracts defined
    cannot have variables or delegates
    once defined cannot be changed without breaking all the classes


Abstract Class :

    cannot be instantiated
    does not support multiple inheritance
 
Share this answer
 
I think that if you do a search you will see tons of documents explaining the difference. But I will resume it as this:

Interfaces can't have a default implementation for any method. Abstract classes can.
A class can implement any interface, but if you already have a class with a base type, it will not be capable of inheriting from an abstract class.

When to use each one?
In general I use both.
I create an interface so all methods can be reimplemented in many different ways (that helps in remoting, for example, or to create fake implementations for testing purposes).
But if some method is probably going to be implemented in the same way by many classes, I create an abstract class that already implement such method but let other methods be fully virtual.

A sample? If you look at the sample of my latest article Creating your own animation format[^] you will see the following:

IFrameReader. This interface has a non-generic untyped version that can be used if you have the right objects but don't know the generic type at compile time (that is, you pass parameters as objects).
Then it has a generic version that you can use and avoid casts if you know the right types at compile time (the ManagedBitmap<Rgb24> for example).

But, to those who want to implement a FrameReader class and don't want to lose time implementing both interfaces (as the untyped version will simple redirect to the typed version), there is the FrameReader class that already implements both interfaces. The untyped (non-generic) version simple redirects to the typed version, which keep those typed methods as abstract.

So, inheriting from the FrameReader class you get an already implemented IFrameReader but you still need to implement the methods that make the IFrameReader<T> work.

I hope this is clear enough.
 
Share this answer
 
v2
Comments
[no name] 16-Oct-14 12:27pm    
Good points, my 5.
Refer below article

Abstract Class versus Interface[^]
 
Share this answer
 
I am not sure whether you have google it or not. There a bunch of articles describing this. Here you go with some of them:

Abstract Class versus Interface[^]

http://stackoverflow.com/questions/761194/interface-vs-abstract-class-general-oo[^]

http://www.c-sharpcorner.com/uploadfile/prasoonk/abstract-class-vs-interface/[^]

Happy Learning!!!
 
Share this answer
 

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