Java9e PPT ch10
Java9e PPT ch10
Chapter 10
Introduction to Inheritance
1
Objectives
© 2019 Cengage. 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 2
classroom use.
Learning About the Concept
of Inheritance
• Inheritance
• A mechanism that enables one class to inherit both the behavior and the attributes of
another class
• Apply your knowledge of a general category to more specific objects
© 2019 Cengage. 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 3
classroom use.
Diagramming Inheritance Using
the UML (1 of 4)
• Unified Modeling Language (UML)
• Consists of many types of diagrams
• Class diagram
• A visual tool
• Provides an overview of a class
© 2019 Cengage. 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 4
classroom use.
Diagramming Inheritance Using
the UML (2 of 4)
© 2019 Cengage. 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 5
classroom use.
Diagramming Inheritance Using
the UML (3 of 4)
© 2019 Cengage. 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 6
classroom use.
Diagramming Inheritance Using
the UML (4 of 4)
• Use inheritance to create a derived class
• Saves time
• Reduces errors
• Reduces the amount of new learning required to use a new class
© 2019 Cengage. 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 7
classroom use.
Inheritance Terminology
• Base class
• Used as a basis for inheritance
• Also called:
- Superclass
- Parent class
• Derived class
• Inherits from a base class
• Always “is a” case or an example of a more general base class
• Also called:
- Subclass
- Child class
© 2019 Cengage. 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 8
classroom use.
Extending Classes (1 of 2)
• Keyword extends
• Used to achieve inheritance in Java
• Example:
public class EmployeeWithTerritory extends Employee
© 2019 Cengage. 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 9
classroom use.
Extending Classes (2 of 2)
© 2019 Cengage. 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 10
classroom use.
Overriding Superclass Methods (1 of 2)
© 2019 Cengage. 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 11
classroom use.
Overriding Superclass Methods (2 of 2)
© 2019 Cengage. 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 12
classroom use.
Calling Constructors During Inheritance (1
of 3)
• When you instantiate an object that is a member of a subclass, you call at least
two constructors:
• The constructor for the base class
• The constructor for the extended class
• The superclass constructor must execute first
• When the superclass contains a default constructor, the execution of the
superclass constructor is transparent
© 2019 Cengage. 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 13
classroom use.
Calling Constructors During Inheritance (2
of 3)
© 2019 Cengage. 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 14
classroom use.
Calling Constructors During Inheritance (3
of 3)
© 2019 Cengage. 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 15
classroom use.
Using Superclass Constructors
That Require Arguments (1 of 2)
• When you write your own constructor
• You replace the automatically supplied version
• When extending a superclass with constructors that require arguments
• The subclass must provide the superclass constructor with the arguments it needs
© 2019 Cengage. 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 16
classroom use.
Using Superclass Constructors
That Require Arguments (2 of 2)
• When a superclass has a default constructor
• You can create a subclass with or without its own constructor
• When a superclass contains only constructors that require arguments
• You must include at least one constructor for each subclass you create
- The first statement within each constructor must call one of the superclass constructors
© 2019 Cengage. 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 17
classroom use.
Accessing Superclass Methods (1 of 2)
© 2019 Cengage. 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 18
classroom use.
Accessing Superclass Methods (2 of 2)
© 2019 Cengage. 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 19
classroom use.
Comparing this and super
© 2019 Cengage. 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 20
classroom use.
Employing Information Hiding (1 of 3)
© 2019 Cengage. 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 21
classroom use.
Employing Information Hiding (2 of 3)
© 2019 Cengage. 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 22
classroom use.
Employing Information Hiding (3 of 3)
• Keyword protected
• Provides an intermediate level of security between public and private access
• Can be used within its own class or in any classes extended from that class
• Cannot be used by “outside” classes
© 2019 Cengage. 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 23
classroom use.
Methods You Cannot Override
• static methods
• final methods
• Methods within final classes
© 2019 Cengage. 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 24
classroom use.
A Subclass Cannot Override static
Methods in Its Superclass (1 of 2)
• A subclass cannot override methods declared static in the superclass
• A subclass can hide a static method in the superclass by declaring a
static method with the same signature as the static method in the
superclass
• Then call the new static method from within the subclass or in another class by
using a subclass object
• Within the static method of a subclass, you cannot access the parent method
using the super object
• Although a child class cannot inherit its parent’s static methods, it can
access its parent’s static methods in the same way any other class can
© 2019 Cengage. 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 25
classroom use.
A Subclass Cannot Override static
Methods in Its Superclass (2 of 2)
© 2019 Cengage. 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 26
classroom use.
A Subclass Cannot Override final
Methods in Its Superclass (1 of 2)
• A subclass cannot override methods declared final in the superclass
• final modifier
• Does not allow the method to be overridden
• Virtual method calls
• Default in Java
• The method used is determined when the program runs
• The object type might not be known until the method executes
© 2019 Cengage. 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 27
classroom use.
A Subclass Cannot Override final
Methods in Its Superclass (2 of 2)
• Advantages to making the method final:
• The compiler knows only one version of the method exists
• The compiler knows which method version will be used
• A program’s performance can be optimized by removing calls to final methods
- Inlining the code: each method call is replaced with the expanded code of the method’s
definition
© 2019 Cengage. 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 28
classroom use.
A Subclass Cannot Override Methods
in a final Superclass (1 of 2)
• When a class is declared final:
• All of its methods are final regardless of which access modifier precedes the
method name
• It cannot be a parent class
© 2019 Cengage. 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 29
classroom use.
A Subclass Cannot Override Methods
in a final Superclass (2 of 2)
© 2019 Cengage. 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 30
classroom use.
Don’t Do It
© 2019 Cengage. 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 31
classroom use.
Summary (1 of 2)
• Inheritance
• A mechanism that enables one class to inherit both the behavior and the attributes of
another class
• Keyword extends
• Used to achieve inheritance in Java
• Polymorphism
• The act of using the same method name to indicate different implementations
© 2019 Cengage. 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 32
classroom use.
Summary (2 of 2)
© 2019 Cengage. 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 33
classroom use.