Abstract Class Versus Interface

Introduction

This article describes the concepts of Abstract class and Interface.

Abstract class

  • Abstract class is a class which can’t be instantiated and should be inherited to access the members.

  • It forces the subclass to follow certain standards and rules as in base class [to follow the filed set and methods as defined in Abstract Class].

  • An abstract class can contain either abstract methods or non-abstract methods.

  • Non-abstract method is a normal method which has implementation. Abstract Methods do not have any implementation , it has only signature in the abstract class, which needs to be implemented in derived class.

  • The purpose of an abstract class is to provide abase class definition for how a set of derived classes will work and then allow the programmers to fill the implementation in the derived classes.

Interface

  • Interface is not a class; it is an entity that gives basic signature of the method but not the definition.

  • As same as “abstract class” It forces the subclass to follow certain standards and rules in derived class [ to follow the same signature as defined in interface].

  • When we create an interface, we are basically creating a set of methods without any implementation that must be implemented in the derived classes.

Differences

Feature Abstract class Interface
Multiple inheritance A class may inherit only one abstract class. A class may inherit several interfaces.
Fields and Constants An abstract class can have fields and constants defined No fields can be defined in interfaces
Default implementation An abstract class can provide complete, default code and/or just the details that have to be overridden. An interface cannot provide any code, just the signature.
Adding functionality If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly. If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method.

When to use “Abstract Class” and when to use “Interface”?

To explain, we willconsider a scenario of STUDENT entity. In that, we will discuss what can beconsider as “Abstract” class and what can be consider as “Interface”

STUDENT ENTITY

Let us consider, we have two type Students, Student 1 and Student 2.

Properties of Student 1 Properties of Student 2
Fields:

   – ID, Name, Address, DOB, Class, Marks

Functions:

   – Need to display details of him in any report
   – Need to calculate Rank as per his marks obtain
   – Paying fees by cash  
   – Coming in School Bus
   – Talented in Dance and Singing
Fields:

   – ID, Name, Address, DOB, Class, Marks

Functions:

   – Need to display details of him in any report
   – Need to calculate Rank as per his marks obtain
   – Paying fees by card
   – Coming in own Vehicle
   – Talented in Yoga

Analysing all fields andfunctions in both type of Students. We have segregated the fields and functionsas four sections as below:

  1. Common fields for a student : It is compulsory for all students.

    ID, Name, Address, DOB, Class, Marks

  2. Functionalities of a student [Performed Same Manner]: There will be certain set of functions that will be performed compulsory but in same manner irrespective of Student.

    Rank Calculation, Displaying Details of Student

  3. Functionalities of a student [Performed Unique Way] : There will be certain set of functions that will be performed compulsory but in unique way with respect to Student.

    Fees Payment, Mode of transport

  4. Talent of a student: It is not compulsory. Some may have one talent. Some may have many talents. Some may have none. We cannot define this

    Dance, Yoga, Singing.

With this Information above, let’s see how to design classfor Student Entity.

Scenario: Considering Student1 and Student2 as two separate Class.

It is not best way if we create Student1 and Student2 as two separate classes and implement all fields and functions as needed for them, because in later period, if “Student3” comes in to the picture, we need towrite code for all fields and functionalities again and again.

Results in Duplication of code

There should be some class which contains all basic fields and functions. Student1, Student2 should inherit this base class

We will Consider “Base STUDENT” as one class which has all Fields Set and Methods . Student1 and Student 2 are two different classes derived from “Base STUDENT” Class.

How to Create the “Base STUDENT”? Normal Class, Abstract or Interface??

Scenario 1: Considering “BaseStudent” as Normal Class.

  1. Declare all fields needed

  2. Implement Functionalities of a student Performed Same Manner] - Which is going to be same in all derived class.

  3. Implement Functionalities of a student [Performed Unique Way] – But this going to differs in each and every derived class.

    Note: Any way this function is going to be overridden in derived class.

    In normal Class, we cannot define just function; we need to implement the function.

  4. Implement function for Dance, Singing and Yoga

    Note: Dance, Singing and Yoga is not common for all derived class, some have one and some may have none.

    Results in unnecessary coding.

Scenario 2: Considering “BaseStudent”as Interface.

  1. Implement Functionalities of a student [Performed Same Manner] - Which is going to be same in all derived class.

    Note: Implementation for methods is not accepted in Interface.

    In all derived class, same set of implementation has to be done.

    Results in Duplication of code.

Scenario 3: Considering “BaseStudent”as “Abstract Class”

  1. Declare all fields needed.

  2. Implement Common functions which is going to be same in all derived class.

    Implement this as Normal Method.

  3. Implement Common Function–but this going to differs in each and every derived class.

    Implement this as Abstract Method, so that it can be overridden.

  4. Implement function for Dance, Singing and Yoga

Question– How to implement this method? Normal or Abstract Method?

If this has to implemented as Normal Method.

This is not going to be same for all in derived class.

If this has to be declared as Abstract Method.

For Student 1, We need to implement Yoga unnecessarily.

For Student 2, We need to implement Dance and Singing unnecessarily.

To overcome this problem, let’s declare Dance, Singing, and Yoga as individual Interface.

So that, it can be implemented in their own way in derived class.

Question –Why we should not declare Dance, Sing and Yoga as individual Abstract class.

Multiple Inheritances is not accepted for class, but for interface it is accepted.

table

If in later period, Student of some other types come in to the picture, we can create that as separate class , inherits the Abstract Base Student and interface as needed.

Coding:

Create a new "console application".

Abstract BaseStudent: Create a class "AbsStudent" as Abstract Class.

  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Text;    
  5. using System.Threading.Tasks;    
  6.     
  7. namespace OOPSConcepts    
  8. {    
  9.     public abstract class  AbsStudent     
  10.     {    
  11.     
  12.         protected string Name;    
  13.         protected string ID;    
  14.         protected string Class;    
  15.         protected string Address;    
  16.     
  17.         public abstract string stuName    
  18.         {    
  19.             get;    
  20.             set;    
  21.         }    
  22.         public abstract string stuID    
  23.         {    
  24.             get;    
  25.             set;    
  26.         }    
  27.         public abstract string stuClass    
  28.         {    
  29.             get;    
  30.             set;    
  31.         }    
  32.         public abstract string stuAddress    
  33.         {    
  34.             get;    
  35.             set;    
  36.         }    
  37.             
  38.     
  39.         public abstract void PaysFees();    
  40.         public abstract void ModeOfTransport();    
  41.     
  42.         public void DisplayDetails(string Name, string ID, string Class, string Address)    
  43.         {    
  44.             Console.WriteLine("Name : " + Name);    
  45.             Console.WriteLine("ID : "+ ID);    
  46.             Console.WriteLine("Class : " + Class);    
  47.             Console.WriteLine("Address : " + Address);    
  48.                 
  49.         }    
  50.     }    

Interfaces - Create IDance, ISing, IYoga as Interfaces.

  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Text;    
  5. using System.Threading.Tasks;    
  6.     
  7. namespace OOPSConcepts    
  8. {    
  9.     interface IDance    
  10.     {    
  11.         void Dance();    
  12.     }    
  13.     interface ISing    
  14.     {    
  15.         void Sing();    
  16.     }    
  17.     interface IYoga    
  18.     {    
  19.         void Yoga();    
  20.     }    

Student 1 - Create a "Student1" as a class and inherits Abstract Class, Dance Interface and Sing Interface.

  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Text;    
  5. using System.Threading.Tasks;    
  6.     
  7. namespace OOPSConcepts    
  8. {    
  9.     public class Student1 : AbsStudent,IDance,ISing    
  10.     {    
  11.     
  12.            
  13.        public override  string stuName    
  14.        {    
  15.            get    
  16.            {    
  17.                return Name;    
  18.            }    
  19.            set    
  20.            {    
  21.                Name = value;    
  22.            }    
  23.        }    
  24.        public override string stuID    
  25.        {    
  26.            get    
  27.            {    
  28.                return ID;    
  29.            }    
  30.            set    
  31.            {    
  32.                ID = value;    
  33.            }    
  34.        }    
  35.        public override string stuClass    
  36.        {    
  37.            get    
  38.            {    
  39.                return Class;    
  40.            }    
  41.            set    
  42.            {    
  43.                Class = value;    
  44.            }    
  45.        }    
  46.        public override string stuAddress    
  47.        {    
  48.            get    
  49.            {    
  50.                return Address;    
  51.            }    
  52.            set    
  53.            {    
  54.                Address = value;    
  55.            }    
  56.        }    
  57.           
  58.        public override void PaysFees()    
  59.        {    
  60.            Console.WriteLine("Payment by Cash");    
  61.        }    
  62.        public override void ModeOfTransport()    
  63.        {    
  64.            Console.WriteLine("Coming in Bus");    
  65.        }    
  66.     
  67.       public void Dance()    
  68.        {    
  69.            Console.WriteLine("Talented in Dance");    
  70.                
  71.        }    
  72.     
  73.        public void Sing()    
  74.        {    
  75.            Console.WriteLine("Talented in Singing");    
  76.        }    
  77.     
  78.            
  79.     }    
  80.     
  81.   }  

Student 2 - Create a "Student2" as a class and inherits Abstract Class and Yoga Interface.

  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Text;    
  5. using System.Threading.Tasks;    
  6.     
  7. namespace OOPSConcepts    
  8. {    
  9.     class Student2: AbsStudent,IYoga    
  10.     {    
  11.     
  12.            
  13.        public override  string stuName    
  14.        {    
  15.            get    
  16.            {    
  17.                return Name;    
  18.            }    
  19.            set    
  20.            {    
  21.                Name = value;    
  22.            }    
  23.        }    
  24.        public override string stuID    
  25.        {    
  26.            get    
  27.            {    
  28.                return ID;    
  29.            }    
  30.            set    
  31.            {    
  32.                ID = value;    
  33.            }    
  34.        }    
  35.        public override string stuClass    
  36.        {    
  37.            get    
  38.            {    
  39.                return Class;    
  40.            }    
  41.            set    
  42.            {    
  43.                Class = value;    
  44.            }    
  45.        }    
  46.        public override string stuAddress    
  47.        {    
  48.            get    
  49.            {    
  50.                return Address;    
  51.            }    
  52.            set    
  53.            {    
  54.                Address = value;    
  55.            }    
  56.        }    
  57.           
  58.        public override void PaysFees()    
  59.        {    
  60.            Console.WriteLine("Payment by Card");    
  61.        }    
  62.        public override void ModeOfTransport()    
  63.        {    
  64.            Console.WriteLine("Coming in Own");    
  65.        }    
  66.     
  67.       public void Yoga()    
  68.        {    
  69.            Console.WriteLine("Talented in Yoga");    
  70.               
  71.        }    
  72.        
  73.            
  74.     }    
  75.     
  76.   } 

Client Class: Create a new class To test the code.

  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Text;    
  5. using System.Threading.Tasks;    
  6.     
  7. namespace OOPSConcepts    
  8. {    
  9.     class ClientClass    
  10.     {    
  11.         static void Main(string[] args)    
  12.         {    
  13.             Student1 objStu1 = new Student1();    
  14.             objStu1.stuID = "Eng001";    
  15.             objStu1.stuName = "Ram";    
  16.             objStu1.stuAddress = "Chennai";    
  17.             objStu1.stuClass = "6th";    
  18.          Console.WriteLine("*****************************");    
  19.             objStu1.DisplayDetails(objStu1.stuName, objStu1.stuID, objStu1.stuClass, objStu1.stuAddress);    
  20.             objStu1.PaysFees();    
  21.             objStu1.ModeOfTransport();    
  22.             objStu1.Dance();    
  23.             objStu1.Sing();    
  24.          Console.WriteLine("****************************");    
  25.             Student2 objStu2 = new Student2();    
  26.             objStu2.stuID = "Med0001";    
  27.             objStu2.stuName = "Ramesh";    
  28.             objStu2.stuAddress = "Madurai";    
  29.             objStu2.stuClass = "8th";    
  30.             Console.WriteLine("******************************");    
  31.             objStu2.DisplayDetails(objStu2.stuName, objStu2.stuID, objStu2.stuClass, objStu2.stuAddress);    
  32.             objStu2.PaysFees();    
  33.             objStu2.ModeOfTransport();    
  34.             objStu2.Yoga();    
  35.             Console.WriteLine("*********************************");    
  36.              Console.ReadLine();    
  37.         }    
  38.             
  39.     }    
  40. }  

Output

Output


Similar Articles