Abstract Class, Interface and relation to Method Overriding and Method Hiding in C#

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?

  • The 'abstract' modifier before a class indicates that the class is incomplete and intended to be used only as a base class and must be implemented in the derived class.
  • It cannot be instantiated.
  • It may contain abstract methods

What is the Abstract Method?

  • 'Abstract' modifier before a method indicates that the method (or property) does not have implementation
  • It's implicitly a virtual method
  • The implementation is provided in the overriding 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

  • Abstract method is implicitly a virtual method
  • signature of the method in the base as well as a derived class should be the same

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

What is Interface?

  • The interface can be viewed as a 100% abstract class
  • interface has no method implementation; has only method definitions without the method body
  • It has only signatures of the method and nobody
  • All the methods and properties in an Interface are, by default, public and abstract
  • method implementations are provided in the class that implements that 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

  • methods in the Interface are, by default, public and abstract
  • signature of the method in the Interface as well as the implementing class, should be the same

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.


Similar Articles