Interface:
–> If your child classes should all implement a certain group of methods/functionalities but each of the child classes is free to provide its own implementation then use interfaces.
For e.g. if you are implementing a class hierarchy for vehicles implement an interface called Vehicle which has properties like Colour MaxSpeed etc. and methods like Drive(). All child classes like Car Scooter AirPlane SolarCar etc. should derive from this base interface but provide a seperate implementation of the methods and properties exposed by Vehicle.
–> If you want your child classes to implement multiple unrelated functionalities in short multiple inheritance use interfaces.
For e.g. if you are implementing a class called SpaceShip that has to have functionalities from a Vehicle as well as that from a UFO then make both Vehicle and UFO as interfaces and then create a class SpaceShip that implements both Vehicle and UFO .
Abstract Classes
–> When you have a requirement where your base class should provide default implementation of certain methods whereas other methods should be open to being overridden by child classes use abstract classes.
For e.g. again take the example of the Vehicle class above. If we want all classes deriving from Vehicle to implement the Drive() method in a fixed way whereas the other methods can be overridden by child classes. In such a scenario we implement the Vehicle class as an abstract class with an implementation of Drive while leave the other methods / properties as abstract so they could be overridden by child classes.
–> The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share.
For example a class library may define an abstract class that is used as a parameter to many of its functions and require programmers using that library to provide their own implementation of the class by creating a derived class.
Use an abstract class
- When creating a class library which will be widely distributed or reused—especially to clients, use an abstract class in preference to an interface; because, it simplifies versioning. This is the practice used by the Microsoft team which developed the Base Class Library. ( COM was designed around interfaces.)
- Use an abstract class to define a common base class for a family of types.
- Use an abstract class to provide default behavior.
- Subclass only a base class in a hierarchy to which the class logically belongs.
Use an interface
- When creating a standalone project which can be changed at will, use an interface in preference to an abstract class; because, it offers more design flexibility.
- Use interfaces to introduce polymorphic behavior without subclassing and to model multiple inheritance—allowing a specific type to support numerous behaviors.
- Use an interface to design a polymorphic hierarchy for value types.
- Use an interface when an immutable contract is really intended.
- A well-designed interface defines a very specific range of functionality. Split up interfaces that contain unrelated functionality.
Nilkanth said:
Thanks
Nice explanation of abstract class & interfaces.
sathishkumar said:
Hey nice explanation.
vikram said:
very undestandable example
uttam Palkar said:
Thank’s a lot
jeetendra kumar said:
nice example
Javed said:
Hi…
thanks.
Good example and define it.
kiran said:
Thanks a lot, Nice example
khaja nizam said:
Really fabulous explanation n i feel its very good ans for the people who is appearing for the interviews
bcoz noadays this question is more common.
Nirav Valera said:
Very easy to understand
Ajay Kumar said:
Nice scenario to elaborate the use of interface and abstract class.
Many many thanks to you…
Mayank said:
It has changed my life..earlier i was in tulip but after reading this i am in microsoft.they asked me what is differnce between abstract class & interfaces and i said the same..they got impressed n hired me as a senior s/w developer of c#
Thanks for sharing
Poornima said:
Nice explanation. Thanks a lot
Aneesh said:
Good post. I was able to understand the scenario of using interface & abstract class clearly.
Shij said:
Excellent article.Thanks a lot
ravindra.patil said:
nice thanks for informatin.
rohit said:
Nice article.
Jubin said:
Very nice explanation.Thank you
dhananjaypandeys said:
I will give Gold Madel to it.
Pingback: C# Interface Abstract « code siblings,adventures,songs and excerpts of my daily work
kiran said:
Nice explanations.
tulasiram said:
thank you very much
Rajanish said:
Thanks a lot for describing it
venkat said:
Nice and brief explanation
soundar said:
Superb Explanation
Ruby said:
Very nice to understand the real scenario …
kaleem said:
good
Haneep CR said:
Nice explanation, Thank you..
debaratisw said:
The post is very good and clear
dixitmca said:
Good Demonstration…Thanks Yarr
xam said:
Perfectly explained…. Thanks a lot.
Ravindra said:
Hats off……
Shruti Saini said:
A very good example. It had cleared my doubt of using abstract vs interfaces. Hats off for such a great example. Thank you sir.
D.L.Santosh said:
nice….
ishu said:
nice article…! but i require some examples which may further improve this..!
Suman Singh said:
Very nice!!!
Ganesh Pawar said:
It’s very interesting and easily understandable……………..nice…..Thanx…..for this
Parthasarathi said:
Thank you..
rahul said:
clear as water.thanks
sayyad usman ali said:
its good and easy to understand…
Uttam Chaturvedi said:
Hey, that’s really a good explanation of Abstract and Interface in real life.
Thanks
Rohin said:
Well defined…thank u
Ashish said:
Best Example i had seen………awesome. Fully Understandable……
Jagdish Patil said:
Nice Article, Very helpful to understanding the concept. Thanks!
aditya said:
very nice…
Dhiraj Mishra said:
nice explanation