| ||||||
| ||||||

InheritanceGoals:
OverviewInheritance allows a 'derived' class to be created from an existing 'base' class. The new class inherits the fields and methods of the base class. In addition, the new class can add new methods, add new fields, and override existing methods. MotivationInheritance provides a way to model the real world concept of generalization/specialization. Consider the different types of people at a university described in the following diagram.
The types at the top of the hierarchy are very general since everyone at the university is a person. Proceeding down the hierarchy things become more specialized because some of the people at the university are students and some are employees. Students and employees have some behavior in common but differ in other ways. The parts they share are captured in the base person class while their differences appear in the derived student and employee classes. Similar comments apply lower in the hierarchy since some of the students at the university are undergraduates and some are graduates. The common term for this type of generalization/specialization is the "is-a" relationship. One might say "a student is a person" or "a faculty member is a employee". The example above intentionally uses types of people in order to demonstrate that the concept of the is-a relationship is not exclusive to programming. Most humans naturally create mental categories such as person, fruit, employee, shape, mammal, etc. to help them deal with the complexity of the real world. The categories capture the elements that all members of the group share. For example, all employees might have an id number and make a certain salary, all shapes might have a color and take up a certain area when drawn on a piece of paper, and so on. This organization makes the world a much simpler place because an understanding of a category gives a lot of information about the behavior of each member. In other words, if you understand a basic concept such as apple, it will help you when you encounter a new member of the group such as Fuji or Braeburn for the first time. Inheritance gives developers a way to express the is-a relationship in their code. This is especially important in object-oriented programming where the goal is to model the real world accurately. If in the real world, the people at a university are categorized as shown in the diagram above, then a software model of the university should be sure to capture those relationships. The common wisdom then, says that whenever the is-a relationship exists in the application domain, it should be modeled in software using inheritance. At this point, the discussion of the meaning and benefits of inheritance has been mostly theoretical. It might help to look at a more concrete advantage as well: elimination of repeated code. Consider the two classes below and note how several lines of identical code appears in both.
The disadvantages of repeated code are well known: extra work to program and extra
work to debug and maintain. In the following discussion, we will see how to rewrite
the above example so the repeated code is factored out into a base class called
Basic syntax
A class specifies its base class by placing a colon and the base class name as part
of its declaration. The example code below shows a base class
An inheritance hierarchy can be as deep as required. For example, a
Memory allocationCreating a derived class object allocates memory for the fields declared in both the base class and the derived class.
Base servicesPublic members of the base class are available to clients of the derived class. That is, client code that is using an instance of the derived class can access any public fields or call any public methods that are declared in the base class.
Single inheritanceC# offers only single inheritance which means that a class can have only one direct base class. This restriction is actually imposed by the CLR so single inheritance is the rule for all .NET languages. The designers of .NET acknowledge that multiple inheritance is more powerful than single inheritance; however, they decided that the extra complications and ambiguous situations that arise in the presence of multiple base classes was not worth this extra power.
Protected access
There is a third access level for class members in addition to the more common
Hiding inherited memberIt is possible that a base class and a derived class might contain an identical member. The example below shows the issue.
This preceding code has two ambiguities. The base class and derived class have an
accessible field with the same name and they contain a method with the same
signature (name and parameter list). To see how such situations might arise in
practice, let's revisit the Suppose the person class was created first. In most countries, each person is given an id number by the government so it would make sense for person to have an id field and one or more access methods as shown below.
Once the basic person class is complete, another team member might begin writing
a derived class such as
At this point in the development process there are no ambiguities. Because
everything is working correctly, the designers of
Now suppose the
The addition of the
It turns out that the derived class method takes precedence over the base class
method. Therefore, the behavior of the client code will change, where previously
the
The C# design team considered this type of behavior change too important to
ignore; therefore, ambiguities between base and derived classes generate a
warning from the compiler. When the designer of the
The keyword
In practice, inheritance hierarchies created by a single team will likely be
carefully designed to minimize these types of ambiguities so the use of
Invoking base class methodsA derived class often needs to invoke a method defined in its base class. Generally, this is extremely easy and the inherited method can be called just as if it were a method of the derived class. The code below shows this simple case.
If the base and derived class both have a method with the same signature, the derived
class must use the keyword
Construction and inheritanceIt is common for a base class to require initialization. For example, the following class would likely need to initialize the person's name and age.
The base class would probably offer one or more constructors to do the required initialization.
Derived classes inherit all the fields of the base class and add their own. When a derived class object is created, both the derived class part of the object and the base class part must be initialized.
The derived class constructor can invoke a base class constructor to initialize
the base class part of the object using the
This material is excerpted from the Programming C# course offered by DevelopMentor. |