Monday 10 February 2014

how to decide to choose abstract class or an interface (Java or .Net)

The abstract class inheritance is used when the subclasses / childs / derived shares the core properties and behaviour of the abstract class. The kind of behaviour that actually defines the class.

On the other hand interface inheritance is used when the classes share peripheral behaviour, ones which do not necessarily define the derived class. It is used to separate implementation and specification. For example , your impl class is called by client using rmi , if there is any change impl class. You are not required to give impl class to client. Client can call impl using specification / interface.



For eg.
Abstract Class : A Car and a Truck shares a lot of core properties and behaviour of an Automobile abstract class,
Interface : They also share some peripheral behaviour like Drillers or PowerGenerators share and doesn't necessarily defines a Car or a Truck, so Car, Truck, Driller and PowerGenerator can all share the same interface IExhaust.

No comments:

Post a Comment