Junior — Middle
How is inheritance implemented for abstract classes in object-oriented programming?
sobes.tech AI
Answer from AI
In C#, an abstract class is a class that cannot be instantiated directly and can contain abstract methods (without implementation). Inheritance from an abstract class is implemented using the : keyword, and the derived class must implement all abstract methods.
Example:
abstract class Animal
{
public abstract void MakeSound();
}
class Dog : Animal
{
public override void MakeSound()
{
Console.WriteLine("Bark");
}
}
Here, the Dog class inherits from Animal and implements the abstract method MakeSound. This allows creating class hierarchies with a common base and different specific implementations.