SlideShare une entreprise Scribd logo
1  sur  2
Télécharger pour lire hors ligne
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

Contenu connexe

Dernier

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Dernier (20)

ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 

En vedette

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)
 

En vedette (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