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 are abstract classes in C#?
An abstract class in C# is a class that cannot be instantiated directly and contains one or more abstract methods that must be implemented by derived classes. Abstract classes provide a common base with shared functionality while requiring derived classes to implement specific methods with their own specialized behavior.
Abstract classes are declared using the abstract keyword and serve as blueprints for related classes that share common characteristics but need different implementations for certain operations.
Syntax
Following is the syntax for declaring an abstract class with abstract methods −
abstract class ClassName {
public abstract ReturnType MethodName();
// Can also have non-abstract methods
public void RegularMethod() {
// implementation
}
}
A derived class must override all abstract methods using the override keyword −
class DerivedClass : ClassName {
public override ReturnType MethodName() {
// implementation
}
}
Key Rules of Abstract Classes
- You cannot create an instance of an abstract class
- You cannot declare an abstract method outside an abstract class
- When a class is declared sealed, it cannot be inherited, abstract classes cannot be declared sealed
- Abstract classes can contain both abstract and non-abstract methods
- If a derived class does not implement all abstract methods, it must also be declared abstract
Example
using System;
abstract class Shape {
public abstract int area();
}
class Rectangle : Shape {
private int length;
private int width;
public Rectangle(int a = 0, int b = 0) {
length = a;
width = b;
Console.WriteLine("Length of Rectangle: " + length);
Console.WriteLine("Width of Rectangle: " + width);
}
public override int area() {
return (width * length);
}
}
class RectangleTester {
static void Main(string[] args) {
Rectangle r = new Rectangle(14, 8);
double a = r.area();
Console.WriteLine("Area: {0}", a);
}
}
The output of the above code is −
Length of Rectangle: 14 Width of Rectangle: 8 Area: 112
Abstract Class with Multiple Derived Classes
using System;
abstract class Animal {
public abstract void MakeSound();
public void Sleep() {
Console.WriteLine("The animal is sleeping...");
}
}
class Dog : Animal {
public override void MakeSound() {
Console.WriteLine("Dog barks: Woof! Woof!");
}
}
class Cat : Animal {
public override void MakeSound() {
Console.WriteLine("Cat meows: Meow! Meow!");
}
}
class Program {
static void Main(string[] args) {
Animal dog = new Dog();
Animal cat = new Cat();
dog.MakeSound();
dog.Sleep();
cat.MakeSound();
cat.Sleep();
}
}
The output of the above code is −
Dog barks: Woof! Woof! The animal is sleeping... Cat meows: Meow! Meow! The animal is sleeping...
Comparison: Abstract Class vs Interface
| Abstract Class | Interface |
|---|---|
| Can have both abstract and non-abstract methods | All methods are abstract by default (before C# 8.0) |
| Can have fields, constructors, and properties | Cannot have fields or constructors |
| A class can inherit only one abstract class | A class can implement multiple interfaces |
| Provides partial implementation | Provides only method signatures |
Conclusion
Abstract classes in C# provide a powerful mechanism for creating hierarchies where base classes define common structure and behavior while forcing derived classes to implement specific functionality. They are ideal when you need to share code among related classes while ensuring certain methods are customized for each implementation.
