Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Difference between Abstract Class and Interface in C#
Both abstract classes and interfaces in C# define contracts for derived classes to implement, but they serve different purposes and have distinct characteristics. An interface defines a contract with method signatures that implementing classes must provide, while an abstract class can provide both abstract methods and concrete implementations that derived classes inherit.
Understanding the key differences helps you choose the right approach for your object-oriented design needs.
Syntax
Following is the syntax for declaring an interface −
public interface IInterfaceName {
void Method1();
int Property1 { get; set; }
}
Following is the syntax for declaring an abstract class −
public abstract class AbstractClassName {
public abstract void AbstractMethod();
public void ConcreteMethod() {
// implementation
}
}
Key Differences
| Aspect | Abstract Class | Interface |
|---|---|---|
| Inheritance | A class can inherit only one abstract class | A class can implement multiple interfaces |
| Member Fields | Can have fields, constructors, and properties | Cannot have fields or constructors (only properties, methods, events) |
| Access Modifiers | Can have public, private, protected, internal modifiers | All members are public by default |
| Method Implementation | Can have both abstract and concrete methods | Only method signatures (abstract by nature) |
| Multiple Inheritance | Not supported − single inheritance only | Supported − multiple interface implementation |
Using Abstract Classes
Abstract classes are ideal when you have related classes that share common functionality but need specific implementations for certain methods −
using System;
public abstract class Vehicle {
protected string brand;
public Vehicle(string brand) {
this.brand = brand;
}
public void StartEngine() {
Console.WriteLine(brand + " engine started");
}
public abstract void DisplayInfo();
}
public class Car : Vehicle {
public Car(string brand) : base(brand) { }
public override void DisplayInfo() {
Console.WriteLine("This is a " + brand + " car");
}
}
public class Program {
public static void Main() {
Vehicle car = new Car("Toyota");
car.StartEngine();
car.DisplayInfo();
}
}
The output of the above code is −
Toyota engine started This is a Toyota car
Using Interfaces
Interfaces are perfect for defining contracts that unrelated classes can implement, enabling multiple inheritance-like behavior −
using System;
public interface IFlyable {
void Fly();
}
public interface ISwimmable {
void Swim();
}
public class Duck : IFlyable, ISwimmable {
public void Fly() {
Console.WriteLine("Duck is flying");
}
public void Swim() {
Console.WriteLine("Duck is swimming");
}
}
public class Program {
public static void Main() {
Duck duck = new Duck();
duck.Fly();
duck.Swim();
}
}
The output of the above code is −
Duck is flying Duck is swimming
When to Use Which
Use an abstract class when −
You have closely related classes that share common code
You want to provide default implementations for some methods
You need to define fields or constructors
Use an interface when −
You want to specify a contract for unrelated classes
You need multiple inheritance capability
You want to define only method signatures without implementations
Conclusion
Abstract classes provide shared functionality with inheritance limitations, while interfaces define contracts enabling multiple inheritance. Choose abstract classes for related classes with common behavior, and interfaces for unrelated classes that need to follow the same contract.
