Difference between Abstract Class and Interface in C#
Today we will learn the Difference between Abstract Class and Interface in C#, and I hope that you will learn a lot!
An abstract is a special type of class that we cannot instantiate, it must be inherited. It can have members like methods, properties, or fields and it can contain implemented methods (concrete methods), as well as abstract methods (methods without a body).
Whereas an interface can only declare methods and properties, but not implement them. All members of an interface don't contain any implementation and the interface also can't have fields and constructors.
KEY TAKEAWAYS:
- Abstract class and interface are both used to define a contract for classes. However, abstract classes can also provide a default implementation for derived classes, while interfaces cannot.
- Abstract classes can define constructors and destructors. Interfaces cannot define constructors or destructors.
- Abstract classes can have fields, which can store state. Interfaces cannot have fields, only properties.
- Abstract classes define base classes for related classes, while interfaces define contracts for unrelated classes.
- Choose between abstract classes and interfaces in C# based on design requirements and reusability, considering whether you need a contract-only definition (interfaces) or default implementation/base class functionality (abstract classes).
Difference between Abstract Class and Interface C#
To better understand the Abstract Class and Interface Difference in C#, let's take a look at the following comparison table:
Abstract Class | Interface |
---|---|
Can contain both abstract and non-abstract methods, properties and some other members | Can only contain methods (which cannot be implemented inside Interface), property and some other members |
Can have access modifiers for its members | C# 8.0 allows access modifiers in an interface. The default level of access for all interface members is public. |
Can provide a default implementation for derived classes | Does not provide any implementation or behavior |
Can define constructors and destructors | Cannot define constructors or destructors |
Can have fields, which can store state | Cannot have fields, only properties |
It can be used to define a base class for a family of related classes | Can be used to define a contract for a group of unrelated classes |
Now let’s learn the Difference between Interface and Abstract Class C# using some code examples.
Difference Between Interface and Abstract Class in C#
Here's a simple example in C# to illustrate some differences:
// Abstract class
public abstract class AbstractClass
{
protected int myField;
public int MyProperty { get; set; }
public void ConcreteMethod()
{
Console.WriteLine("This is a concrete method in an abstract class.");
}
public abstract void AbstractMethod(); // This abstract method must be implemented by class that inherits from this abstract class.
}
}
// Interface
public interface IMyInterface
{
int MyProperty { get; set; } // This is a property. Fields cannot be included in an interface.
void MyMethod(); // This method must be implemented by any class that uses this interface.
}
Now let’s look at a class to see the c# difference between abstract class and interface, that implements our earlier defined "IMyInterface" and another that inherits from "AbstractClass".
public class InterfaceImplementer : IMyInterface
{
private int _myProperty;
public int MyProperty
{
get { return _myProperty; }
set { _myProperty = value; }
}
public void MyMethod()
{
Console.WriteLine("This is a method implementing the interface method.");
}
}
public class AbstractClassInheritor : AbstractClass
{
public override void AbstractMethod()
{
Console.WriteLine("This is a method implementing the abstract class method.");
}
}
In the "InterfaceImplementer" class, we are required to provide implementations for all members of the "IMyInterface" interface. In this case, the "MyMethod" method and "MyProperty" property.
In the "AbstractClassInheritor" class, we are required to provide an implementation for the "AbstractMethod" method from "AbstractClass". However, we also automatically inherit the "ConcreteMethod" and "MyProperty" from "AbstractClass" and can use them as is.
It's also worth noting that a class can both inherit from an abstract class and implement multiple interfaces.
public class HybridClass : AbstractClass, IMyInterface
{
public override void AbstractMethod()
{
Console.WriteLine("This is a method implementing the abstract class method.");
}
public void MyMethod()
{
Console.WriteLine("This is a method implementing the interface method.");
}
}
In this "HybridClass", we are providing implementations for both the abstract method from "AbstractClass" and the method from "IMyInterface".
Let's consider some more examples in C# difference between abstract class and interface
// Interface
public interface IShape
{
double Area();
double Perimeter();
}
public class Rectangle : IShape
{
public double Width { get; set; }
public double Height { get; set; }
public double Area()
{
return Width * Height;
}
public double Perimeter()
{
return 2 * (Width + Height);
}
}
public class Circle : IShape
{
public double Radius { get; set; }
public double Area()
{
return Math.PI * Math.Pow(Radius, 2);
}
public double Perimeter()
{
return 2 * Math.PI * Radius;
}
}
In this example, the IShape interface defines the Area() and Perimeter() methods that must be implemented by classes that implement the interface. The Rectangle and Circle classes implementing the IShape interface and providing their own implementation of the Area() and Perimeter() methods.
// Abstract class
public abstract class Animal
{
public string Name { get; set; }
public abstract void MakeSound();
}
public class Dog : Animal
{
public override void MakeSound()
{
Console.WriteLine("Woof!");
}
}
public class Cat : Animal
{
public override void MakeSound()
{
Console.WriteLine("Meow!");
}
}
In this example, the Animal class is an abstract class with an abstract method MakeSound(). The Dog and Cat classes are derived classes that inherit from the Animal class and implement the MakeSound() method.
C# Difference between Interface and Abstract Class - Performance Comparison
When it comes to performance, abstract classes, and interfaces are both highly efficient and have negligible differences. The choice between using an abstract class or an interface should be based on the design requirements of the application, rather than on performance considerations.
When choosing between an abstract class and an interface in C#, consider the following factors:
- Design Requirements: Consider the design requirements of the application and choose the option that best meets those requirements.
- Reusability: Consider how the abstract class or interface can be reused in the other parts of the application or in future projects.
- For Just Contract: If you need to define just a contract for the classes, use Interface.
- Default Implementation: If you need to provide a default implementation for derived classes, or when you want to define a base class for a family of related classes use an abstract class.
In summary, abstract classes and interfaces are both important concepts in C# that serve different purposes.
We invite you to read our other detailed articles about Abstract class vs Interface, What is Abstract Classes in C#, C# Abstract Class, and Abstract class in C# with examples.