Difference Between Similar Terms and Objects

Difference between Abstract Class and Interface in C#

An abstract class looks a lot like an interface, but the concept is a bit confusing for the beginners of OOP. Conceptually, an abstract class looks just like an interface, of course, without any implementation, however they have their fair share of differences. While an abstract class can be either partially or fully implemented, an interface must be implemented completely. Well, the most notable difference between the two is that an abstract class can have default implementation, while an interface is just the definition of methods which contains only the member declarations. Let’s discuss the theoretical aspects of both in detail.

What is an Abstract Class?

An abstract class is a special type of class which acts as a base of other classes and cannot be instantiated. The implementation logic of an abstract class is provided by its derived classes. To make a class abstract, the “abstract” modifier is used which means some missing implementation needs to be implemented in the class derived from it. It contains both abstract and non-abstract members. An abstract class is intended to provide basic functionality which can be further shared and overridden by multiple derived classes. It is useful to avoid any kind of code duplication. They look very much like interfaces but with added functionality.

Difference between Abstract Class and Interface in C#

What is an Interface?

An interface, on the other hand, is not a class which contains only the signature of functionality. It’s a pattern with no implementation. Conceptually speaking, it’s just the definition of methods which contains only the declaration of members. It’s an empty shell which does not contain the implementation of its members. It’s like an abstract base class which only contains abstract members such as methods, events, indexers, properties, etc. It cannot be instantiated directly and its members can be implemented by any class. Additionally, multiple interfaces can be implemented by a class, however, a class can only inherit a single class.

Abstract Class vs. Interface : difference between Abstract Class and Interface in C#

  1. Multiple Inheritance – A class can only use one abstract class, hence multiple inheritance is not supported. An interface, on the other hand, can support multiple inheritance, which means a class can inherit any number of inheritances.
  2. Definition  of Abstract Class and Interface in C#– An abstract class is a special type of class which may contain definition with no implementation. The implementation logic is provided by its derived classes. It can have abstract as well as non-abstract methods. An interface, on the other hand, is just a pattern which cannot do anything. Technically, it’s just an empty shell.
  3. Implementation – An abstract class can contain both definition and its implementation. It’s an incomplete class which cannot be instantiated. An interface can only have the signature of the functionality without any code.
  4.  Access Modifiers – An abstract class can have several access modifiers such as subs, functions, properties, etc, while an interface is not allowed to have access modifiers and all methods must be implicitly defined as public.
  5. Homogeneity – An abstract class is used for implementations of the same type, behavior, and status, while an interface is used for implementations that share only method signatures.
  6. Declaration – An abstract class acts as a base class for all other classes so it can declare or use any variable while an interface is not allowed to declare any variables.
  7. Constructor Declaration – While an abstract class can have constructor declaration, an interface cannot have constructor declaration.
  8. Core vs. Peripheral – An abstract class is used to define the core identity of a class and can be used for objects of the same data type. An interface, on the other hand, is used to define the peripheral ability of a class.
  9. Rigid vs. Supple – An abstract class is more supple in terms of functionality, at least from a developer’s perspective, while an interface is more rigid.

Abstract Class vs. Interface: Table form

Abstract Class

Interface

An abstract class can have all access modifiers for member declaration of functions, subs, and properties. An interface is not allowed to have access modifiers for member declaration of functions, subs, properties, etc. All the members are assumed as implicitly public.
A class can at the most use only one abstract class. A class may inherit any number of interfaces.
Multiple inheritance is not supported in abstract class. An interface may support multiple inheritance.
Technically, it’s a class which may or may not contain both definition and its implementation. An interface can only have the signature of functionality so it’s basically an empty shell.
An abstract class can contain members. consts, defined methods, and method stubs. An interface can only contain methods and consts.
It defines the core identity of a class and is used to describe objects of the same data type. It is used to describe the peripheral abilities of a class.
Members of an abstract class cannot be static unless it’s a Complete Member. Members of an interface cannot be static.
It’s ideal for implementations of the same kind and common behavior. It’s better to use interfaces if several implementations share only method signatures.
It can have constructor declaration. It cannot have constructor declaration.
An abstract class has pre-defined fields and constrants. Fields cannot be defined in interfaces.
It can have both abstract and non-abstract methods. It can only have abstract methods.

Summary

What is the difference between an abstract class and an interface? This is probably one of the most common questions asked in any technical interview. You are likely to find a plethora of information about abstract classes and interfaces in any C# tutorial, however, understanding the difference between the two is quite the tough part. You can consolidate all the information you can find and still cannot get enough. Well, conceptually both are the most important terms in programming and are quite same, however, they differ a lot in terms of functionality. While an abstract class is a special type of class which acts as a base for other classes, an interface, on the other hand, is just an empty shell with only member declarations.


Search DifferenceBetween.net :

Custom Search


Help us improve. Rate this post! 1 Star2 Stars3 Stars4 Stars5 Stars
Loading...

Email This Post Email This Post : If you like this article or our site. Please spread the word. Share it with your friends/family.



Leave a Response

Please note: comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

References :


[0]Liberty, Jesse. Learning C#. Sebastopol: O’Reilly Media, 2002. Print

[1]Liberty, Jesse. Learning C#. Sebastopol: O’Reilly Media, 2002. Print

[2]Lavin, Peter. Object-Oriented PHP: Concepts, Techniques, and Code. San Francisco: No Starch Press, 2006. Print

[3]Stellman, Andrew and Jennifer Greene. Head First C#. Kolkata: Shroff Publisers & Distributors, 2008. Print

[4]"Image Credit: https://stackoverflow.com/questions/2570814/when-to-use-abstract-classes"

Articles on DifferenceBetween.net are general information, and are not intended to substitute for professional advice. The information is "AS IS", "WITH ALL FAULTS". User assumes all risk of use, damage, or injury. You agree that we have no liability for any damages.


See more about : , ,
Protected by Copyscape Plagiarism Finder