Abstract class versus Interface

转载 2006年06月16日 14:51:00

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 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 the same as an interface but with the restriction that it cannot make a class inherit from it. 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.

Constants

Only Static final constants.

Both instance and static constants are possible.

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 the various implementations only share method signatures then it is better to use Interface.

If the 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

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.

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
{
    /// <summary>
    /// Summary description for IEmployee.
    /// </summary>
    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();
        //has to be casted because of the interface!
        emp = (IEmployee) 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;
    //no casting is requird!
    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.

interface和abstract 的区别和相同点

在Java语言中,abstract class和interface是支持抽象类定义的两种机制。 不能创建abstract类的实例,然而可以创建一个变量,其类型是一个抽象类,并让它指向具体子类的一个实...
  • Mandypan
  • Mandypan
  • 2016年08月06日 19:30
  • 5692

java接口(interface)与抽象类(abstract class)的区别

很久没有看到如此精辟的文章了!作者写得实在太好了!正在学JAVA或者想了解抽象类与接口的朋友,绝对不可错过!本人极力推荐! OOP中接口与抽象类的区别 Java语言接口与继承的本质 深入理解接口...
  • zgrjkflmkyc
  • zgrjkflmkyc
  • 2013年09月30日 21:08
  • 5862

抽象类(abstract class)和接口(Interface)的区别

前言 抽象类(abstract class)和接口(Interface)是Java语言中对于抽象类定义进行支持的两种机制,赋予了Java强大的面向对象能力。二者具有很大的相似性,甚至可以相互替换,因...
  • aptentity
  • aptentity
  • 2017年04月01日 11:53
  • 1216

抽象类(abstract class)与接口(interface)的异同

抽象类:包含抽象方法的类就是抽象类 接口:指抽象方法的集合,方法不包含方法体相同点: 都不能被实例化 接口的实现类或者抽象类的子类都必须实现了接口或抽象类中的方法后才可以被实例化 不同点: 接口只有...
  • aym_fuhong
  • aym_fuhong
  • 2016年09月05日 20:07
  • 1718

c#中abstract与class的区别

http://blog.163.com/shirx2005@126/blog/static/8176490620091024102254107/
  • conganguo
  • conganguo
  • 2014年10月05日 21:01
  • 875

通过案例理解Abstract类和interface类

IBM上的一篇文章 看了之后深受启发。         看上面一篇可以理解的就好了,我这里是按照自己的理解,啰嗦的进行一下表述。           如果是偶尔阅读到这里,这个不算,我们假设你是主动搜...
  • shareye1992
  • shareye1992
  • 2016年07月07日 17:22
  • 761

JAVA基础再回首(七)——final关键字、抽象类abstract、接口interface、权限修饰符、导包

JAVA基础再回首(七)——final关键字、抽象类abstract、接口interface、权限修饰符、导包 没有到不了的明天。 嬉皮笑脸面对人生的难。 相信自己,不然你什么都做不了...
  • m366917
  • m366917
  • 2016年08月03日 21:04
  • 1886

Java之抽象(abstract)类、接口(interface)的用法总结

导读: 1、什么是抽象类(abstract)? 2、如何定义抽象类(abstract)? 3、抽象类(abstract)的作用? 4、何为接口?接口和类的区别? 5、怎么区分抽象类和接口? ...
  • dtjiawenwang88
  • dtjiawenwang88
  • 2017年06月14日 20:26
  • 523

java为什么要用abstract与Interface?

八.抽象类 有些类的实例不存在,但他代表一类不同对象的集合。如Animal类,没有动物叫Animal,但Animal尤其子类Dog,Cat等等。 抽象除了被继承外,没有值和用途 抽象的方法:抽象...
  • neverlesspray
  • neverlesspray
  • 2016年08月01日 13:59
  • 703

抽象类abstract和接口interface的区别与深入思考

在以前的编程过程中,经常对abstract和interface混淆,相信很多初学者都有这样的困惑,也问过很多经验丰富的程序员,他们也不能清楚地说出个所以然来。经过自己的思考,想到了一种比较形象的方式来...
  • wujiaxian
  • wujiaxian
  • 2014年10月11日 11:32
  • 2791
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Abstract class versus Interface
举报原因:
原因补充:

(最多只允许输入30个字)