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


In my previous article, I covered Method Overriding and Method Hiding. I recommend you to forego this article to have a look at that article before proceeding further to read this article:
Method Overriding and Method Hiding in C# .

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 which do not require implementation in the base class.

What is Abstract Class?

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

What is 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 previous article. In previous article Area() method of the base class was modified to return area of a circle using 'Method Overriding' but here we do not implement 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 maynot 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();

        }

    }
}

Output:

9
Hello in Base Class

Keypoints:

  • abstract method is implicitly a virtual method
  • signature of the method in base as well as derived class should be the same
Now what if we wanted a sort of encapsulation which required no method implementation at all? For this we have Interface.

What is Interface?
  • 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 no body
  • all the methods and properties in an Interface are by default public and abstract
  • method implementaions 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, of 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

    {

        #region Area Members

        public double CircleArea(double r)

        {

            return 3.142 * r * r;

        }

 

        public double RectangleArea(double l, double b)

        {

            return l * b;

        }

 

        #endregion

 

        public static void Main(string[] args)

        {

            B obj = new B();

            Console.WriteLine(obj.CircleArea(3));

            Console.WriteLine(obj.RectangleArea(2, 5));

            Console.ReadLine();

        }

    }
}

Output:

28.278
10

Keypoints:

  • methods in the Interface are by default public and abstract
  • signature of the method in the Interface as well as 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 overriden in the derived class if some additional functionality is required WHEREAS, Abstract Method has no implementation in the base class; it is implemented by the overriding method in the derived class.
  
In other words, Virtual method has an implementation & provides the derived class with the option of overriding it. 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 'new' keyword to the method in the derived class. We can also not override that class method unless the method has '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 comparative understanding of Overriding, Hiding, Abstract Types and Interfaces.
Tell me if there is any query.