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:
- Common fields for a student : It is compulsory for all students.
ID, Name, Address, DOB, Class, Marks
- 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
- 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
- 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.
- Declare all fields needed
- Implement Functionalities of a student Performed Same Manner] - Which is going to be same in all derived class.
- 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.
- 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.
- 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”
- Declare all fields needed.
- Implement Common functions which is going to be same in all derived class.
Implement this as Normal Method.
- 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.
- 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.
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.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace OOPSConcepts
- {
- public abstract class AbsStudent
- {
-
- protected string Name;
- protected string ID;
- protected string Class;
- protected string Address;
-
- public abstract string stuName
- {
- get;
- set;
- }
- public abstract string stuID
- {
- get;
- set;
- }
- public abstract string stuClass
- {
- get;
- set;
- }
- public abstract string stuAddress
- {
- get;
- set;
- }
-
-
- public abstract void PaysFees();
- public abstract void ModeOfTransport();
-
- public void DisplayDetails(string Name, string ID, string Class, string Address)
- {
- Console.WriteLine("Name : " + Name);
- Console.WriteLine("ID : "+ ID);
- Console.WriteLine("Class : " + Class);
- Console.WriteLine("Address : " + Address);
-
- }
- }
- }
Interfaces - Create IDance, ISing, IYoga as Interfaces.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace OOPSConcepts
- {
- interface IDance
- {
- void Dance();
- }
- interface ISing
- {
- void Sing();
- }
- interface IYoga
- {
- void Yoga();
- }
- }
Student 1 - Create a "Student1" as a class and inherits Abstract Class, Dance Interface and Sing Interface.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace OOPSConcepts
- {
- public class Student1 : AbsStudent,IDance,ISing
- {
-
-
- public override string stuName
- {
- get
- {
- return Name;
- }
- set
- {
- Name = value;
- }
- }
- public override string stuID
- {
- get
- {
- return ID;
- }
- set
- {
- ID = value;
- }
- }
- public override string stuClass
- {
- get
- {
- return Class;
- }
- set
- {
- Class = value;
- }
- }
- public override string stuAddress
- {
- get
- {
- return Address;
- }
- set
- {
- Address = value;
- }
- }
-
- public override void PaysFees()
- {
- Console.WriteLine("Payment by Cash");
- }
- public override void ModeOfTransport()
- {
- Console.WriteLine("Coming in Bus");
- }
-
- public void Dance()
- {
- Console.WriteLine("Talented in Dance");
-
- }
-
- public void Sing()
- {
- Console.WriteLine("Talented in Singing");
- }
-
-
- }
-
- }
Student 2 - Create a "Student2" as a class and inherits Abstract Class and Yoga Interface.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace OOPSConcepts
- {
- class Student2: AbsStudent,IYoga
- {
-
-
- public override string stuName
- {
- get
- {
- return Name;
- }
- set
- {
- Name = value;
- }
- }
- public override string stuID
- {
- get
- {
- return ID;
- }
- set
- {
- ID = value;
- }
- }
- public override string stuClass
- {
- get
- {
- return Class;
- }
- set
- {
- Class = value;
- }
- }
- public override string stuAddress
- {
- get
- {
- return Address;
- }
- set
- {
- Address = value;
- }
- }
-
- public override void PaysFees()
- {
- Console.WriteLine("Payment by Card");
- }
- public override void ModeOfTransport()
- {
- Console.WriteLine("Coming in Own");
- }
-
- public void Yoga()
- {
- Console.WriteLine("Talented in Yoga");
-
- }
-
-
- }
-
- }
Client Class: Create a new class To test the code.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace OOPSConcepts
- {
- class ClientClass
- {
- static void Main(string[] args)
- {
- Student1 objStu1 = new Student1();
- objStu1.stuID = "Eng001";
- objStu1.stuName = "Ram";
- objStu1.stuAddress = "Chennai";
- objStu1.stuClass = "6th";
- Console.WriteLine("*****************************");
- objStu1.DisplayDetails(objStu1.stuName, objStu1.stuID, objStu1.stuClass, objStu1.stuAddress);
- objStu1.PaysFees();
- objStu1.ModeOfTransport();
- objStu1.Dance();
- objStu1.Sing();
- Console.WriteLine("****************************");
- Student2 objStu2 = new Student2();
- objStu2.stuID = "Med0001";
- objStu2.stuName = "Ramesh";
- objStu2.stuAddress = "Madurai";
- objStu2.stuClass = "8th";
- Console.WriteLine("******************************");
- objStu2.DisplayDetails(objStu2.stuName, objStu2.stuID, objStu2.stuClass, objStu2.stuAddress);
- objStu2.PaysFees();
- objStu2.ModeOfTransport();
- objStu2.Yoga();
- Console.WriteLine("*********************************");
- Console.ReadLine();
- }
-
- }
- }
Output