Wednesday, 5 August 2015

Abstract Class & Interface

Why do we use abstract class : 

We create a abstract class when we want to move the common functionality of 2 or more classes to a base class and we don't want the base class to be instantiated.

Why do we use Interface :

It is a pure abstract class ans we use it when we want to provide a guideline for what will be happening but not in detail.

Abstract Class Vs Interface, Design Choice :

In short , we will use interface when a template needs to be described for a group of sub-classes while we use an interface when a role needs to be described for other classes. 

template -> has some properties and some functionality
roles -> only functionality.

If you did not get the template and role part, use this :
  • If the functionality you are creating will be useful across a wide range of disparate objects, use an interface. Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing common functionality to unrelated classes.
  • If you are designing small, concise bits of functionality, use interfaces. If you are designing large functional units, use an abstract class.

No comments:

Post a Comment