You are on page 1of 10

Java Programming, Eighth Edition 10-1

Chapter 10
Introduction to Inheritance
A Guide to this Instructor’s Manual:

We have designed this Instructor’s Manual to supplement and enhance your teaching
experience through classroom activities and a cohesive chapter summary.

This document is organized chronologically, using the same headings that you see in the
textbook. Under the headings you will find: lecture notes that summarize the section, Teaching
Tips, Class Discussion Topics, and Additional Projects and Resources. Pay special attention to
teaching tips and activities geared towards quizzing your students and enhancing their critical
thinking skills.

In addition to this Instructor’s Manual, our Instructor’s Resources also contain PowerPoint
Presentations, Test Banks, and other supplements to aid in your teaching experience.

At a Glance

Instructor’s Manual Table of Contents


• Overview

• Objectives

• Teaching Tips

• Quick Quizzes

• Class Discussion Topics

• Additional Projects

• Additional Resources

• Key Terms

© 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
Java Programming, Eighth Edition 10-2

Lecture Notes

Overview
Chapter 10 expands on the concepts of inheritance, polymorphism, and information
hiding, which were introduced in Chapter 1. Students will learn to derive classes and to
access constructors, data fields, and methods of a superclass.

Objectives
• Learn about the concept of inheritance
• Extend classes
• Override superclass methods
• Call constructors during inheritance
• Access superclass methods
• Employ information hiding
• Learn which methods you cannot override

Teaching Tips
Learning About the Concept of Inheritance
1. Explain the concept of inheritance using real-life examples.

Teaching A common analogy for inheritance is to describe a vehicle class being extended
Tip by car, truck, and motorcycle.

Diagramming Inheritance Using the UML

1. Describe Unified Modeling Language (UML). UML consists of many different types
of diagrams. Introduce the class diagram. A class diagram for the Employee class
(program listing in Figure 10-1) is shown in Figure 10-2. Using Figure 10-3, explain
how a class diagram depicts inheritance.

Teaching
Students can learn more about UML at www.uml.org.
Tip

2. Discuss the benefits of using inheritance to create new classes rather than creating them
from scratch. Some of these benefits are described in the bulleted list on page 494.

© 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
Java Programming, Eighth Edition 10-3

3. Point out that many of the existing Java objects that students have been using are
inherited classes. The Integer and JOptionPane classes inherit from the Object
class.

Teaching View the class inheritance for Integer at


Tip http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Integer.html.

Inheritance Terminology

1. Define the terms base class and derived class, as well as the synonyms for these terms:
superclass or parent class for a base class, and subclass or child class for a derived
class.

Teaching Some students prefer the terms superclass and subclass, primarily because the
Tip term matches the Java call super() used later in the chapter.

2. Spend time reviewing the “is a” model discussed in the book. Use real-world “is a”
examples that your students will understand.

3. Define composition and explain how it differs from inheritance.

4. Define aggregation and explain how it differs from inheritance.

Two Truths and a Lie


1. Discuss the two truths and a lie on page 496.

Extending Classes

1. Discuss the keyword extends, which is used to declare a derived class. Use Figure
10-4 to illustrate the use of the extends keyword.

2. Remind students that a derived class can access the methods of its base class, but not
the reverse.

3. Discuss the instanceof keyword. The use of this keyword is shown on page 497.

4. If possible, extend one of the classes you built in an earlier lecture or assigned to the
students. Reusing a class that your students have experienced really drives home the
idea of reusability.

© 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
Java Programming, Eighth Edition 10-4

Note that the casting operator, (), can be used when an object is declared using
Teaching
the base class but instantiated using the superclass. This is often combined with
Tip
the use of instanceof.

Two Truths and a Lie


1. Discuss the two truths and a lie on page 498.

You Do It
1. Students should follow the steps in the book on pages 498–502 to create a Java
application with two classes that demonstrate inheritance.

Quick Quiz 1
1. ____ is the principle that allows you to apply your knowledge of a general category to
more specific objects.
Answer: Inheritance

2. True or False: A class diagram consists of a rectangle divided into five sections.
Answer: False

3. You use the keyword ____ to declare a derived class in Java.


Answer: extends

4. The ____ operator determines whether an object is a member of a descendant of a class.


Answer: instanceof

Overriding Superclass Methods


1. Re-introduce the term polymorphism. When a child class declares a method with the
same name, return type, and arguments as a method in the parent class, it is said to
override the method. Child classes often override the methods when the method
implementation in the parent class is not appropriate for the object in the child class.
Note that Java will always call the child method when the object is of the child type.

2. Remind students of the polymorphic constructors built in Chapter 4.

© 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
Java Programming, Eighth Edition 10-5

Polymorphism is present in all languages. In English, consider the many


Teaching definitions of the word run. We understand the proper definition of the word
Tip based on context. Java also uses context to understand which polymorphic
function to call.

3. Define the term subtype polymorphism.

4. During your lecture, override a few methods from a parent class. Be sure to use
methods that your students will understand.

Teaching A good programming technique is placing the Java annotation @Override


Tip before any child method that overrides a parent method.

Two Truths and a Lie


1. Discuss the two truths and a lie on page 505.

You Do It
1. Students should follow the steps in the book on pages 505–506 to create a Java
application that creates a class that overrides a parent class method.

Calling Constructors During Inheritance


1. Explain that when an object of a child class is instantiated, the parent class constructor
is called before the child class constructor. When using default constructors, this is
transparent to the programmer.

2. Review the example in Figure 10-8 that illustrates what happens during instantiation of
a child object. The program output is seen in Figure 10-9.

3. During your lecture, create the subclass’s default constructor that calls the superclass
default constructor.

Teaching Remind students that a call to super() must be the first statement in a subclass
Tip constructor.

© 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
Java Programming, Eighth Edition 10-6

Using Superclass Constructors That Require Arguments

1. Note that when a programmer provides a constructor for a subclass, he or she can call
the superclass constructor using the super keyword. The syntax for calling a
superclass constructor is shown on page 509.

2. Explain that a subclass can refer to its superclass using the super keyword.

3. During your lecture, create the subclass’s parameterized constructor that calls the
superclass’s parameterized constructor.

Two Truths and a Lie


1. Discuss the two truths and a lie on page 510.

You Do It
1. Students should follow the steps in the book on pages 510–513 to create a Java
application that creates a class that uses subclass constructors.

Accessing Superclass Methods

1. Note that the super keyword can be used to reference a method in the superclass, as
seen in Figure 10-13.

2. During your class lecture, create a subclass method that calls a superclass method.
Output methods are a good example.

Comparing this and super

1. Remind students that the this reference points to variables and methods within the
current class. The super reference points to variables and methods within the super
class.

2. If a class has not overridden a superclass method, then the this reference can be used
to access the superclass method. However, it is preferable to use the super reference
instead.

Two Truths and a Lie


1. Discuss the two truths and a lie on page 515.

Quick Quiz 2
1. Using the same method name to indicate different implementations is called ____.
© 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
Java Programming, Eighth Edition 10-7

Answer: polymorphism

2. When creating a subclass, rewriting a superclass method is referred to as ____ the


method.
Answer: overriding

3. True or False: When you instantiate an object that is a member of a subclass, you are
actually calling at least two constructors.
Answer: True

4. The keyword ____ always refers to the superclass of the class in which you use it.
Answer: super

Employing Information Hiding


1. Review the concept of information hiding. Remind students that it is common to use
the private keyword as an access modifier for data fields, and to use the public
access modifier for methods.

Teaching Learn more about information hiding at www.tech-recipes.com/rx/1428/java-


Tip information-hiding.

2. Note that this pattern prevents a subclass from directly accessing data fields of a
superclass. Since this access may be desired, the protected access modifier can be
used. This access modifier lets a subclass access the variable or method but not
unrelated classes.

3. Using the protected access method can lead to fragile Java classes.

4. A good programming technique is to use the set methods of the superclass’s private
variables.

5. Use the superclass and subclass code that you have been building during your lecture to
demonstrate how information hiding works.

Two Truths and a Lie


1. Discuss the two truths and a lie on page 518.

Methods You Cannot Override


1. Inform students that there are three types of methods that cannot be overridden:
static methods, final methods, and methods within final classes.

© 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
Java Programming, Eighth Edition 10-8

A Subclass Cannot Override static Methods in Its Superclass

1. Discuss what happens when a subclass declares a method with the same name, return
type, and parameter list as a static method in the superclass.
2. Figures 10-17 through 10-21 show attempts at overriding static superclass methods
that fail. Figures 10-22 and 10-23 show the correct method of hiding or reusing a
static method name in a subclass.

A Subclass Cannot Override final Methods in Its Superclass

1. Note that when a superclass uses the final keyword in a method specification, a
subclass cannot override this method.

2. Describe how Java uses virtual method calls.

3. Discuss the reasons why a programmer would choose to make a method final.

4. Discuss how final methods are placed inline within the code, resulting in faster
program execution.

A Subclass Cannot Override Methods in a final Superclass

1. Explain that the final keyword can also be used as part of a class definition. This
means that the entire class cannot serve as a superclass.

Two Truths and a Lie


1. Discuss the two truths and a lie on page 524.

Don’t Do It
1. Review this section, discussing each point with your class.

Quick Quiz 3
1. The concept of keeping data private is known as ____.
Answer: information hiding

2. Using the keyword ____ provides you with an intermediate level of security between
public and private access.
Answer: protected

3. True or False: Although a child class cannot inherit its parent’s static methods, it can
access its parent’s static methods the same way any other class can.
Answer: True
© 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
Java Programming, Eighth Edition 10-9

4. You can declare a class to be ____. When you do, this class cannot be a superclass.
Answer: final

Class Discussion Topics


1. Why are the concepts of inheritance and polymorphism so important to object-oriented
programming? Do these concepts make more sense to you now than when they were
first introduced in Chapter 1?

2. Why would you want to make an entire class final?

3. How does inheritance reduce the workload on a computer programmer?

Additional Projects
1. Using the Internet, research the term multiple inheritance. What does it mean? Is it
supported in Java? Why or why not?

2. Using the Internet, research inheritance in Java. What is the base class for all Java
objects? What methods does this class provide? Which methods of this class are
commonly overridden?

3. Research the classes used in an Android project. Determine from where each class
inherits.

Additional Resources
1. Inheritance from the Java tutorial:
http://download.oracle.com/javase/tutorial/java/concepts/inheritance.html

2. Encapsulation is not information hiding:


www.javaworld.com/javaworld/jw-05-2001/jw-0518-encapsulation.html

3. Objects and Information Hiding:


www.devarticles.com/c/a/Java/Java-Objects-and-Information-Hiding

4. Writing Final Classes and Methods:


http://download.oracle.com/javase/tutorial/java/IandI/final.html

© 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.
Java Programming, Eighth Edition 10-10

Key Terms
 Aggregation: a type of composition in which a class contains one or more members of
another class that would continue to exist without the object that contains them.
 Base class: a class that is used as a basis for inheritance.
 Child class: a synonym for derived class.
 Class diagram: a visual tool that provides you with an overview of a class. It consists
of a rectangle divided into three sections—the top section contains the name of the
class, the middle section contains the names and data types of the attributes, and the
bottom section contains the methods.
 Composition: a relationship in which one class contains one or more members of
another class that would not continue to exist without the object that contains them.
 Derived class: a class that inherits from a base class.
 extends: the keyword used to achieve inheritance in Java.
 Fragile: classes that are prone to errors.
 Information hiding: the concept of keeping data private.
 Inheritance: a mechanism that enables one class to inherit, or assume, both the
behavior and the attributes of another class.
 Inlining: an automatic process that optimizes performance. Because a final method’s
definition can never be overridden, the compiler can optimize a program’s performance
by removing the calls to final methods and replacing them with the expanded code of
their definitions at each method call location.
 instanceof operator: determines whether an object that is the operand on the left is
a member or descendant of the class that is the operand on the right.
 Override the method: create a method in a child class that has the same name and
argument list as a method in its parent class.
 Parent class: a synonym for base class.
 Polymorphism: using the same method name to indicate different implementations.
 protected: the keyword that provides you with an intermediate level of security
between public and private access. Protected members are those that can be
used by a class and its descendants.
 Subclass: a synonym for derived class.
 Subtype polymorphism: the ability of one method name to work appropriately for
different subclasses of a parent class.
 super: the keyword that always refers to the superclass of the class in which you use
it.
 Superclass: a synonym for base class.
 Unified Modeling Language (UML): a graphical language used by programmers and
analysts to describe classes and object-oriented processes.
 Upcast: change an object to an object of a class higher in the object’s inheritance
hierarchy.
 Virtual method calls: those in which the method used is determined when the program
runs, because the type of the object used might not be known until the method executes.
In Java, all instance method calls are virtual calls by default.

© 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
license distributed with a certain product or service or otherwise on a password-protected website for classroom use.

You might also like