Abstract Class in C#
- An abstract class is always used as a base class for all the derived classes.
- We can't create objects of an abstract class.
- Abstract classes contain both abstract members as well as non-abstract members.
- When there is a scenario where you want to split the default functionality through the device/child/subclasses, then in that case, we can go for the abstract class.
- To mark a class as an abstract class, we need to make use of the abstract keyword.
- We can have a partial implementation in an abstract class.
Syntax
accessspecifier abstract class ClassName
{
}
Abstract Class with Partial Implementation Example
public abstract class Mobile
{
public abstract void SMSFunctionality();
public abstract void PlayStore();
public void PrintDetails()
{
Console.WriteLine("Non-abstract method in abstract class");
}
}
Explanation. In the above example, we have created an abstract class called Mobile which will have 2 abstract methods (SMSFunctionality, PlayStore) & 1 Non-abstract method (PrintDetails). So you can see for the non-abstract method, we have a method body. You will not get any build errors while doing the compilation.
Real-Time Example
Program.cs
public abstract class Mobile
{
public abstract void SMSFuntionality();
public abstract void PlayStore();
}
public class Samsung : Mobile
{
public override void SMSFuntionality()
{
Console.WriteLine("SMS Functionality of Samsung Class");
}
public override void PlayStore()
{
Console.WriteLine("PlayStore Functionality of Samsung Class");
}
}
public class Nokia : Mobile
{
public override void SMSFuntionality()
{
Console.WriteLine("SMS Functionality of Nokia Class");
}
public override void PlayStore()
{
Console.WriteLine("PlayStore Functionality of Nokia Class");
}
}
Main Method()
static void Main(string[] args)
{
Samsung objSamsung = new Samsung();
objSamsung.SMSFuntionality();
objSamsung.PlayStore();
Nokia objNokia = new Nokia();
objNokia.SMSFuntionality();
objNokia.PlayStore();
Console.ReadLine();
}
Explanation
In the above example, we have taken real real-time example of a Mobile phone. In mobile, we have a different common feature that is used in different mobile brands i.e. Nokia, Samsung, LG etc. In every brand, we will have a common feature i.e. SMS Functionality, Play Store, etc. So as per the code, we have created a Mobile abstract class & we have defined 2 abstract methods. As per the concept, the abstract methods need to be overridden & provide the method implementation in the derived class, which is going to inherit the abstract base class. In this case, we have 2 derive/child classes, i.e. Samsung & Nokia, where we have inherited the base abstract class, i.e. Mobile & implemented their own logic in the method.
This is one of the best examples where if you have some common functionality that you want to share across the derived classes, then you can go for the abstract class.
Example 2
using System;
class HelloWorld
{
static void Main()
{
Circle C = new Circle();
C.Area();
Triangle T = new Triangle();
T.Area();
Console.ReadLine();
}
}
public abstract class Shape
{
public abstract void Area();
}
public class Circle : Shape
{
double radius;
public override void Area()
{
Console.WriteLine("Enter the Radius");
radius = double.Parse(Console.ReadLine());
Console.WriteLine("Area = " + (3.141 * radius * radius));
}
}
public class Triangle : Shape
{
double b, h;
public override void Area()
{
Console.WriteLine("Please Enter base & height");
b = double.Parse(Console.ReadLine());
h = double.Parse(Console.ReadLine());
Console.WriteLine("Area = " + (0.5 * b * h));
}
}
Explanation
In the above code, we have taken the example of Area for a Circle and triangle. So for both, the Area formula will be different, but it is a common functionality. So we have created an abstract class Shape which will contain 1 abstract method (Area). Circle, Triangle derive class inherited the Shape abstract class, overridden the Area method & provided their own logic for the Area.
This is one more example where the abstract class is preferable.
Conclusion
In this article, I have explained what is an abstract class in C#, along with real-time examples.
Happy Learning !.