Methods in an Abstract Class

Methods in an Abstract Class

An abstract class is a class that cannot be instantiated directly and can only be used as a base class for other classes. It is used to provide a common interface and behavior for a group of related classes while allowing each class to implement its behavior.

In this context, it is important to know what kind of methods we can use in an abstract class we using it. In this short article, we are going to know about the available types of methods:

  1. Abstract methods: These are methods that are declared in the abstract class but do not have an implementation. Abstract methods must be implemented in any derived classes. To declare an abstract method in C#, you use the «abstract» keyword before the method definition.

  2. Virtual methods: These are methods that provide a default implementation but can be overridden by derived classes if needed. To declare a virtual method in C#, you use the «virtual» keyword before the method definition.

  3. Non-virtual methods: These are methods that do not provide a default implementation and cannot be overridden by derived classes. To declare a non-virtual method in C#, you simply define the method in the abstract class without any special keywords.

  4. Static methods: These are methods that belong to the abstract class itself, rather than to any instances of the class. Static methods cannot be overridden by derived classes, but they can be called using the abstract class name. To declare a static method in C#, you use the «static» keyword before the method definition.

Here’s an example of an abstract class in C# that uses different types of methods:

public abstract class Shape
{
    public abstract double GetArea();

    public virtual string GetName()
    {
        return "Shape";
    }

    public string GetColor()
    {
        return "Red";
    }

    public static void PrintMessage()
    {
        Console.WriteLine("This is a shape.");
    }
}

public class Rectangle : Shape
{
    private double _width;
    private double _height;

    public Rectangle(double width, double height)
    {
        _width = width;
        _height = height;
    }

    public override double GetArea()
    {
        return _width * _height;
    }

    public override string GetName()
    {
        return "Rectangle";
    }
}

In this example, we define an abstract class called «Shape» that contains an abstract method called «GetArea», a virtual method called «GetName», a non-virtual method called «GetColor», and a static method called «PrintMessage». The «GetArea» method is abstract, so it must be implemented by any derived classes. The «GetName» method is virtual, so it provides a default implementation but can be overridden by derived classes. The «GetColor» method is non-virtual, so it cannot be overridden by derived classes. The «PrintMessage» method is static, so it belongs to the abstract class itself and cannot be overridden by derived classes.

We then define a derived class called «Rectangle» that inherits from «Shape» and implements the «GetArea» method. The «GetName» method is overridden to provide a different implementation than the default one provided by the «Shape» class.

Did you find this article valuable?

Support Guillermo Valenzuela's blog by becoming a sponsor. Any amount is appreciated!