Abstract Class Vs Interface in C#

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 have fields? Abstract class can have fields. Interfaces can’t have fields.
Implementations of its members/methods? Abstract classes can have implementations for some of their members (methods). Interface can't have implementation for any of its members.
Access modifiers? Abstract class members can have access to modifiers. Interface members can’t have access modifiers.
Default access modifiers? Abstract class members can be private by default which can be changed. Interface members can be public by default which can not be changed.
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-mentioned points by practical examples as follows.

Practical Scenarios

Example 1

How to create Abstract Classes and Interfaces?

public abstract class Customer {
}
public interface ICustomer {

}

Example 2

How to create Abstract Classes and Interfaces along with fields?

public abstract class Customer {
    int i1; // This is valid in an abstract class.
}
public interface ICustomer {
    // Fields in interfaces are implicitly public, static, and final,
    // so you cannot have a non-final instance variable like 'int i2;' here.
    int i2; // Not possible
}

Example 3

How to create Abstract Classes and Interfaces along with members?

public abstract class Customer {
    public void print1() {
        // Method implementation here
    }
}
public interface ICustomer {
    void print2(); // This is valid, but there should be no method body
}

Example 4

How is member implementation possible in Abstract Classes and Interfaces?

public abstract class Customer
{
    public abstract void Print1();
}
public interface ICustomer
{
    void Print2();
}
public class MainClass : Customer, ICustomer
{
    public override void Print1()
    {
        // Implementation for Print1
    }
    public void Print2()
    {
        // Implementation for Print2
    }
}

Example 5

How to implement abstract or interface members in inherited classes?

public abstract class Customer
{
    public abstract void Print1();
}
public interface ICustomer
{
    void Print2();
}
public class MainClass : Customer, ICustomer
{
    public override void Print1()
    {
        // Implementation for Print1
    }
    public void Print2()
    {
        // Implementation for Print2
    }
}

Example 6

How to implement inherit from another Abstract Class or Interface to an 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
{
}
// This is not allowed in C#
// public interface ICustomer3 : Customer1 // Not possible
// {
// }

Note

  • Abstract class keywords are used to create Abstract Classes.
  • interface keyword is used to create Interfaces.
    • The interface will give the compile time error “Interfaces cannot contain fields” because it will not allow fields.
    • 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.
    • The interface will give the error Abstract “interface members cannot have a definition ”
    • override keyword is used to implement abstract members.
    • public access modifier is a must for interface member implementation.

    Summary

    Now, I believe you will be able to know the key difference between Abstract Class and Interface in C#.

    • 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.


Similar Articles