What is meant by an Abstract Class? | C# interview Questions 17

An abstract class in C# is a class that cannot be instantiated directly and is designed to be a base class for other classes. It can contain abstract methods (methods without a body) that must be implemented by derived classes, as well as concrete methods with implementation. Abstract classes are used to provide a common definition of a base class that multiple derived classes can share.

Key points about abstract classes:

Cannot be instantiated: You cannot create an object of an abstract class.
Can contain both abstract and concrete methods: Abstract methods must be implemented in derived classes.
Supports inheritance: Used to define a common interface for a group of related classes.
May include fields, properties, and methods: Both abstract and fully implemented members can exist.
Example:

csharp
Copy code
public abstract class Animal
{
public abstract void MakeSound(); // Abstract method

public void Sleep() // Concrete method
{
Console.WriteLine(“Sleeping…”);
}
}

public class Dog : Animal
{
public override void MakeSound()
{
Console.WriteLine(“Bark”);
}
}
In this example, Animal is an abstract class with an abstract method MakeSound and a concrete method Sleep. The Dog class inherits from Animal and provides an implementation for the MakeSound method.

Leave a Reply

Your email address will not be published. Required fields are marked *