What is an abstract method?
An abstract method is a method declared within an abstract class in object-oriented programming (OOP) that doesn't have an implementation in the class where it's declared. Instead, the implementation is provided by the concrete (non-abstract) subclasses that inherit from the abstract class. Abstract methods are used to define a method signature and functionality that must be present in all derived classes, but the actual implementation can vary based on the specific requirements of each subclass.
Key characteristics of abstract methods:
-
Declaration Only: Abstract methods are declared with a method signature (return type, method name, and parameter list), followed by a semicolon. They do not contain a method body.
-
No Method Body: Since abstract methods lack a method body, they don't contain any code or logic. They serve as placeholders for methods that must be implemented in derived classes.
-
Must Be in Abstract Class: Abstract methods can only exist within an abstract class. An abstract class is a class that contains at least one abstract method and cannot be instantiated directly.
-
Required Implementation: Any class that inherits from an abstract class containing abstract methods must provide concrete implementations for those abstract methods. Failure to do so will result in a compilation error.
-
Virtual by Nature: Abstract methods are inherently virtual in nature. This means that they are designed to be overridden by derived classes, and the 'virtual' keyword is not required for abstract methods.
-
Access Modifiers: Abstract methods can have access modifiers (such as 'public', 'protected', etc.) that determine their visibility and accessibility in derived classes.
Here's an example of an abstract class with an abstract method in C#:
public abstract class Shape
{
public abstract double CalculateArea(); // Abstract method
public void Display()
{
Console.WriteLine("This is a shape.");
}
}
public class Circle : Shape
{
private double radius;
public Circle(double radius)
{
this.radius = radius;
}
public override double CalculateArea()
{
return Math.PI * radius * radius;
}
}
public class Rectangle : Shape
{
private double width;
private double height;
public Rectangle(double width, double height)
{
this.width = width;
this.height = height;
}
public override double CalculateArea()
{
return width * height;
}
}
public class Program
{
static void Main(string[] args)
{
Shape circle = new Circle(5);
double circleArea = circle.CalculateArea();
circle.Display(); // Output: This is a shape.
Console.WriteLine("Circle area: " + circleArea); // Output: Circle area: 78.53981633974483
Shape rectangle = new Rectangle(4, 6);
double rectangleArea = rectangle.CalculateArea();
rectangle.Display(); // Output: This is a shape.
Console.WriteLine("Rectangle area: " + rectangleArea); // Output: Rectangle area: 24
}
}
In this example, the 'Shape' class is an abstract class with an abstract method 'CalculateArea()'. The 'CalculateArea()' method does not have an implementation in the 'Shape' class because it is abstract. The 'Circle' and 'Rectangle' classes derive from the 'Shape' class and provide their own implementations of the 'CalculateArea()' method.