SlideShare a Scribd company logo
1 of 2
Download to read offline
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,
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

More Related Content

Recently uploaded

Structuring Teams and Portfolios for Success
Structuring Teams and Portfolios for SuccessStructuring Teams and Portfolios for Success
Structuring Teams and Portfolios for Success
UXDXConf
 

Recently uploaded (20)

IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024
 
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdfHow Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
 
Syngulon - Selection technology May 2024.pdf
Syngulon - Selection technology May 2024.pdfSyngulon - Selection technology May 2024.pdf
Syngulon - Selection technology May 2024.pdf
 
Speed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in MinutesSpeed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in Minutes
 
A Business-Centric Approach to Design System Strategy
A Business-Centric Approach to Design System StrategyA Business-Centric Approach to Design System Strategy
A Business-Centric Approach to Design System Strategy
 
Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi IbrahimzadeFree and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
 
Introduction to Open Source RAG and RAG Evaluation
Introduction to Open Source RAG and RAG EvaluationIntroduction to Open Source RAG and RAG Evaluation
Introduction to Open Source RAG and RAG Evaluation
 
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
 
ECS 2024 Teams Premium - Pretty Secure
ECS 2024   Teams Premium - Pretty SecureECS 2024   Teams Premium - Pretty Secure
ECS 2024 Teams Premium - Pretty Secure
 
AI presentation and introduction - Retrieval Augmented Generation RAG 101
AI presentation and introduction - Retrieval Augmented Generation RAG 101AI presentation and introduction - Retrieval Augmented Generation RAG 101
AI presentation and introduction - Retrieval Augmented Generation RAG 101
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
 
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdfThe Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
 
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptxUnpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
 
Demystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyDemystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John Staveley
 
Structuring Teams and Portfolios for Success
Structuring Teams and Portfolios for SuccessStructuring Teams and Portfolios for Success
Structuring Teams and Portfolios for Success
 
Buy Epson EcoTank L3210 Colour Printer Online.pdf
Buy Epson EcoTank L3210 Colour Printer Online.pdfBuy Epson EcoTank L3210 Colour Printer Online.pdf
Buy Epson EcoTank L3210 Colour Printer Online.pdf
 
What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024
 
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
 
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya HalderCustom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
 
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdfLinux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
 

Featured

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
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