Introduction

An abstract class provides a nice mechanism to implement abstraction as it encapsulates abstract as well as concrete methods within its scope.

We had virtual methods that needed implementation in the base class and allowed optionally to override that method in the derived class if some additional functionality was required. But Abstract classes can have abstract methods that do not require implementation in the base class.

What is Abstract Class?

What is the Abstract Method?

Consider the following example in which the scenario is different from the previous article. In the previous article Area() method of the base class was modified to return the area of a circle using 'Method Overriding', but here we do not implement the Area() method in the base class at all. We just provide its signature in the base class and implement it in the derived class. In addition, we can also encapsulate a non-abstract method in the abstract class, which here is Hello().

using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
    public abstract class AbstractDemo // it may or may not have abstract methods and it cannot be instantiated
    {
        public abstract double Area(double r); // abstract method

        public void Hello() // non-abstract method
        {
            Console.WriteLine("Hello in Base Class");
        }
    }
    public class B : AbstractDemo
    {
        public override double Area(double r)
        {
            return r * r;
        }
    }
    public class Test
    {
        public static void Main(string[] args)
        {
            B obj = new B();
            Console.WriteLine(obj.Area(3));
            obj.Hello();
            Console.ReadLine();
        }
    }
}

Key points

Now what if we wanted a sort of encapsulation that required no method implementation at all? For this, we have Interface.

What is Interface?

In broad terms, Interface may be considered as a Contract between the Client and Service Providers regarding the functionality of the System (or set of requirements) while implementing data hiding (abstraction) of as to how these functionalities shall be implemented.

Consider the following example, which has an Interface named 'Area' that provides only definitions to the methods CircleArea() and RectangleArea(). It is Class B that implements interface 'Area' and actually provides implementations to these two methods.

using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
    interface Area
    {
        double CircleArea(double r);
        double RectangleArea(double l, double b);
    }
    Class B : Area
    {
        public double CircleArea(double r)
        {
            return 3.142 * r * r;
        }
        public double RectangleArea(double l, double b)
        {
            return l * b;
        }
        public static void Main(string[] args)
        {
            B obj = new B();
            Console.WriteLine(obj.CircleArea(3));
            Console.WriteLine(obj.RectangleArea(2, 5));
            Console.ReadLine();
        }
    }
}

Key points

Difference between Abstract Method & Virtual Method

A virtual method must have its implementation in the base class and may optionally be overridden in the derived d class if some additional functionality is required. WHEREAS, the Abstract Method has no implementation in the base class; it is implemented by the overriding method in the derived class.

In other words, the Virtual method has an implementation & provides the derived class with the option of overriding it. The abstract method does not provide an implementation & forces the derived class to override the method.

Note.If any class method does not have a virtual, abstract or override keyword and we attempt to have its new implementation in any derived class, a warning is generated to supply a 'new' keyword to the method in the derived class. We can also not override that class method unless the method has a ' virtual' keyword in the base class.

Hope my efforts helped you to understand all these concepts at the same place without referring here and there for a comparative understanding of Overriding, Hiding, and Abstract Types and Interfaces.