Relationships Among Classes
Relationships Among Classes
Classes
Java™ How to Program, 10/e
Late Objects Version
Readings
Chapter 8: Classes and Objects: A Deeper Look
Chapter 9: Object-Oriented Programming: Inheritance
as a has-a relationship.
The following example presents a class Date (Fig.
Employee
- firstName : String
- lastName : String
- birthDate : Date
- hireDate: Date
<<constructor>> Employee(firstName: String, lastName: String, birthDate : Date, hireDate: Date)
+ toString() : String
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
9.1 Introduction
Inheritance
A new class (subclass) is created by acquiring an existing
class’s (superclass) members and possibly embellishing them
with new or modified capabilities.
Can save time during program development by basing new
classes on existing proven and debugged high-quality software.
Increases the likelihood that a system will be implemented and
maintained effectively.
Java supports only single inheritance, in which each class is
derived from exactly one direct superclass.
The Inheritance is sometimes referred to as a is-a
relationship.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
9.1 Introduction (Cont.)
A subclass can be a superclass of future subclasses.
A subclass can add its own fields and methods (becomes
more specific than its superclass)
The direct superclass is the superclass from which the
subclass explicitly inherits.
An indirect superclass is any class above the direct
superclass in the class hierarchy.
The Java class hierarchy begins with class Object (in
package java.lang)
Every class in Java directly or indirectly extends (or “inherits
from”) Object.
inherited method
It is a compilation error to override a method with a
https://www.onlyjavatech.com/inheritance-in-java/:Reference
https://www.onlyjavatech.com/inheritance-in-java/:Reference
46
9.6 Class Object
All classes in Java inherit directly or indirectly from class
Object, so its 11 methods are inherited by all other classes.
Figure 9.12 summarizes Object’s methods.
Every array has an overridden clone method that copies the
array.
If the array stores references to objects, the objects are not copied—a
shallow copy is performed.