Difference between Interface and Abstract Class is favourite question of interviewer while he wants to evaluate your knowledge about Object Oriented Programming. When we consider .NET, there are some more stuff we can include in the answer of question. .NET has introduced some more features which creates some similarities and differences between the two building blocks of OOPs like .NET introduced Automatic Properties, Extension Methods, etc.
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Interface Vs Abstract Class - Jinal Desai .NET
1. Articles from Jinal Desai .NET
Interface Vs Abstract Class
2012-06-19 16:06:40 Jinal Desai
Difference between Interface and Abstract Class is favourite question of interviewer while he wants to
evaluate your knowledge about Object Oriented Programming. When we consider .NET, there are some
more stuff we can include in the answer of question. .NET has introduced some more features which
creates some similarities and differences between the two building blocks of OOPs like .NET introduced
Automatic Properties, Extension Methods, etc.
Following is a try to find some real similarities and differences between the two terminology from the
perspective of .NET paradigm.
Similarities
1. Instantiation: Both cannot be instantiated.
2. Constructor: Both cannot have constructor. Interface is just contract, it does not have any
concrete stuff. Abstract class means you must inherit it in any non abstract child class and you
cannot inherit constructors.
3. Properties: Both have properties. Abstract class can have concrete properties. Interface have
only signature of property which you need to implement in the implementor class.
4. Indexer Property: Both can have indexer property. But again interface can have only signature
which we need to implement in the implementor class while abstract class can have definition as
well along with the signature of indexer property.
5. Event: Both can contain event. Interface can have only signature of event while abstract class
can contain concrete event.
6. Extension Method: Both can have extension methods. But in both Interface and abstract class
extension methods does not have any existence unless you create it’s implementor’s/inheritor’s
object using new keyword.
7. Back Trace: Both can be useful to back trace all it’s implementors/inheritors. We can assign
object of implementor/inheritor to interface/abstract class respectively using new keyword and back
trace all the implementor/inheritor.
Differences
Parameter Interface Abstract Class
.NET does not support multiple
A Class can implement multiple
Inheritance/Implementation inheritance, so a Class can inherit
interface.
only one Abstract Class.
Interface has only signature, you
In Abstract Class you can provide
Default Implementation cannot provide implementation in
default implementation.
interface.
While adding new stuff to interface, it
In case of Abstract Class you can
Default Implementation is nightmare to find all the
take advantage of default
Pros/Cons implementors and implement newly
implementation.
defined stuff.
Access Modifiers (public, Interface does not have access
private, protected, modifiers. Everything defined inside Abstract Class can have access
internal, protected the interface is assumed public modifier.
internal) modifier.
Abstract Class suits to implement
Interface suits to implement core requirements that is shared
peripheral requirements. i.e. both among common type of objects.
Purpose
person and vehicle class can i.e. Person abstract class can be
implement IRun interface. inherited by Manager, Supervisor,
2. Clerk, Administrator…
It is better to use interface when It is better to use abstract class
various implementations share only when various implementations of
method signature. the same kind share common
When to use Use interface when immutable behaviour.
contract is required. Use abstract class to define
Using interface we can design common base class for family of
polymorphic heirarchy of value types. types.
Interface cannot contain data fields.
However in .NET we can have
abstract properties in interface.
Abstract class can have data
Data Fields However just like methods you need
fields.
to implement those abstract
properties inside the implementor
class.
Interface cannot have automatic
properties. You need to implement all Abstract class can have automatic
Automatic Properties
the properties defined in interface into properties.
all it’s implementors.
References
http://www.codeproject.com/Articles/258173/Interfaces-Vs-Abstract-Classes
http://www.codeproject.com/Articles/11155/Abstract-Class-versus-Interface
http://www.codeproject.com/Articles/303081/Comparison-of-Interface-and-Abstract-Class-and-Use
http://www.codeproject.com/Articles/23302/Abstract-Classes-Vs-Interfaces
http://stackoverflow.com/questions/504977/why-cant-i-create-an-abstract-constructor-on-an-abstract-c-
sharp-class
Blog this!
Bookmark on Delicious
Digg this post
Recommend on Facebook
Share on FriendFeed
Share on Linkedin
Share on Orkut
share via Reddit
Share with Stumblers
Share on technorati
Tweet about it
Subscribe to the comments on this post