Abstract Classes and Pure Virtual Functions

December 23, 2015 0 By Manish Kumar

In this article we will be discussing abstract classes, and how they are implemented in C#. The first part of the article will define abstract classes and describe how to use them as tools for Object Oriented Design (OOD).

Simply stated, an abstract class is a class which does not fully represent an object. Instead, it represents a broad range of different classes of objects. However, this representation extends only to the features that those classes of objects have in
common. Thus, an abstract class provides only a partial description of its objects.

Abstract classes have the following features:
– An abstract class cannot be instantiated.
– An abstract class may contain abstract methods and accessors.
– It is not possible to modify an abstract class with the sealed (C# Reference) modifier, which means that the class cannot be inherited.
– A non-abstract class derived from an abstract class must include actual Implementations of all inherited abstract methods and accessors.

Whereas virtual keyword is used to modify a method, property, indexer or event declaration, and allow it to be overridden in a derived class.

The abstract modifier can be used with classes, methods, properties, indexers, and events. Use the abstract modifier in a class declaration to indicate that a class is intended only to be a base class of other classes. Members marked as abstract, or included in an abstract class, must be implemented by classes that derive from the abstract class.

in general, virtual does not work on classes. it is only for a class method, property, indexer or event declaration to have the possibility, to override them in a derived class.
an abstract class can’t be initiated and can more interpreted as a template of a class, which has to be derived to create an instance of it

example
***** Abstract Class *****/

public abstract class Car
{
public int NumberOfWheels = 4;

// Abstract method – MUST be overridden!
public abstract string Honk();
}

public class Peterbilt : Car
{
public Peterbilt()
{
this.NumberOfWheels =18;
}
public override string Honk()
{
return “HOOOOONK”;
}
}
public class Chevrolet : Car
{
public override string Honk()
{
return “BEEP!”;
}
}

/***** Virtual Method Class *****/
public class Person
{
public Person(string name)
{
_Name = name;
}
private string _Name;

// Virtual method – does not have to be overridden
public virtual string GetInfo()
{
return “Name: ” + _Name;
}
}
public class Employee : Person
{
private string _EmployeeID;
public Employee(string name, string employeeID)
:base(name)
{
_EmployeeID = employeeID;
}

public override string GetInfo()
{
return base.GetInfo() + “\nEmployee ID: ” + _EmployeeID ;
}
}

/***** Test Class *****/
public class TestClass
{

public void Test()
{
//Car c = new Car(); // Cannot create an instance of an abstract class
Car c = new Chevrolet();
c.Honk();

Person p1 = new Person(“Jane Doe”); // perfetctly legal
Employee p2 = new Employee(“John Smith”, “12345”);
Person p3 = p2 as Person; // downcast to Person

string s1 = p1.GetInfo(); // returns Jane Doe
string s2 = p2.GetInfo(); // returns name and ID
string s3 = p3.GetInfo(); // returns name and ID!
}
}