here 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 IMovableinterface. | An abstract class defines the core identity of aclass 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 abstractclass 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 theinterface 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 ininterfaces | 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
Collapseusing System;
namespace AbstractsANDInterfaces
{
public abstract class Employee
{
protected String id;
protected String lname;
protected String fname;
public abstract String ID
{
get;
set;
}
public abstract String FirstName
{
get;
set;
}
public abstract String LastName
{
get;
set;
}
public String Update()
{
return "Employee " + id + " " +
lname + " " + fname +
" updated";
}
public String Add()
{
return "Employee " + id + " " +
lname + " " + fname +
" added";
}
public String Delete()
{
return "Employee " + id + " " +
lname + " " + fname +
" deleted";
}
public String Search()
{
return "Employee " + id + " " +
lname + " " + fname +
" found";
}
public abstract String CalculateWage();
}
}Interface Employee
Collapseusing System;
namespace AbstractsANDInterfaces
{
public interface IEmployee
{
String ID
{
get;
set;
}
String FirstName
{
get;
set;
}
String LastName
{
get;
set;
}
String Update();
String Add();
String Delete();
String Search();
String CalculateWage();
}
}Inherited Objects
Emp_Fulltime:
Collapseusing System;
namespace AbstractsANDInterfaces
{
public class Emp_Fulltime : Employee
{
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;
}
}
public new String Add()
{
return base.Add();
}
public new String Delete()
{
return base.Delete();
}
public new String Search()
{
return base.Search();
}
public new String Update()
{
return base.Update();
}
public override String CalculateWage()
{
return "Full time employee " +
base.fname + " is calculated " +
"using the Abstract class...";
}
}
}Emp_Fulltime2:
Collapseusing System;
namespace AbstractsANDInterfaces
{
public class Emp_fulltime2 : IEmployee
{
protected String id;
protected String lname;
protected String fname;
public Emp_fulltime2()
{
}
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;
}
}
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.";
}
public String CalculateWage()
{
return "Full time employee " +
fname + " caluculated using " +
"Interface.";
}
}
}Code for Testing
Collapse
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" ;
MessageBox.Show(emp.Add().ToString());
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());
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.