Inheritance: Starting Out With Java: From Control Structures Through Objects Fifth Edition
Inheritance: Starting Out With Java: From Control Structures Through Objects Fifth Edition
Inheritance
Starting Out with Java:
From Control Structures through Objects
Fifth Edition
by Tony Gaddis
Chapter Topics
Chapter 10 discusses the following main topics:
What Is Inheritance?
Calling the Superclass Constructor
Overriding Superclass Methods
Protected Members
Chains of Inheritance
The Object Class
Polymorphism
Abstract Classes and Abstract Methods
Interfaces
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
10-2
What is Inheritance?
Generalization vs. Specialization
Real-life objects are typically specialized versions of
other more general objects.
The term insect describes a very general type of
creature with numerous characteristics.
Grasshoppers and bumblebees are insects
They share the general characteristics of an insect.
However, they have special characteristics of their own.
grasshoppers have a jumping ability, and
bumblebees have a stinger.
10-3
Inheritance
Insect
Contains those attributes
and methods that are
shared by all insects.
BumbleBee
Grasshopper
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
10-4
The is a Relationship
The relationship between a superclass and an inherited
class is called an is a relationship.
A grasshopper is a insect.
A poodle is a dog.
A car is a vehicle.
10-5
The is a Relationship
We can extend the capabilities of a class.
Inheritance involves a superclass and a subclass.
The superclass is the general class and
the subclass is the specialized class.
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
10-6
Inheritance
The subclass inherits fields and methods from the
superclass without any of them being rewritten.
New fields and methods may be added to the subclass.
The Java keyword, extends, is used on the class header
to define the subclass.
public class FinalExam extends GradedActivity
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
10-7
FinaExam
- numQuestions : int
- pointsEach : double
- numMissed : int
+ FinalExam(questions : int,
missed : int)
+ getPointsEach() : double
+ getNumMissed() : int
Example:
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
GradedActivity.java,
GradeDemo.java,
FinalExam.java,
FinalExamDemo.java
10-8
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
10-9
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
10-10
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
10-11
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
10-12
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
10-13
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
10-14
CurvedActivity
- rawScore : double
- percentage : double
+ CurvedActivity
(percent : double)
+ setScore(s : double) : void
+ getRawScore() : double
+ getPercentage() : double
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
10-15
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
10-16
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
10-17
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
10-18
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
10-19
Protected Members
Protected members of class:
may be accessed by methods in a subclass, and
by methods in the same package as the class.
10-20
Protected Members
Using protected instead of private makes some tasks
easier.
However, any class that is derived from the class, or is in the
same package, has unrestricted access to the protected
member.
It is always better to make all fields private and then
provide public methods for accessing those fields.
If no access specifier for a class member is provided, the class
member is given package access by default.
Any method in the same package may access the member.
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
10-21
Access Specifiers
Access
Modifier
default
(no modifier)
Yes
Yes
Public
Yes
Yes
Protected
Yes
Yes
Private
No
No
Access
Modifier
Accessible to a subclass
outside the package?
default
(no modifier)
No
No
Public
Yes
Yes
Protected
Yes
No
Private
No
No
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
10-22
Chains of Inheritance
A superclass can also be derived from another
class.
Object
Example:
GradedActivity.java
PassFailActivity.java
PassFailExam.java
PassFailExamDemo.java
GradedActivity
PassFailActivity
PassFailExam
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
10-23
Chains of Inheritance
Classes often are depicted graphically in a class
hierarchy.
A class hierarchy shows the inheritance
relationships between classes.
GradedActivity
FinalExam
PassFailActivity
PassFailExam
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
10-24
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
10-25
10-26
Polymorphism
A reference variable can reference objects of classes that are derived
from the variables class.
GradedActivity exam;
We can use the exam variable to reference a GradedActivity
object.
exam = new GradedActivity();
The GradedActivity class is also used as the superclass for the
FinalExam class.
An object of the FinalExam class is a GradedActivity object.
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
10-27
Polymorphism
A GradedActivity variable can be used to reference a
FinalExam object.
GradedActivity exam = new FinalExam(50, 7);
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
10-28
Polymorphism
Other legal polymorphic references:
GradedActivity exam1 = new FinalExam(50, 7);
GradedActivity exam2 = new PassFailActivity(70);
GradedActivity exam3 = new PassFailExam(100, 10, 70);
10-29
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
10-30
Polymorphism
It is the objects type, rather than the reference type,
that determines which method is called.
Example:
Polymorphic.java
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
10-31
Abstract Classes
An abstract class cannot be instantiated, but other classes are
derived from it.
An Abstract class serves as a superclass for other classes.
The abstract class represents the generic or abstract form of all
the classes that are derived from it.
A class becomes abstract when you place the abstract key word
in the class definition.
public abstract class ClassName
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
10-32
Abstract Methods
An abstract method has no body and must be
overridden in a subclass.
An abstract method is a method that appears in a
superclass, but expects to be overridden in a subclass.
An abstract method has only a header and no body.
AccessSpecifier abstract ReturnType MethodName(ParameterList);
Example:
Student.java, CompSciStudent.java, CompSciStudentDemo.java
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
10-33
Abstract Methods
Notice that the key word abstract appears in the header, and
that the header ends with a semicolon.
public abstract void setValue(int value);
10-34
Interfaces
An interface is similar to an abstract class that has all
abstract methods.
It cannot be instantiated, and
all of the methods listed in an interface must be written elsewhere.
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
10-35
Interfaces
The general format of an interface definition:
public interface InterfaceName
{
(Method headers...)
}
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
10-36
Interfaces
If a class implements an interface, it uses the
implements keyword in the class header.
public class FinalExam3 extends GradedActivity
implements Relatable
Example:
GradedActivity.java
Relatable.java
FinalExam3.java
InterfaceDemo.java
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
10-37
Fields in Interfaces
An interface can contain field declarations:
all fields in an interface are treated as final and static.
10-38
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
10-39
Interfaces in UML
GradedActivity
FinalExam3
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Relatable
10-40
RetailItem.java
CompactDisc.java
DvdMovie.java
PolymorphicInterfaceDemo.java
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
10-41
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
10-42
2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
10-43