Friday, March 18, 2011

What is the difference between an Interface and an Abstract Class?

Interface:
An interface is a collection of method prototypes, i.e. method signatures only, no body.
The class that implements the interface must provide an implementation to the methods in the interface.
So a interface is used to enforce a contract.

Abstract class:
An abstract class is one that has one or more abstract methods. An abstract method is one that has only a signature and no implementation. An abstract class can have implemented methods. An abstract class cannot be instantiated, but can be sub-classed/extended. The class that extends the abstract class must provide a definition for the abstract methods.

So, an abstract class is used to provide an initial implementation, that needs to be extended by a sub class. It is used to propagate 'a kind of' relationship.

No comments:

Post a Comment