My contemplations and diurnal novelties are publicized here


Inheritance is a useful programming concept, but it is easy to use inappropriately. Often interfaces do the job better.

Inheritance is a good choice when:

  • Your inheritance hierarchy represents an “is-a” relationship and not a “has-a” relationship.
  • You can reuse code from the base classes.
  • You need to apply the same class and methods to different data types.
  • The class hierarchy is reasonably shallow, and other developers are not likely to add many more levels.
  • You want to make global changes to derived classes by changing a base class.

These considerations are discussed in order below.

Inheritance and “Is a” Relationships

Two ways to show class relationships in object-oriented programming are “is a” and “has a” relationships. In an “is a” relationship, the derived class is clearly a kind of the base class. For example, a class namedPremierCustomer represents an “is a” relationship with a base class named Customer because a premier customer is a customer. However, a class named CustomerReferral represents a “has a” relationship with theCustomer class because a customer referral has a customer, but a customer referral is not a kind of customer.

Objects in an inheritance hierarchy should have an “is a” relationship with their base class because they inherit the fields, properties, methods, and events defined in the base class. Classes that represent a “has a” relationship with other classes are not suited to inheritance hierarchies because they may inherit inappropriate properties and methods. For example, if the CustomerReferral class were derived from the Customer class discussed previously, it might inherit properties that make no sense, such as ShippingPrefs and LastOrderPlaced. “Has a” relationships such as this should be represented using unrelated classes or interfaces. The following illustration shows examples of both “is a” and “has a” relationships.

relationship

Copied & edited from http://msdn.microsoft.com/en-us/library/27db6csx(VS.80).aspx#Mtps_DropDownFilterText

When to Use Interfaces

Interfaces are a powerful programming tool because they allow you to separate the definition of objects from their implementation. Interfaces and class inheritance each have advantages and disadvantages, and you may end up using a combination of both in your projects.

Flexibility in Implementation

There are several other reasons why you might want to use interfaces instead of class inheritance:

  • Interfaces are better suited to situations in which your applications require many possibly unrelated object types to provide certain functionality.
  • Interfaces are more flexible than base classes because you can define a single implementation that can implement multiple interfaces.
  • Interfaces are better in situations in which you do not need to inherit implementation from a base class.
  • Interfaces are useful in cases where you cannot use class inheritance. For example, structures cannot inherit from classes, but they can implement interfaces.

Copied and edited from  http://msdn.microsoft.com/en-us/library/3b5b8ezk(v=vs.80).aspx

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: