C# Difference between Abstract Class and Interface with Examples

  By : Suresh Dasari
  Posted On : 25-Apr-2023

Here, we will learn what is abstract class in c#, how to use abstract class in c#, what is interface in c#, how to use interface in c#, and the differences between abstract class and interface in c# with examples.

What is Abstract Class in C#?

In c#, abstract class is a class that is defined with abstract keyword. The abstract classes cannot be instantiated and those can be used only as a base class for other classes.

 

If we try to create an instance for the abstract class, we will get the “Cannot create an instance of the abstract type” exception as shown below.

 

C# Abstract Class Instance Creation Exception

 

The abstract class will contain both abstract methods (methods with only declarations) and non-abstract methods (methods with both declarations and definitions).

 

Following is the example of defining the abstract class in c# with abstract methods and non-abstract methods.

 

// Abstract class
public abstract class Vehicle
{
   // Abstract method
   public abstract void GetDetails();

   // Non-abstract method
   public void SetDetails()
   {
      Console.WriteLine("Set Base Vehicle Details");
   }
}

In this abstract class example, we can define both abstract and normal methods. As discussed, the abstract class cannot be instantiated, and the class that is derived from the abstract class must provide the implementation for abstract methods. So, we must need to create a derived class for the abstract class to access the abstract methods and provide the implementation for abstract methods.

 

To learn more about the abstract class, check out Abstract Class in C# with Examples.

What is Interface in C#

In c#, interface is same as the abstract class, but the only difference is the interface will contain only the declarations of methods, properties, and events that need to be implemented in derived classes.

 

In c#, interface is like a contract. The class or struct that implements an interface must provide an implementation for all the members specified in the interface definition.

 

Following is the example of creating the interface in c# and implementing the interface methods in the derived class.

 

// Interface
interface Vehicle
{
   public void GetDetails();
   public void SetDetails();
}

// Derived class
public class Car : Vehicle
{
   public virtual void GetDetails()
   {
      Console.WriteLine("Get Vehicle Details");
   }
   public virtual void SetDetails()
   {
      Console.WriteLine("Set Vehicle Details");
   }
}

In c#, interface methods, and properties are always public, we are not allowed to use any other access modifiers because all the interface methods and properties must be implemented in derived classes.

 

In c#, we are not allowed to create an instance for the interface directly, but it can be instantiated by the class or struct that implements the interface.

 

To learn more about interfaces, check out Interface in c# with examples.

Differences between Abstract Class and Interface in C#

As discussed, the abstract class and interface both are useful to define the methods and properties that need to be implemented in derived classes. The following table lists the main differences between abstract class and interface in c#.

 

Abstract ClassInterface
The abstract class is a class that is defined with abstract keyword, and it will contain both abstract (only declarations) and non-abstract (declarations and implementations) members. The interface is same as the abstract class, but the only difference is it will contain only the declarations of methods and properties that need to be implemented.
The class that is derived from the abstract class must implement all the abstract members. The class that is derived from the interface must implement all the members of the interface.
The abstract class cannot be instantiated, and it will act as a base class for other classes. The interface also cannot be instantiated directly, but it can be instantiated by the class or struct that implements an interface.
The members of the abstract class can contain different access modifiers. By default, all the members of the interface are public, and we are not allowed to use any other access modifiers.
A class can inherit only one abstract class. A class can inherit multiple interfaces.
The class that is derived from the abstract class must implement all the abstract members. The class is that is derived from the interface must provide the implementation for all the members of the interface.
The abstract class can have constructors. The interface will not accept any constructors.
The abstract class can contain the fields. The interface will not accept any fields.

To learn more about the interface, abstract classes, and abstract methods, visit our c# tutorial