See
My OO Presentation Notes as a preface.
Inheritance is concept that should be familiar with all of us, since we inherit physical characteristics from our parents. In object-oriented programming, the general concept of inheritance works similarly, but is different than genetic inheritance. In C# and Java, inheritance is where new classes can be formed by inheriting from one abstract or concrete class. The new classes are considered derived or concrete classes, and the classes from which they inherit are known as base classes. When a derived class inherits from a base class, the former can do everything the latter can do, and whatever specialized functionality it provides in addition.
The basic concept of inheritance in OO can be represented as an “Is-a” relationship. For example, we can say that “Dog” is a generalization of dog types, such as “Dalmatian,” “Golden Retriever,” “Border Collie,” and others. When a “Dalmation” class inherits from the “Dog” class, the “Dalmation” object “Is-a” dog. The “Dalmation” object inherits all the functionality and properties common to all dogs. We can say that “Dog” is an abstraction of “Dalmation.” Abstraction will be discussed in a later post.
There are several uses for inheritance, some of these include specialization, overriding, extension, and reuse (InheritanceWiki, 2006). Specialization is used to create specialized functionality for a new class or object. With specialization, the new, derived class inherits the characteristics of the base class, and then implements something new that is unique, and not a part of the base class. Using our “Dog” concept, the “Dog” class might have a bark method. Using specialization, we could create an “AttackDog” class that inherits the bark method from the “Dog” class, and adds a new feature, the ability to attack. Specialization is also used when derived classes implement the behavior of abstract classes that only declare functionality and do not implement it.
public class Dog
{
string eyeColor;
string coatLenght;
public void Bark()
{
// make a barking noise
}
}
public class AttackDog : Dog
{
// AttackDog inherits eyeColor,
// coatLength, and Bark()
// add specialization
public void Attack()
{
// do some attacking
}
}
Overriding is the ability to inherit methods from a base class, and to supersede those methods with new implementations. The derived class must provide the same method signature as the base class, and return the same type. Using our “Dog” model, a “MuteDog” class would override the bark method it inherits from “Dog,” and implement new functionality, such as silence, or no bark. Overriding is commonly used with abstract classes that expect derived classes to implement more specific functionality. Another good opportunity for overriding is with the “ToString” method inherited from the object class in C#.
public class MuteDog : Dog
{
// AttackDog inherits eyeColor,
// coatLength, and Bark()
// add specialization
public override void Bark()
{
// todo: bark in silence
}
}
Extension is term describing when a derived class inherits from a base class and provides additional functionality or characteristics. In specialization, the new class implements functionality or characteristics that are not part of the inherited class. With extension, the functionality or characteristics could be present in the inherited class. Extension is also the term used to describe the relationship of class inheritance. “Dalmation,” “Golden Retriever,” and “Border Collie” are extensions of “Dog.”
One of the most powerful advantages of object-oriented programming is code reuse. Simply put, the ability to reuse modules and their functionality in multiple projects saves time and money. Code reuse, when used properly, reduces coding time, testing, and maintenance. Code reuse can be as simple as two derived classes using the same functionality they inherited from their base class, or more extensive such as multiple projects sharing a class library. The idea encouraging code reuse is that functionality can be reused and not rewritten, if design properly.
References:
InheritanceWiki. (n.d.). Retrieve May 22, 2006, from http://en.wikipedia.org/wiki/Inheritance_%28object-oriented_programming%29