Wednesday, August 25, 2010

Why Interface ? Difference between Interface and Abstract Class?

Interface are the rules or specification for an object.

To discuss Interface we first discuss Inheritance. Every body knows inheritance is inheriting Parent class property into derived class, but why we need inheritance. We need inheritance to map Real World Problem into Software.

Let’s take an example of real world,

How we define a human being?

We define a human being who has one head, two eyes, two ears etc., so we have set of specification for a real world entity to be human being.

So to create these specification in software we create a class with name Human Being, then classes derived from this class will be Human Being , because they have all properties of the Human Being.

So to make independent Rules and their implementation Interface and abstract classes comes into picture.


Difference between Interface and Abstract Class:

The practical difference is that we need abstract class when we required default implementation and some protected fields(which needs to be define by derived class not by user which is not possible in Interface)