Inheritance
In the article on encapsulation, you saw how a class defines data which it
encapsulates and provides member methods and properties that can manipulate this
data.
Encapsulation is a great principle because it enables us to always maintain
strict control over the way data is manipulated. In the real world however, that
principle is not enough. A class may need to evolve in some way.
Historically, the software industry suffered from problems that stemmed from the
re-use and modification of source code. In the early days, reuse of code usually
meant copying the source code from an earlier program, modifying it and then
adding to it so that it might perform the new tasks. This was problematic on two
levels. Firstly, software source code is just text, and text can be modified for
good or for bad. A programmer is equally capable of writing a new and original
bug as they are of modifying perfectly good source code to include a new and
annoying bug. Secondly, source code as a commodity for resale in an increasingly
commercial world opened publishers of software to problems of blatant
intellectual property theft because, to sell source code is to give away all
your industrial secrets.
The object, defined by classes as you have seen previously, provides a way to
package behaviour along with data and, due to some ground breaking work by
Ole-Johan Dahl and Kristen Nygaard of the Norwegian Computing Center in 1967, an
object can be created which reuses the data and behaviour of a previously
defined class and, through inheritance, enables the new class to maintain the
original behaviour while add its own.
Subclass
In the article on encapsulation, you saw a class that stored the name and address
of a person. This is a very common thing to do so to reuse the software that
enables us to store and manipulate names and addresses would be highly
desirable. Many records require this information but may wish to add to that as
well. For example, a bank might wish to associate an account number and balance
with the same data to define a bank account or a shop may want to associate the
person with a list of purchases and a telephone number to create a customer
record.
Software inheritance enables us to do exactly this. Languages such as C#, C++,
Java, Visual Basic and many others enable us to reuse an existing class such
that we inherit the behaviours and data of the original while still allowing us
to express the new ideas and algorithms that we need.
Classes which take part in these "family trees" of objects have a relationship
to one another. A class which takes on the behaviours of another is called a
derived class or a subclass. The class from
which the subclass inherits its software DNA is called the base
class or the superclass.
Example of simple inheritance
The class shown below has a single member. A string called Name. We can create
an instance of this class, set the name and get the value stored back again at a
later time.
public class
BaseClass
{
public string
Name { get; set;
}
}
Using the class is accomplished like so:
//Create an instance of the class
BaseClass bc=new
BaseClass();
//Set the Name string
bc.Name="Fred";
//Read the Name string out and print it on the console
Console.WriteLine(bc.Name);
Now, a second class can be created which "inherits" the capabilities of the base
class. Note how the derived class nominates the base class in its declaration.
public class
DerivedClass :
BaseClass
{
public int
Age { get; set;
}
}
Using the derived class in a similar manner, you see that the age value can be
set as you might expect. Also, the DerivedClass object has a Name property which
can be set and read showing the the capabilities of the base class have been
inherited.
//Create an instance of the derived class
DerivedClass dc = new
DerivedClass();
//Set the age
dc.Age=10;
//Write it out to the console
Console.WriteLine(dc.Age);
//We can also set the Name, even though DerivedClass
doesn't define that property
dc.Name="Jim";
//And use the name as we did in the base class
Console.WriteLine(dc.Name);
You can also see from this screenshot, that Visual Studio understands that
DerivedClass instance has both Age and Name properties and that the Name
property comes from the base class.

Summary
In this article you have seen that a class may be used as the basis for a more
evolved or derived class that inherits the behaviour of its ancestor. The chain
of inheritance can be arbitrarily long such that a new class could nominate a
derived class as its base, thereby inheriting many charactristics from older
classes.
Inheritance is the mechanism whereby a class becomes a reusable commodity.
|