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
What is the difference between an interface and an abstract class in C#?
In C#, both interfaces and abstract classes provide a way to define contracts that derived classes must follow. However, they serve different purposes and have distinct characteristics that make them suitable for different scenarios.
An interface defines a contract specifying what methods, properties, and events a class must implement, but provides no implementation itself. An abstract class can provide both abstract members (without implementation) and concrete members (with full implementation).
Interface Syntax
Following is the syntax for declaring an interface −
public interface IInterfaceName {
void MethodName();
string PropertyName { get; set; }
}
Abstract Class Syntax
Following is the syntax for declaring an abstract class −
public abstract class AbstractClassName {
public abstract void AbstractMethod();
public void ConcreteMethod() {
// implementation provided
}
}
Key Differences
| Aspect | Interface | Abstract Class |
|---|---|---|
| Implementation | No implementation (contracts only) | Can have both abstract and concrete methods |
| Inheritance | A class can implement multiple interfaces | A class can inherit only one abstract class |
| Fields | Cannot have fields or constructors | Can have fields, constructors, and destructors |
| Access Modifiers | All members are public by default | Can use public, private, protected, internal |
| Multiple Inheritance | Supports multiple inheritance | Does not support multiple inheritance |
Using Interface Example
using System;
public interface IVehicle {
void Start();
void Stop();
}
public interface IFlyable {
void Fly();
}
public class Airplane : IVehicle, IFlyable {
public void Start() {
Console.WriteLine("Airplane engine started");
}
public void Stop() {
Console.WriteLine("Airplane engine stopped");
}
public void Fly() {
Console.WriteLine("Airplane is flying");
}
}
public class Program {
public static void Main() {
Airplane plane = new Airplane();
plane.Start();
plane.Fly();
plane.Stop();
}
}
The output of the above code is −
Airplane engine started Airplane is flying Airplane engine stopped
Using Abstract Class Example
using System;
public abstract class Animal {
protected string name;
public Animal(string name) {
this.name = name;
}
public void Sleep() {
Console.WriteLine(name + " is sleeping");
}
public abstract void MakeSound();
}
public class Dog : Animal {
public Dog(string name) : base(name) { }
public override void MakeSound() {
Console.WriteLine(name + " barks: Woof!");
}
}
public class Cat : Animal {
public Cat(string name) : base(name) { }
public override void MakeSound() {
Console.WriteLine(name + " meows: Meow!");
}
}
public class Program {
public static void Main() {
Animal dog = new Dog("Buddy");
Animal cat = new Cat("Whiskers");
dog.MakeSound();
dog.Sleep();
cat.MakeSound();
cat.Sleep();
}
}
The output of the above code is −
Buddy barks: Woof! Buddy is sleeping Whiskers meows: Meow! Whiskers is sleeping
When to Use Which
Use an interface when you want to define a contract that unrelated classes can implement, or when you need multiple inheritance. Use an abstract class when you have related classes that share common functionality and you want to provide both abstract methods and concrete implementations.
Conclusion
Interfaces define pure contracts without implementation and support multiple inheritance, while abstract classes provide a mix of abstract and concrete members with single inheritance. Choose interfaces for defining capabilities across unrelated classes, and abstract classes for sharing code among related classes.
