Before understanding the abstract class let’s understand the abstract methods.
Contents
Abstract Method
The abstract method is a method without implementation. i.e. Abstract method just has the declaration of the method, however, there is no implementation or body for a method. The implementation of a method is done in the derived class. The derived class must implement all the abstract methods to avoid compile-time errors.
C# Syntax
<access-modifier> abstract <return-type> MethodName (parameters)
To declare an abstract method, we need to add an abstract keyword in the declaration. (In general, when we declare a method, we must add body to a method, when we don’t want to add body to a method then we need to define the method using abstract keyword.)
example
public abstract void Print(string msg);
Abstract Method only declared inside the abstract classes. The abstract method declared in the non-abstract classes will lead to a compile-time error.
Abstract Class
Abstract Class is a class that contains abstract as well as non-abstract methods. So, we can say that abstract classes is a partially implemented class which further can be used for developing some of the operations of an object which requires specific implementations in the derived classes.
C# Syntax
<access-modifier> abstract class ClassName
{
//class implementations
}
To declare abstract classes, we must use abstract keyword. Without abstract keyword, we cannot create abstract classes
Example:
public abstract class TestClass
{
public abstract void Print(string msg);
public void PrintException(string msg)
{
}
}
Non-abstract classes can contain only non-abstract methods, whereas abstract classes can have both abstract as well as non-abstract methods. So, if there is a derived class that inherits abstract classes containing both abstract and non-abstract methods in it, then derived class will be able to use non-abstract methods of the abstract classes and implement abstract methods from abstract classes as it is compulsory.
Another important point to remember is that we cannot create an instance of abstract classes, but we can create a reference to it.
Below is a C# code snippet example to understand more about abstract classes.
public abstract class MathOperation
{
public abstract double Calculate(int ii);
}
public class SquareCalculation : MathOperation
{
public override double Calculate(int ii)
{
return Math.Sqrt(ii);
}
}
public class CubeCalculation : MathOperation
{
public override double Calculate(int ii)
{
return ii * ii * ii;
}
}
Main Program
class Program
{
static void Main()
{
MathOperation calculation = new CubeCalculation();
Console.WriteLine(calculation.Calculate(3));
Console.ReadLine();
}
}
Why we need an abstract class?
Abstract classes can have abstract as well as non-abstract methods hence it likes the partial implementation of classes. Since it’s incomplete we can provide default functionality for the derived class. So that the common behavior will be the same across the derived class. In the future, if there is any change in base class then we can easily change the functionality of base class and all derived class will be using the new functionality. If in base class instead of
Without an abstract classes, we could have to provide dummy implementations of the methods which we intend to override. But then there will be a risk of forgetting the implementation.
Points to remember
- Abstract classes can contain abstract members as well as non-abstract members.
- Abstract classes can have constructors.
- Abstract classes can implement a property.
- It does not support Multiple inheritances as a class can inherit from only one Abstract Class.
- We can add access specifiers in abstract classes.
- Abstract classes can contain constants and fields.
Helpful Resources
- Abstract classes Interview questions
- Abstract classes Multiple Choice Questions (MCQ) Quiz.
- Interface
- Inheritance
- Abstraction
- Encapsulation