Click here to Skip to main content
Click here to Skip to main content

Abstract Class versus Interface

, 7 Jan 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
Abstract class versus Interface: Usage and Implementation.

Introduction

In this article along with the demo project I will discuss Interfaces versus Abstract classes. The concept of Abstract classes and Interfaces is a bit confusing for beginners of Object Oriented programming. Therefore, I am trying to discuss the theoretical aspects of both the concepts and compare their usage. And finally I will demonstrate how to use them with C#.

Background

An Abstract class without any implementation just looks like an Interface; however there are lot of differences than similarities between an Abstract class and an Interface. Let's explain both concepts and compare their similarities and differences.

What is an Abstract Class?

An abstract class is a special kind of class that cannot be instantiated. So the question is why we need a class that cannot be instantiated? An abstract class is only to be sub-classed (inherited from). In other words, it only allows other classes to inherit from it but cannot be instantiated. The advantage is that it enforces certain hierarchies for all the subclasses. In simple words, it is a kind of contract that forces all the subclasses to carry on the same hierarchies or standards.

What is an Interface?

An interface is not a class. It is an entity that is defined by the word Interface. An interface has no implementation; it only has the signature or in other words, just the definition of the methods without the body. As one of the similarities to Abstract class, it is a contract that is used to define hierarchies for all subclasses or it defines specific set of methods and their arguments. The main difference between them is that a class can implement more than one interface but can only inherit from one abstract class. Since C# doesn’t support multiple inheritance, interfaces are used to implement multiple inheritance.

Both Together

When we create an interface, we are basically creating a set of methods without any implementation that must be overridden by the implemented classes. The advantage is that it provides a way for a class to be a part of two classes: one from inheritance hierarchy and one from the interface.

When we create an abstract class, we are creating a base class that might have one or more completed methods but at least one or more methods are left uncompleted and declared abstract. If all the methods of an abstract class are uncompleted then it is same as an interface. The purpose of an abstract class is to provide a base class definition for how a set of derived classes will work and then allow the programmers to fill the implementation in the derived classes.

There are some similarities and differences between an interface and an abstract class that I have arranged in a table for easier comparison:

Feature

Interface

Abstract class

Multiple inheritance

A class may inherit several interfaces.

A class may inherit only one abstract class.

Default implementation

An interface cannot provide any code, just the signature.

An abstract class can provide complete, default code and/or just the details that have to be overridden.

Access Modfiers An interface cannot have access modifiers for the subs, functions, properties etc everything is assumed as public An abstract class can contain access modifiers for the subs, functions, properties

Core VS Peripheral

Interfaces are used to define the peripheral abilities of a class. In other words both Human and Vehicle can inherit from a IMovable interface.

An abstract class defines the core identity of a class and there it is used for objects of the same type.

Homogeneity

If various implementations only share method signatures then it is better to use Interfaces.

If various implementations are of the same kind and use common behaviour or status then abstract class is better to use.

Speed

Requires more time to find the actual method in the corresponding classes.

Fast

Adding functionality (Versioning)

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.

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.

Fields and Constants No fields can be defined in interfaces An abstract class can have fields and constrants defined

Using the Code

Let me explain the code to make it a bit easier. There is an Employee abstract class and an IEmployee interface. Within the Abstract class and the Interface entity I am commenting on the differences between the artifacts.

I am testing both the Abstract class and the Interface by implementing objects from them. From the Employee abstract class, we have inherited one object: Emp_Fulltime. Similarly from IEmployee we have inherited one object: Emp_Fulltime2.

In the test code under the GUI, I am creating instances of both Emp_Fulltime and Emp_Fulltime2 and then setting their attributes and finally calling the calculateWage method of the objects.

Abstract Class Employee

using System;

namespace AbstractsANDInterfaces
{
    /// 


    /// Summary description for Employee.

    /// 

    
    public abstract class Employee
    {
        //we can have fields and properties 


        //in the Abstract class

        protected String id;
        protected String lname;
        protected String fname;

        //properties


        public abstract String ID
        {
            get;
            set;
        }

        public abstract String FirstName
        {
            get;
            set;
        }
        
        public abstract String LastName
        {
            get;
            set;
        }
        //completed methods


        public String Update()
        {
            return "Employee " + id + " " + 
                      lname + " " + fname + 
                      " updated";
        }
        //completed methods


        public String Add()
        {
            return "Employee " + id + " " + 
                      lname + " " + fname + 
                      " added";
        }
        //completed methods


        public String Delete()
        {
            return "Employee " + id + " " + 
                      lname + " " + fname + 
                      " deleted";
        }
        //completed methods


        public String Search()
        {
            return "Employee " + id + " " + 
                      lname + " " + fname + 
                      " found";
        }

        //abstract method that is different 


        //from Fulltime and Contractor

        //therefore i keep it uncompleted and 

        //let each implementation 

        //complete it the way they calculate the wage.


        public abstract String CalculateWage();
        
    }
}

Interface Employee

using System;


namespace AbstractsANDInterfaces
{
    /// <span class="code-SummaryComment"><summary></span>


    /// Summary description for IEmployee.

    /// <span class="code-SummaryComment"></summary></span>

    public interface IEmployee
    {
        //cannot have fields. uncommenting 


        //will raise error!
        //        protected String id;
        //        protected String lname;
        //        protected String fname;


        //just signature of the properties 

        //and methods.

        //setting a rule or contract to be 

        //followed by implementations.


        String ID
        {
            get;
            set;
        }

        String FirstName
        {
            get;
            set;
        }
        
        String LastName
        {
            get;
            set;
        }
        
        // cannot have implementation


        // cannot have modifiers public 

        // etc all are assumed public

        // cannot have virtual


        String Update();

        String Add();

        String Delete();

        String Search();

        String CalculateWage();
    }
}

Inherited Objects

Emp_Fulltime:

using System;

namespace AbstractsANDInterfaces
{
    /// 


    /// Summary description for Emp_Fulltime.

    /// 

     
    //Inheriting from the Abstract class

    public class Emp_Fulltime : Employee
    {
        //uses all the properties of the 


        //Abstract class therefore no 

        //properties or fields here!


        public Emp_Fulltime()
        {
        }


        public override String ID
        {
            get

            {
                return id;
            }
            set
            {
                id = value;
            }
        }
        
        public override String FirstName
        {
            get

            {
                return fname;
            }
            set
            {
                fname = value;
            }
        }

        public override String LastName
        {
            get

            {
                return lname;
            }
            set
            {
                lname = value;
            }
        }

        //common methods that are 

        //implemented in the abstract class

        public new String Add()
        {
            return base.Add();
        }
        //common methods that are implemented 


        //in the abstract class

        public new String Delete()
        {
            return base.Delete();
        }
        //common methods that are implemented 


        //in the abstract class

        public new String Search()
        {
            return base.Search();
        }
        //common methods that are implemented 


        //in the abstract class

        public new String Update()
        {
            return base.Update();
        }
        
        //abstract method that is different 


        //from Fulltime and Contractor

        //therefore I override it here.

        public override String CalculateWage()
        {
            return "Full time employee " + 
                  base.fname + " is calculated " + 
                  "using the Abstract class...";
        }
    }
}

Emp_Fulltime2:

using System;

namespace AbstractsANDInterfaces
{
    /// 

    /// Summary description for Emp_fulltime2.


    /// 

    
    //Implementing the interface

    public class Emp_fulltime2 : IEmployee
    {
        //All the properties and 


        //fields are defined here!

        protected String id;
        protected String lname;
        protected String fname;

        public Emp_fulltime2()
        {
            //


            // TODO: Add constructor logic here

            //

        }

        public String ID
        {
            get

            {
                return id;
            }
            set
            {
                id = value;
            }
        }
        
        public String FirstName
        {
            get
            {
                return fname;
            }
            set

            {
                fname = value;
            }
        }

        public String LastName
        {
            get
            {
                return lname;
            }
            set
            {
                lname = value;
            }
        }

        //all the manipulations including Add,Delete, 


        //Search, Update, Calculate are done

        //within the object as there are not 

        //implementation in the Interface entity.

        public String Add()
        {
            return "Fulltime Employee " + 
                          fname + " added.";
        }

        public String Delete()
        {
            return "Fulltime Employee " + 
                        fname + " deleted.";
        }

        public String Search()
        {
            return "Fulltime Employee " + 
                       fname + " searched.";
        }

        public String Update()
        {
            return "Fulltime Employee " + 
                        fname + " updated.";
        }
        
        //if you change to Calculatewage(). 


        //Just small 'w' it will raise 

        //error as in interface

        //it is CalculateWage() with capital 'W'.

        public String CalculateWage()
        {
            return "Full time employee " + 
                  fname + " caluculated using " + 
                  "Interface.";
        }
    }
}

Code for Testing

//This is the sub that tests both 

//implementations using Interface and Abstract

private void InterfaceExample_Click(object sender, 
                                System.EventArgs e)
{
    try

    {

        IEmployee emp;

        Emp_fulltime2 emp1 = new Emp_fulltime2();

        emp =  emp1;
        emp.ID = "2234";
        emp.FirstName= "Rahman" ;
        emp.LastName = "Mahmoodi" ;
        //call add method od the object


        MessageBox.Show(emp.Add().ToString());
        
        //call the CalculateWage method

        MessageBox.Show(emp.CalculateWage().ToString());


    }
    catch(Exception ex)
    {
        MessageBox.Show(ex.Message);
    }

}

private void cmdAbstractExample_Click(object sender, 
                                   System.EventArgs e)
{

    Employee emp;

    emp = new Emp_Fulltime();
    

    emp.ID = "2244";
    emp.FirstName= "Maria" ;
    emp.LastName = "Robinlius" ;
    MessageBox.Show(emp.Add().ToString());

    //call the CalculateWage method


    MessageBox.Show(emp.CalculateWage().ToString());

}

Conclusion

In the above examples, I have explained the differences between an abstract class and an interface. I have also implemented a demo project which uses both abstract class and interface and shows the differences in their implementation.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Rahman Mahmoodi
Software Developer
Australia Australia
Rahman has worked in different areas of Software System Development Life Cycle. He started working as Tester and then moved to application programming and web application programming.
 
At the moment his area of interest are .Net including ASP.Net/2, VB.net, C# and object oriented design using UML.
 
He has Bachelor of Computing with Distinction Grade.

Comments and Discussions

 
GeneralMy vote of 5 Pinmemberjawed.ace19-Dec-11 19:48 
GeneralMy vote of 4 PinmemberMember 818100420-Nov-11 1:22 
GeneralMy vote of 5 Pinmemberversion_2.023-Oct-11 19:14 
GeneralRe: My vote of 5 PinmemberGeeth B28-Nov-11 5:00 
QuestionNeat, Concise and to the Point PinmemberPankaj Babre18-Oct-11 19:17 
GeneralMy vote of 5 Pinmembermbehdad11-Oct-11 15:27 
GeneralMy vote of 5 PinmemberAnurag Gandhi2-Sep-11 1:16 
GeneralMy vote of 5 PinmemberRamendra Kumar Sinha24-Aug-11 18:05 
Questionwhat is the use of volatile keyword Pinmemberradhakrishna mulpuri23-Aug-11 1:33 
GeneralMy vote of 5 PinmemberCengiz C.18-Jul-11 8:04 
Questiongreat! Pinmemberquangnd.edu16-Jul-11 17:23 
GeneralReally awesome example Pinmemberdupindersingh6-Jun-11 22:39 
GeneralMy vote of 5 Pinmembergbkaran20-May-11 22:15 
GeneralMy vote of 4 Pinmemberraj23karthik29-Apr-11 0:50 
Thanks!
GeneralWhat is the difference between an abstract class and interface PinmemberMember 458490814-Apr-11 2:05 
GeneralRe: What is the difference between an abstract class and interface PinmemberChiranjibC16-Apr-11 2:16 
GeneralMy vote of 5 Pinmemberrajnikantv29-Mar-11 0:17 
Generalexcellent Pinmemberali yeganeh28-Mar-11 11:02 
QuestionMistake? Why create an Interface 'instance'? Pinmembermatleeds16-Feb-11 5:57 
AnswerRe: Mistake? Why create an Interface 'instance'? PinmemberHomeSen15-Mar-11 23:09 
GeneralMy vote of 5 PinmembergloierTech11-Feb-11 17:49 
GeneralMy vote of 5 PinmemberJustin Porteous22-Jan-11 2:55 
GeneralMy vote of 5 PinmemberS.Dhanasekaran24-Nov-10 0:28 
GeneralMy vote of 5 Pinmemberbutchzn16-Nov-10 20:09 
GeneralInterface Pinmemberwaqar_khanzada4-Nov-10 10:24 
GeneralMy vote of 5 PinmemberEswa28-Oct-10 5:53 
GeneralMy vote of 5 Pinmemberkapil041126-Oct-10 20:39 
GeneralMy vote of 5 Pinmemberkirankkk200922-Aug-10 7:48 
GeneralMy vote of 5 Pinmembershankul26-Jul-10 22:29 
GeneralHii PinmemberNimbalkarTushar20-Jul-10 20:17 
GeneralInterfaces do not provide a means for multiple inheritance PinmemberHemanshuBhojak12323-Jun-10 3:05 
GeneralRe: Interfaces do not provide a means for multiple inheritance PinmvpMark Nischalke26-Oct-10 10:40 
GeneralRe: Interfaces do not provide a means for multiple inheritance PinmemberHemanshuBhojak12327-Oct-10 19:31 
GeneralRe: Interfaces do not provide a means for multiple inheritance PinmvpMark Nischalke28-Oct-10 1:22 
GeneralAbstract Class versus Interface Pinmemberwhitepearl720-Jun-10 18:52 
GeneralExcellent. The first article I've seen that gives a practical and understandable explanation PinmemberMr Yossu20-Jun-10 9:01 
Generalusing an interface without declaring its function Pinmemberkazim bhai14-Jun-10 2:44 
GeneralAbstract v/s Interface Pinmemberarpitgold5-Jun-10 10:27 
QuestionWhat makes class as abstract. PinmemberHarsha T17-May-10 2:35 
GeneralVery Good PinmemberAnupama K P23-Mar-10 4:10 
QuestionIDisposable PinmemberMichael Siroen3-Dec-09 2:25 
GeneralSuper Material PinmemberUdayakumar Venkataravanappa29-Sep-09 5:42 
GeneralSpeed? [modified] PinmemberMartijn van Dorp22-Jun-09 7:33 
GeneralRe: Speed? PinmemberDerek Viljoen23-Apr-10 2:51 
GeneralRe: Speed? PinmemberMartijn van Dorp23-Apr-10 3:51 
QuestionInterface or Abstract? PinmemberLLKrisJ12-Jan-09 10:29 
GeneralI agree, this is a good article Pinmemberzoldello11-Dec-08 11:40 
Generalalso Pinmemberykorotia28-Nov-08 7:38 
Generaldoubt PinmemberShahbaz Yousuf23-Nov-08 19:22 
GeneralRe: doubt Pinmemberykorotia28-Nov-08 7:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.141022.1 | Last Updated 7 Jan 2008
Article Copyright 2005 by Rahman Mahmoodi
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid