Difference between Abstract Class and Interface in C#
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.
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#
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- Constructor Declaration – While an abstract class can have constructor declaration, an interface cannot have constructor declaration.
- 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.
- 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
|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.|
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 :
Email This Post
: If you like this article or our site. Please spread the word. Share it with your friends/family.
Written by : Sagar Khillar.
and updated on November 30, 2017
<!-- Reference --></p><div id="che_reference"> <h2>References :</h2><br />Liberty, Jesse. Learning C#. Sebastopol: O’Reilly Media, 2002. Print<p>Liberty, Jesse. Learning C#. Sebastopol: O’Reilly Media, 2002. Print
Lavin, Peter. Object-Oriented PHP: Concepts, Techniques, and Code. San Francisco: No Starch Press, 2006. Print
Stellman, Andrew and Jennifer Greene. Head First C#. Kolkata: Shroff Publisers & Distributors, 2008. Print
”Image Credit: https://stackoverflow.com/questions/2570814/when-to-use-abstract-classes”