What is an Abstract Class in .NET?
An abstract class is a class that cannot be instantiated directly so for our example below, you will get an error when you attempt to compile it.
To use an abstract class, the class must be inherited into a new class. See the example code below, Person line will not work but the line below will.
To get it to compile, remove the Person oPerson = new Person(); line.
abstract class Person
{
string sFirstName;
string sLastName;
}
class Employee : Person
{
string sEmpNumber;
}
class Program
{
static void Main(string[] args)
{
/* This line won’t work because Person can’t be instantiated. */
Person oPerson = new Person();
/* This line will work because Person is inherited */
Employee oEmp = new Employee();
oEmp.sFirstName = "Mister";
}
}
Last Modified : June 2023