Interface Vs Abstract Class - Jinal Desai .NET

1,007 views
912 views

Published on

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.

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,007
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
51
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Interface Vs Abstract Class - Jinal Desai .NET

  1. 1. Articles from Jinal Desai .NETInterface Vs Abstract Class2012-06-19 16:06:40 Jinal DesaiDifference between Interface and Abstract Class is favourite question of interviewer while he wants toevaluate your knowledge about Object Oriented Programming. When we consider .NET, there are somemore stuff we can include in the answer of question. .NET has introduced some more features whichcreates some similarities and differences between the two building blocks of OOPs like .NET introducedAutomatic Properties, Extension Methods, etc.Following is a try to find some real similarities and differences between the two terminology from theperspective 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 multipleInheritance/Implementation inheritance, so a Class can inherit interface. only one Abstract Class. Interface has only signature, you In Abstract Class you can provideDefault Implementation cannot provide implementation in default implementation. interface. While adding new stuff to interface, it In case of Abstract Class you canDefault Implementation is nightmare to find all the take advantage of defaultPros/Cons implementors and implement newly implementation. defined stuff.Access Modifiers (public, Interface does not have accessprivate, protected, modifiers. Everything defined inside Abstract Class can have accessinternal, 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. 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 commonWhen 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 dataData 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 automaticAutomatic Properties the properties defined in interface into properties. all it’s implementors.Referenceshttp://www.codeproject.com/Articles/258173/Interfaces-Vs-Abstract-Classeshttp://www.codeproject.com/Articles/11155/Abstract-Class-versus-Interfacehttp://www.codeproject.com/Articles/303081/Comparison-of-Interface-and-Abstract-Class-and-Usehttp://www.codeproject.com/Articles/23302/Abstract-Classes-Vs-Interfaceshttp://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

×