Article Overview
- Background
- Difference between Abstract Class and Interface in C#
- Practical Scenarios
- Summary
Background
Here is a list of the key differences between Abstract Class and Interface in C#. This article will be useful to both beginners and professional C# developers.
Difference between Abstract Class and Interface in C#
Category |
Abstract Class |
Interface |
What is it? |
Abstract doesn’t provide full abstraction. |
Interface provides full abstraction. |
How to declare/create? |
abstract class is used to create Abstract classes. |
interface is used to create Interfaces. |
Can it has fields? |
Abstract class can have fields. |
Interfaces can’t have fields. |
Implementations of its members/methods? |
Abstract classes can have implementations for some of its members (methods). |
Interface can't have implementation for any of its members. |
Access modifiers? |
Abstract class members can have access modifiers. |
Interface members can’t have access modifiers. |
Default access modifiers? |
Abstract class members can be private by default which can be change. |
Interface members can be public by default which can not be change. |
Is it inherited from another Abstract Class or Interface?
|
Abstract class can inherit from another abstract class or another interface.
|
Interface can inherit from another interface only and cannot inherit from an abstract class. |
Inherit from another Abstract Class or Interface?
|
Abstract class can inherit from another abstract class or another interface. |
Interface can inherit from another interface only and cannot inherit from an abstract class. |
Now, let us see the above-mentionded points by practical examples as following.
Practical Scenarios
Example 1
How to create Abstract Classes and Interfaces?
- public abstract class Customer
- {
- }
-
- public interface ICustomer
- {
- }
Note
- abstract class keywords are used to create Abstract Classes.
- interface keyword is used to create Interfaces.
Example 2
How to create Abstract Classes and Interfaces along with fields?
- public abstract class Customer
- {
- int i1;
- }
-
- public interface ICustomer
- {
- int i2;
- }
Note
- Interface will give compile time error “Interfaces cannot contain fields” because it will not allow fields.
Example 3
How to create Abstract Classes and Interfaces along with members?
- public abstract class Customer
- {
- public abstract void Print1();
- }
-
- public interface ICustomer
- {
- void Print2();
- }
Note
- Abstract keyword is required for members of Abstract Classes. You can use access modifiers. By default it will be private.
- For members of Interfaces you can’t use access modifiers. By default it will be public.
Example 4
How ismember implementation possible in Abstract Classes and Interfaces?
- public abstract class Customer
- {
- public void Print1()
- {
- }
- }
-
- public interface ICustomer
- {
- void Print2()
- {
- }
- }
Note
- Interface will give error Abstract “interface members cannot have a definition”
Example 5
How to implement abstract or interface members in inherited class?
- public abstract class Customer
- {
- public abstract void Print1();
- }
-
- public interface ICustomer
- {
- void Print2();
- }
-
- public class MainClass : Customer, ICustomer
- {
- public override void Print1()
- {
- }
- public void Print2()
- {
- }
- }
Note
- override keyword is used to implement abstract members.
- public access modifier is must for interface member implementation.
Example 6
How to implement inherit from another Abstract Class or Interface to Abstract Class or Interface?
- public abstract class Customer1
- {
- }
-
- public abstract class Customer2 : Customer1
- {
- }
-
- public abstract class Customer3 : Customer1, ICustomer1
- {
- }
-
- public interface ICustomer1
- {
- }
-
- public interface ICustomer2 : ICustomer1
- {
- }
-
- public interface ICustomer3 : Customer1
- {
- }
Note
- An interface can inherit from another interface only and cannot inherit from an abstract class, whereas an abstract class can inherit from another abstract class or another interface.
- Therefore, interface ICustomer3 can not inherit to abstract class Customer1.
Summary
Now, I believe you will be able to know the key difference between Abstract Class and Interface in C#.