Understanding C# Abstract Classes

c# pauseC# is an object oriented language. The foundation of any object oriented language(OOP) is laid on three pillars: Inheritance, Polymorphism & and Encapsulation. Inheritance fosters code organization and reusability. Polymorphism provides a single interface to multiple methods located in different derived classes and promotes uniformity among multiple derived classes. Encapsulation refers to managing access and organization of the data within a class. Concrete understanding of these three concepts is a gateway to developing applications in C# in particular and all other object oriented programming languages in general.

Want to know more about OOP in C#? Take this course

What is C# Abstract Class? A bigger Picture!

The concept of the C# abstract class falls under the category of the inheritance section of OOP. In order to understand abstract classes in C#, you should first understand inheritance in C#. Inheritance basically refers to organizing classes in a way that they can be managed more efficiently and the code that is common among different classes can be reused. Inheritance in C# is implemented via class hierarchy and interfaces.

Inheritance, a typical example!

The most commonly referred and probably the best example of inheritance in C# is the relationship between a parent class named “Shape” and its child classes called Rectangle, Square and a Circle. The principle of inheritance is that the methods or properties that are common between different child classes are packaged into parent classes and the method and properties that are unique among child classes are packaged exclusively in the child classes. The child classes can then inherit parent class and can access the common property.

To have a detailed understanding of inheritance, check out this course

For instance, every shape can have a DisplayArea method since every shape has an area. This method should be present in the parent class. Rectangle and Square can have angles, but a Circle has no angle. Therefore, the ‘angles’ variable should be packaged only in Square and Rectangle class and not in parent class Shape or in Circle class. Similar, radius is a variable exclusive to the Circle class; therefore, it should not be packaged into a Shape class either. The following code snippet explains this concept:

class Shape

{

public string name;

public virtual void DisplayArea()

{

Console.Write("\nThe area of the "+ name+ " is: ");

}

public Shape(string name)

{

this.name = name;

}

}

 

The shape class only contains a string member ‘name’ and a method DisplayArea since these two attributes are common in all shapes.

Rectangle class can look like this:

class Recatangle : Shape

{

public int angles;

public int length;

public int width;

public Recatangle(int angles, int length, int width, string name)

: base(name)

{

this.angles = angles;

this.length = length;

this.width = width;

}

public override void DisplayArea()

{

base.DisplayArea();

Console.Write(length * width);

}

}

 

The Rectangle class inherits the Shape class and contains three variables of type int: angles, length and width. Apart from that, the class overrides the DisplayArea method since the Shape class does not contain information on how to calculate area of the derived shape.

This is due to the fact that different shapes have different formula for calculating area therefore it cannot be packaged into parent class and child classes have to override this Display area method to implement their own formula for calculating area. The DisplayArea method would, howeve,r call the DisplayArea method of base class Shape which print a statement which can be common to all derived classes.

The Square and Circle classes have following definition:

class Square : Shape

{

public int angles;

public int sidelength;

public Square(int angles, int sidelength, string name)

: base(name)

{

this.angles = angles;

this.sidelength = sidelength;

}

public override void DisplayArea()

{

base.DisplayArea();

Console.Write(sidelength * sidelength);

}

}

class Circle : Shape

{

public int radius;

public Circle(int radius, string name):base(name)

{

this.radius = radius;

}

public override void DisplayArea()

{

base.DisplayArea();

Console.Write(3.22 * radius * radius);

}

}

 

Both Square and Circle classes implement Shape and have overridden the DisplayArea method to implement the formulas for their own area calculation.

In order to see how inheritance works and how calling DisplayArea method on the objects of the above classes, the following lines of code can be used:

Shape sp = new Shape("Shape");

Recatangle r = new Recatangle(4, 4, 8, "Rectangle");

Square s = new Square(4, 4, "Square");

Circle c = new Circle(3, "Circle");

sp.DisplayArea();

r.DisplayArea();

s.DisplayArea();

c.DisplayArea();

Where C# Abstract Classes come into play?

A shape itself is nothing, if anyone creates an object of Shape class and call its DisplayArea method, following line would be executed:

Console.Write("\nThe area of the "+ name+ " is: ");

 

This piece of code is useless and it is not displaying any area because Shape itself is nothing. If someone is asked “Hey what’s the formula of area of a shape”, the person would probably ask, “What sort of shape; square, rectangle or what?” No one can tell the area of shape. Shape itself is an abstract thing; it actually exists in the form of some concrete thing such as a rectangle or circle. This real life concept is extended to C# abstract classes.

Want to know further about C# Abstract Classes? Here is a detailed course

An abstract class is a class which cannot be instantiated or the object of such class doesn’t exist. Only classes which derive this abstract base class can be instantiated. Methods and properties inside an abstract class can be declared abstract with keyword abstract. An abstract method has no definition. All, the classes that inherit an abstract class must override the abstract methods of that class failing which compiler generates a compile time error. For instance to make Shape class abstract along with its DisplayArea method, following modifications are required:

abstract class Shape

{

public string name;

public abstract void DisplayArea();

public Shape(string name)

{

this.name = name;

}

}

 

Now, Shape class cannot be directly instantiated and Rectangle, Square and Circle must override the DisplayArea method. Also, the DisplayArea method cannot be called via ‘base’ keyword in the derived class since it doesn’t have definition. This is more logical now; every Shape can have DisplayArea method but Shape itself has no area. Actual area belongs to Rectangle (Length x Width), Square (Side* Side), Circle (Pie x (Radius ^2)). Therefore these child classes override and implement the DisplayArea method according to their respective formulas. This is basic concept of C# abstract class.

Difference between Abstract and Virtual Methods

There are two major differences between abstract methods and virtual methods.

1-     Abstract methods have no definition; Virtual methods have a definition.

2-     Derived classes MUST override abstract methods whereas it is not mandatory to override virtual methods in the derived class.

For learning more C# concepts, take this course