What Is an Abstract Class?
In C#, an abstract class is a unique class that serves as a base class from which additional classes can inherit. Although they cannot be used to create other classes directly, abstract classes can be used as a model. When you wish to offer a collection of common methods or properties that will be inherited by other classes but don’t want to permit the direct creation of the abstract class, you can use abstract classes.
Is It Possible to Create an Object From the Abstract Class in C#
The short answer to this question is no, you cannot create an object from an abstract class in C#. An abstract class cannot be instantiated directly since it is unfinished and has at least one abstract method. If you attempt to construct an object of an abstract class, a compile-time error will be displayed.
A derived class object from the abstract class can be produced, though. All abstract methods and attributes defined in an abstract class must have an implementation when the abstract class is inherited from. Once you have provided an implementation for all the abstract members, you can create an object of the derived class and use it to access the members of the abstract class.
For example, let’s say you have an abstract class called Vehicle:
{
public abstract void Drive();
}
This abstract class has one abstract method called Drive(). Let’s say you create a class called Car that inherits from the Vehicle abstract class:
{
public override void Drive()
{
Console.WriteLine("Driving the car...");
}
}
In this example, the Car class provides an implementation for the Drive() method defined in the Vehicle abstract class. Now, you can create an object of the Car class and use it to access the members of the Vehicle abstract class, like this:
myVehicle.Drive();
In this example, we make a Car class object and assign it to a Vehicle type variable. The Drive() method on the myVehicle object can then be called, which invokes the Car class’s implementation of the Drive() method.
Conclusion
In C#, an object cannot be created from an abstract class. Abstract classes are deficient and unable to be instantly created. You can make objects of derived classes that derive from the abstract class, though, and utilize them to gain access to its members. In this way, you can use abstract classes as a blueprint for creating other classes and provide a common set of methods and properties for those classes to inherit.