Archive

Posts Tagged ‘OOP’s’

Difference between abstract class and interface

October 13, 2009 1 comment

Hi,

Today I’m going to discuss some very exciting facts and concepts about OOP’s that I came across during my project works.

So as the title explains I’m trying to list down differences between a class and interface:

  • Class: In simple words its a collection of data members and member functions/methods/subroutines on basis of which we can perform operations/ data manipulation just by creating an instance of it. Here needless to say that; all member functions/methods/subroutines are  concrete i.e they have signature as well as implementation.
    • Abstract Class: Its bit special type of class which provides you to have member functions/methods/subroutines as concrete as well as member functions/methods/subroutines which do not have implementation.
    • Need of abstract class comes into picture in case of multiple inheritance as per above specification 🙂
  • Interface: As name implies its basic outline of any concrete class. It has got just member functions/methods/subroutines with mere signature and no implementation. That’s why it ease the Multiple Inheritance.

The Pure Object Oriented language’s comes up with the limitation of not supporting multiple inheritence. And in any case if we want to implement this then, interface is the only work around for it (I mean class novel: Ibook, Imagzine).

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