Object-Oriented Programming:
Inheritance
Chapter 11 of Visual C# How to Program, 6/e
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
11.1 Introduction
Inheritance allows a new class to absorb an existing class’s
members.
A derived class normally adds its own fields and methods to
represent a more specialized group of objects.
Inheritance saves time by reusing proven and debugged high-quality
software.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
11.1 Introduction (Cont.)
The direct base class is the base class which the derived class
explicitly inherits.
An indirect base class is any class above the direct base class in the
class hierarchy.
The class hierarchy begins with class object.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
11.1 Introduction (Cont.)
The is-a relationship represents inheritance.
For example, a car is a vehicle, and a truck is a vehicle.
New classes can inherit from thousands of pre-built classes in class
libraries.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
11.2 Base Classes and Derived Classes
Figure 11.1 lists several simple examples of base classes and derived
classes.
Note that base classes are “more general,” and derived classes are
“more specific.”
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
11.2 Base Classes and Derived Classes (Cont.)
The UML class diagram of Fig. 11.2 shows an inheritance hierarchy
representing a university community.
Each arrow represents an is-a relationship.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
11.2 Base Classes and Derived Classes (Cont.)
Now consider the Shape hierarchy in Fig. 11.3.
We can follow the arrows to identify the is-a relationships.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
11.2 Base Classes and Derived Classes (Cont.)
Objects of all classes that extend a common base class can be treated
as objects of that base class.
However, base-class objects cannot be treated as objects of their
derived classes.
A derived class can customize methods it inherits from its base class.
In such cases, the derived class can override (redefine) the base-
class method with an appropriate implementation.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
11.3 protected Members
A base class’s private members are inherited by derived classes,
but are not directly accessible by derived-class methods and
properties.
A base class’s protected members can be accessed by members
of that base class and by members of its derived classes, but not by
clients of the class.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
11.4.1 Creating and Using a CommissionEmployee
Class
CommissionEmployee (Fig. 11.4) represents an
employee who is paid a percentage of their sales.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
11.4.1 Creating and Using a CommissionEmployee Class
(cont.)
A colon (:) followed a class name at the end of the class declaration header
indicates that the class extends the class to the right of the colon.
Every C# class directly or indirectly inherits object’s methods.
If a class does not specify that it inherits from another class, it implicitly
inherits from object.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
11.4.1 Creating and Using a CommissionEmployee Class
(cont.)
Declaring instance variables as private and providing public properties to
manipulate and validate them helps enforce good software engineering.
Constructors are not inherited.
Either explicitly or implicitly, a call to the base-class constructor is made.
Class object’s default (empty) constructor does nothing.
Even if a class does not have constructors, the default constructor will call the
base class’s default or parameterless constructor.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
11.4.1 Creating and Using a CommissionEmployee Class
(cont.)
CommissionEmployee Method ToString
Method ToString is special—it’s one of the methods that every class inherits directly
or indirectly from class object.
Method ToString returns a string representing an object.
Class object’s ToString method is primarily a placeholder that typically should be
overridden by a derived class.
To override a base-class method, a derived class must declare a method with keyword
override.
The method must have the same signature (method name, number of parameters and
parameter types) and return type as the base-class method.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
11.4.1 Creating and Using a CommissionEmployee Class
(cont.)
Figure 11.5 tests class CommissionEmployee.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
11.4.2 Creating a BasePlusCommissionEmployee
Class without Using Inheritance
ClassBasePlusCommissionEmployee (Fig. 11.6)
contains a first name, last name, social security number,
gross sales amount, commission rate and base salary
—“Base” in the class name stands for “base salary” not
base class.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
11.4.2 Creating a BasePlusCommissionEmployee Class without Using
Inheritance (cont.)
Note the similarity between this class and class Commission
Employee (Fig. 11.4)—in this example, we do not yet exploit that
similarity.
Class BasePlusCommissionEmployeeTest
Figure 11.7 tests class BasePlusCommissionEmployee.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
11.4.2 Creating a BasePlusCommissionEmployee Class without Using
Inheritance (cont.)
Much of the code for BasePlusCommissionEmployee
is similar, if not identical, to the code for CommissionEmployee.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
11.4.3 Creating a CommissionEmployee–BasePlusCommissionEmployee
Inheritance Hierarchy
Now we declare class BasePlusCommissionEmployee
(Fig. 11.8), which extends class CommissionEmployee
(Fig. 11.4).
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
11.4.3 Creating a CommissionEmployee–
BasePlusCommissionEmployee Inheritance Hierarchy (cont.)
A BasePlusCommissionEmployee object is a
CommissionEmployee.
Each derived-class constructor must implicitly or explicitly call its
base-class constructor to ensure that the instance variables
inherited from the base class are initialized properly.
A constructor initializer with keyword base invokes the base-class
constructor.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
11.4.3 Creating a CommissionEmployee–
BasePlusCommissionEmployee Inheritance Hierarchy (cont.)
BasePlusCommissionEmployee Method Earnings
The virtual and abstract keywords indicate that a
base-class method can be overridden in derived classes.
The override modifier declares that a derived-class
method overrides a virtual or abstract base-class
method.
This modifier also implicitly declares the derived-class
method virtual.
We need to declare CommissionEmployee’s Earnings
method virtual.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
11.4.3 Creating a CommissionEmployee–
BasePlusCommissionEmployee Inheritance Hierarchy (cont.)
The compiler generates additional errors because base class CommissionEmployee’s
instance variables are private.
The errors can be prevented by using the public properties inherited from class
CommissionEmployee.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
11.4.4 CommissionEmployee–BasePlusCommissionEmployee Inheritance
Hierarchy Using protected Instance Variables
For the next example, we made class
CommissionEmployee’s instance variables protected.
We also declared the Earnings method virtual so that
BasePlusCommissionEmployee can override the method.
Class BasePlusCommissionEmployee (Fig. 11.9) extends
the version of class CommissionEmployee with protected
data rather than the one with private data in Fig. 11.4.
Because the instance variables are now protected, the
compiler does not generate errors.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
11.4.4 CommissionEmployee–BasePlusCommissionEmployee
Inheritance Hierarchy Using protected Instance Variables
Class BasePlusCommissionEmployeeTest
The BasePlusCommissionEmployeeTest class in this example’s project is
identical to that of and produces the same output, so we do not show the code
here.
Although the version of class BasePlusCommissionEmployee in Fig. 11.6
does not use inheritance and the version in Fig. 11.9 does, both classes provide
the same functionality.
The source code in Fig. 11.9 is considerably shorter than that in Fig. 11.6,
because a large portion of the class’s functionality is now inherited from
CommissionEmployee.
This makes the code easier to maintain, modify and debug, because the code
related to a CommissionEmployee exists only in that class.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
11.4.5 CommissionEmployee–BasePlusCommissionEmployee
Inheritance Hierarchy Using private Instance Variables
Using protected instance variables creates several potential
problems.
The derived-class object can set an inherited variable’s value
directly without validity checking.
Derived-class methods would need to be written to depend on the
base class’s data implementation.
You should be able to change the base-class implementation while still
providing the same services to the derived classes.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
11.4.5 CommissionEmployee–BasePlusCommissionEmployee Inheritance
Hierarchy Using private Instance Variables (cont.)
Class CommissionEmployee (Fig. 11.10) declares private
instance variables and provide public properties.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
11.4.5 CommissionEmployee–BasePlusCommissionEmployee Inheritance
Hierarchy Using private Instance Variables (cont.)
ClassBasePlusCommissionEmployee (Fig. 11.11)
inherits from CommissionEmployee’s and can access the
private base-class members via the inherited public
properties.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
11.4.5 CommissionEmployee–BasePlusCommissionEmployee Inheritance
Hierarchy Using private Instance Variables (cont.)
Class BasePlusCommissionEmployee’s Earnings overrides
class CommissionEmployee’s Earnings method.
The new version obtains the portion of the employee’s earnings
based on commission alone by calling CommissionEmployee’s
Earnings method with the expression base.Earnings()
This method invocation is a good software engineering practice—by
having BasePlusCommissionEmployee’s Earnings method
invoke CommissionEmployee’s Earnings method to calculate
part of a BasePlusCommissionEmployee object’s earnings, we
avoid duplicating the code and reduce code-maintenance problems
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
11.4.5 CommissionEmployee–BasePlusCommissionEmployee Inheritance
Hierarchy Using private Instance Variables (Cont.)
Class BasePlusCommissionEmployeeTest
Class BasePlusCommissionEmployeeTest performs
the same manipulations on a
BasePlusCommissionEmployee object as in Figs. 11.7
and produces the same output, so we do not show it here.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
11.5 Constructors in Derived Classes
• The derived-class constructor, before performing its own tasks,
invokes its direct base class’s constructor.
• This is done either explicitly or implicitly.
• If the base class is derived from another class, the base-class
constructor invokes the constructor of the next class up in the
hierarchy, and so on.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
11.6 Software Engineering with Inheritance
• When a new class extends an existing class, the new class inherits the
members of the existing class.
• We can customize the new class to meet our needs by including additional
members and by overriding base-class members.
• Independent software vendors (ISVs) can develop and sell proprietary
classes.
• Users then can derive new classes from these library classes without
accessing the source code.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
11.6 Software Engineering with Inheritance (Cont.)
Effectivesoftware reuse improves the software-
development process.
Object-oriented programming facilitates software reuse,
potentially shortening development time.
The availability of substantial and useful class libraries
helps deliver the maximum benefits of software reuse
through inheritance.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
11.7 Class object
All classes inherit directly or indirectly from the object class.
Figure 11.12 summarizes object’s methods.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.
©1992-2017 by Pearson Education, Inc. All Rights Reserved.