Sobes.tech
Junior — Middle

Can you explain what abstract methods are in C# and how they differ from regular methods?

sobes.tech AI

Answer from AI

Abstract methods in C# are methods declared in an abstract class without an implementation. They serve as a template that must be implemented by all derived classes. Unlike regular methods, abstract methods do not have a body (implementation) in the base class and require overriding in subclasses.

Example:

abstract class Animal
{
    public abstract void MakeSound(); // abstract method
}

class Dog : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("Woof");
    }
}

Here, MakeSound is an abstract method that must be implemented in the Dog class. Regular methods have an implementation and can be called directly, whereas abstract methods are used to define a mandatory interface for inheritors.