What Is an Abstract Class?
An abstract class in C# is a class that cannot be instantiated. It is intended to be a base class that other classes inherit from. Abstract classes can contain both abstract members (without implementation) and concrete members (with implementation).
Key Features of Abstract Classes in C#
- Cannot be instantiated directly.
- May contain abstract methods, properties, or indexers.
- Can contain fully implemented methods.
- Used to enforce a base structure for derived classes.
Example in C#
abstract class Animal
{
public abstract void MakeSound(); // abstract method
public void Sleep() // normal method
{
Console.WriteLine("Sleeping...");
}
}
class Dog : Animal
{
public override void MakeSound()
{
Console.WriteLine("Bark!");
}
}
In this example, Animal is an abstract class.
You cannot create new Animal(), but you can create Dog,
which inherits and implements the abstract method.
No comments:
Post a Comment