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
Abstract Classes in C#
An abstract class in C# includes both abstract and non-abstract methods. A class is declared abstract using the abstract keyword. You cannot instantiate an abstract class directly ? it must be inherited by a derived class that provides implementations for all abstract methods.
Syntax
Following is the syntax for declaring an abstract class and an abstract method −
public abstract class ClassName {
// Abstract method ? no body, must be overridden
public abstract void MethodName();
// Non-abstract method ? has a body, can be inherited as-is
public void RegularMethod() {
// implementation
}
}
A derived class overrides the abstract method using the override keyword −
public class DerivedClass : ClassName {
public override void MethodName() {
// implementation
}
}
Key Rules of Abstract Classes
-
An abstract class cannot be instantiated directly.
-
An abstract method has no body ? only the signature. The derived class must provide the implementation.
-
An abstract class can contain non-abstract methods with full implementations that derived classes inherit.
-
If a derived class does not implement all abstract methods, it must also be declared
abstract.
Let us see an example, wherein we have an abstract class Vehicle and abstract method display() −
public abstract class Vehicle {
public abstract void display();
}
The abstract class has derived classes: Bus, Car, and Motorcycle. The following is an implementation of the Bus derived class −
public class Bus : Vehicle {
public override void display() {
Console.WriteLine("Bus");
}
}
Example
Let us see the complete example of abstract classes in C# −
using System;
public abstract class Vehicle {
public abstract void display();
}
public class Bus : Vehicle {
public override void display() {
Console.WriteLine("Bus");
}
}
public class Car : Vehicle {
public override void display() {
Console.WriteLine("Car");
}
}
public class Motorcycle : Vehicle {
public override void display() {
Console.WriteLine("Motorcycle");
}
}
public class MyClass {
public static void Main() {
Vehicle v;
v = new Bus();
v.display();
v = new Car();
v.display();
v = new Motorcycle();
v.display();
}
}
The output of the above code is −
Bus Car Motorcycle
Abstract Class with Non-Abstract Methods
An abstract class can also have regular methods with full implementations. Derived classes inherit these methods without needing to override them −
Example
using System;
public abstract class Shape {
public abstract double Area();
public void Describe() {
Console.WriteLine("This is a shape with area: " + Area());
}
}
public class Circle : Shape {
double radius;
public Circle(double r) {
radius = r;
}
public override double Area() {
return Math.PI * radius * radius;
}
}
public class Rectangle : Shape {
double width, height;
public Rectangle(double w, double h) {
width = w;
height = h;
}
public override double Area() {
return width * height;
}
}
public class Program {
public static void Main() {
Shape s1 = new Circle(5);
s1.Describe();
Shape s2 = new Rectangle(4, 6);
s2.Describe();
}
}
The output of the above code is −
This is a shape with area: 78.5398163397448 This is a shape with area: 24
Here, Area() is abstract and must be overridden, while Describe() is a non-abstract method that is inherited and calls the overridden Area() through polymorphism.
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. |
| Can have access modifiers (public, protected, etc.). | Members are public by default. |
Conclusion
Abstract classes in C# provide a way to define a common base with both abstract methods (that must be overridden) and non-abstract methods (that can be inherited). They enforce a contract on derived classes while allowing shared functionality. Use abstract classes when related classes share common behavior but need their own specific implementations for certain methods.
