CSO Gaddis Java Chapter10
CSO Gaddis Java Chapter10
Inheritance
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
©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 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.
• A specialized object has:
– all of the characteristics of the general object, plus
– additional characteristics that make it special.
• In object-oriented programming, inheritance is used to
create an “is a” relationship among classes.
©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 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.
• The subclass is based on, or extended from, the superclass.
– Superclasses are also called base classes, and
– subclasses are also called derived classes.
• The relationship of classes can be thought of as parent classes
and child classes.
©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.
©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 10-7
The GradedActivity Example
GradedActivity
Contains those attributes and methods
- score : double that are shared by all graded activities.
©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 10-9
Inheritance, Fields and Methods
• When an instance of the subclass is created, the non-private
methods of the superclass are available through the subclass
object.
setScore(newScore);
©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 10-10
Inheritance and Constructors
• Constructors are not inherited.
• When a subclass is instantiated, the superclass default
constructor is executed first.
• Example:
– SuperClass1.java
– SubClass1.java
– ConstructorDemo1.java
©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 10-11
The Superclass’s Constructor
• The super keyword refers to an object’s superclass.
• The superclass constructor can be explicitly called
from the subclass by using the super keyword.
• Example:
– SuperClass2.java, SubClass2.java, ConstructorDemo2.java
– Rectangle.java, Cube.java, CubeDemo.java
©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 10-12
Calling The Superclass Constructor
• If a parameterized constructor is defined in the
superclass,
– the superclass must provide a no-arg constructor, or
• subclasses must provide a constructor, and
• subclasses must call a superclass constructor.
• Calls to a superclass constructor must be the first
java statement in the subclass constructors.
©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 10-13
Overriding Superclass Methods
• A subclass may have a method with the same
signature as a superclass method.
• The subclass method overrides the superclass
method.
• This is known as method overriding.
• Example:
– GradedActivity.java, CurvedActivity.java,
CurvedActivityDemo.java
©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 10-14
Overriding Superclass Methods
GradedActivity
- score : double
+ setScore(s : double) : void
+ getScore() : double
+ getGrade() : char
©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 10-15
Overriding Superclass Methods
• Recall that a method’s signature consists of:
– the method’s name
– the data types method’s parameters in the order that they
appear.
• A subclass method that overrides a superclass method
must have the same signature as the superclass
method.
• An object of the subclass invokes the subclass’s
version of the method, not the superclass’s.
©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 10-16
Overriding Superclass Methods
• An subclass method can call the overridden superclass method
via the super keyword.
super.setScore(rawScore * percentage);
©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 10-17
Overriding Superclass Methods
• Both overloading and overriding can take place in an
inheritance relationship.
• Overriding can only take place in an inheritance
relationship.
• Example:
– SuperClass3.java,
– SubClass3.java,
– ShowValueDemo.java
©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 10-18
Preventing a Method from Being
Overridden
• The final modifier will prevent the overriding of a
superclass method in a subclass.
©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.
• Java provides a third access specification,
protected.
• A protected member’s access is somewhere between
private and public.
• Example:
– GradedActivity2.java
– FinalExam2.java
– ProtectedDemo.java
©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 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
Accessible to a subclass inside Accessible to all other classes
Access Modifier
the same package? inside the same package?
default
Yes Yes
(no modifier)
Public Yes Yes
Protected Yes Yes
Private No No
©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 10-22
Chains of Inheritance
Example:
GradedActivity.java
GradedActivity
PassFailActivity.java
PassFailExam.java
PassFailExamDemo.java
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
The Object Class
• All Java classes are directly or indirectly derived from a class
named Object.
• Object is in the java.lang package.
• Any class that does not specify the extends keyword is
automatically derived from the Object class.
©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 10-25
The Object Class
• Because every class is directly or indirectly derived
from the Object class:
– every class inherits the Object class’s members.
• example: toString and equals.
• In the Object class, the toString method returns a
string containing the object’s class name and a hash of
its memory address.
• The equals method accepts the address of an object
as its argument and returns true if it is the same as the
calling object’s address.
• Example: ObjectMethods.java
©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 10-26
Polymorphism
• A reference variable can reference objects of classes that are
derived from the variable’s class.
GradedActivity exam;
©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);
©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 10-29
Polymorphism and Dynamic Binding
• If the object of the subclass has overridden a method in the
superclass:
– If the variable makes a call to that method the subclass’s version of the
method will be run.
GradedActivity exam = new PassFailActivity(60);
exam.setScore(70);
System.out.println(exam.getGrade());
©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 10-30
Polymorphism
• It is the object’s type, rather than the reference type,
that determines which method is called.
• Example:
– Polymorphic.java
• You cannot assign a superclass object to a subclass
reference variable.
©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.
©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.
©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 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.
• The purpose of an interface is to specify behavior for other
classes.
• An interface looks similar to a class, except:
– the keyword interface is used instead of the keyword class,
and
– the methods that are specified in an interface have no bodies, only
headers that are terminated by semicolons.
©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 10-35
Interfaces
• The general format of an interface definition:
©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.
• 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.
• Because they automatically become final, you must provide an
initialization value.
public interface Doable
{
int FIELD1 = 1, FIELD2 = 2;
(Method headers...)
}
• In this interface, FIELD1 and FIELD2 are final static
int variables.
• Any class that implements this interface has access to these
variables.
©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 10-38
Implementing Multiple Interfaces
• A class can be derived from only one superclass.
• Java allows a class to implement multiple interfaces.
• When a class implements multiple interfaces, it must provide
the methods specified by all of them.
• To specify multiple interfaces in a class definition, simply list
the names of the interfaces, separated by commas, after the
implements key word.
©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 10-39
Interfaces in UML
FinalExam3 Relatable
©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 10-40
Polymorphism with Interfaces
• Java allows you to create reference variables of an interface
type.
• An interface reference variable can reference any object
that implements that interface, regardless of its class type.
• This is another example of polymorphism.
• Example:
– RetailItem.java
– CompactDisc.java
– DvdMovie.java
– PolymorphicInterfaceDemo.java
©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 10-41
Polymorphism with Interfaces
• In the example code, two RetailItem reference variables,
item1 and item2, are declared.
• The item1 variable references a CompactDisc object and
the item2 variable references a DvdMovie object.
• When a class implements an interface, an inheritance
relationship known as interface inheritance is established.
– a CompactDisc object is a RetailItem, and
– a DvdMovie object is a RetailItem.
©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 10-42
Polymorphism with Interfaces
• A reference to an interface can point to any class that
implements that interface.
• You cannot create an instance of an interface.
©2013 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 10-43