0% found this document useful (0 votes)
41 views50 pages

Relationships Among Classes

This document provides an overview of object-oriented programming concepts like inheritance, subclasses, and polymorphism in Java. It discusses how subclasses inherit and extend the attributes and behaviors of their parent superclasses, and how subclasses can override or modify inherited methods. Examples are provided of superclass-subclass relationships between classes like Employee and CommissionEmployee. The key aspects covered are inheritance hierarchies, protected vs private vs public access modifiers, calling superclass constructors from subclasses, and overriding methods.

Uploaded by

pilotbairexd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views50 pages

Relationships Among Classes

This document provides an overview of object-oriented programming concepts like inheritance, subclasses, and polymorphism in Java. It discusses how subclasses inherit and extend the attributes and behaviors of their parent superclasses, and how subclasses can override or modify inherited methods. Examples are provided of superclass-subclass relationships between classes like Employee and CommissionEmployee. The key aspects covered are inheritance hierarchies, protected vs private vs public access modifiers, calling superclass constructors from subclasses, and overriding methods.

Uploaded by

pilotbairexd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 50

Relationships among

Classes
Java™ How to Program, 10/e
Late Objects Version

© Copyright 1992-2015 by Pearson Education, Inc. All Rights


Reserved.
References & Reading
 The content is mainly selected (sometimes modified)
from the original slides provided by the authors of the
textbook

 Readings
 Chapter 8: Classes and Objects: A Deeper Look
 Chapter 9: Object-Oriented Programming: Inheritance

©1992-2015 by Pearson Education, Inc.


All Rights Reserved.
Outline
8.8 Composition
9.1 Introduction
9.2 Superclasses and Subclasses
9.3 protected Members
9.4 Relationship Between Superclasses and Subclasses
9.5 Constructors in Subclasses
9.6 Class Object

©1992-2015 by Pearson Education, Inc.


All Rights Reserved.
8.8 Composition
 A class can have references to objects of other classes
as members.
 This is called composition and is sometimes referred to

as a has-a relationship.
 The following example presents a class Date (Fig.

8.7), a class Employee (Fig. 8.8) that has 2 Dates,


and a class EmployeeTest (Fig. 8.9) to demonstrate
the composition.
 UML representation of Composition:

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
Date
- month: int
- day: int
- year: int
<<constructor>> Date(month: int, day: int, year : int)
+ toString() : String

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.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
9.2 Superclasses and Subclasses
 Figure 9.1 lists several simple examples of superclasses
and subclasses
 Superclasses tend to be “more general” and subclasses “more
specific.”

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
9.2 Superclasses and Subclasses
(Cont.)
 A superclass exists in a hierarchical relationship with its
subclasses.
 Fig. 9.2 shows a sample university community class hierarchy
 Also called an inheritance hierarchy.
 Each arrow in the hierarchy represents an is-a relationship.
 Follow the arrows upward in the class hierarchy
 “an Employee is a CommunityMember”
 “a Teacher is a Faculty member.”
 CommunityMember is the direct superclass of Employee,
Student and Alumnus, and is an indirect superclass of all the
other classes in the diagram.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
9.2 Superclasses and Subclasses
(Cont.)
 Objects of all classes that extend a common superclass
can be treated as objects of that superclass.
 Commonality expressed in the members of the superclass.
 Inheritance issue
 All members of all superclasses are inherited (except private)
 A subclass can inherit methods that it does not need or should
not have.
 Even when a superclass method is appropriate for a subclass,
that subclass often needs a customized version of the method.
 The subclass can override (redefine) the superclass method
with an appropriate implementation.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
9.3 protected Members
 A class’s public members are accessible wherever the
program has a reference to an object of that class or one of
its subclasses.
 A superclass’s private members are accessible only
within the class itself but they are hidden from its subclasses
(can be accessed only through the public or protected
methods inherited from the superclass)
 protected access is an intermediate level of access
between public and private.
 A superclass’s protected members can be accessed by members
of that superclass, by members of its subclasses and by members of
other classes in the same package
 All public and protected superclass members retain their
original access modifier when they become members of the subclass.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
9.3 protected Members (Cont.)
 Subclass methods can refer to public and protected
members inherited from the superclass simply by using the
member names.
 When a subclass method overrides an inherited superclass
method, the superclass version of the method can be
accessed from the subclass by preceding the superclass
method name with keyword super and a dot (.) separator.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
9.4.1 Creating and Using a
CommissionEmployee Class (Cont.)
 Class CommissionEmployee (Fig. 9.10) extends
implicitly class Object (from package java.lang).
 CommissionEmployee inherits Object’s methods.
 If you don’t explicitly specify which class a new class extends,
the class extends Object implicitly.
 Constructors are not inherited.
 The first task of a subclass constructor is to call its

direct superclass’s constructor explicitly (super) or


implicitly
 If the code does not include an explicit call to the

superclass constructor, Java implicitly calls the


superclass’s default or no-argument constructor.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
9.4.1 Creating and Using a
CommissionEmployee Class (Cont.)
 toString is one of the methods that every class
inherits directly or indirectly from class Object.
 Returns a String representing an object.
 Called implicitly whenever an object must be converted to a
String representation.
 Class Object’s toString method returns a
String that includes the name of the object’s class.
 This is primarily a placeholder that can be overridden by a
subclass to specify an appropriate String representation.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
9.4.1 Creating and Using a
CommissionEmployee Class (Cont.)
 To override a superclass method, a subclass must
declare a method with the same signature as the
superclass method
 The optional @Override annotation

 Indicates/ensures that a method should override a superclass


method with the same signature.
 If it does not, a compilation error occurs.
 The CommissionEmployee class overrides
Object’s toString method

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
9.4.1 Creating and Using a
CommissionEmployee Class (Cont.)
Changing inherited method access modifier
 A subclass can change the access modifier of an

inherited method
 It is a compilation error to override a method with a

more restricted access modifier


 A public superclass method cannot become a protected or
private subclass method
 A protected superclass method cannot become a private
subclass method

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
9.4.3 Creating a CommissionEmployee–
BasePlusCommissionEmployee Inheritance Hierarchy

 Class BasePlusCommissionEmployee class extends


class CommissionEmployee
 A BasePlusCommissionEmployee object is a
CommissionEmployee
 Inheritance passes on class CommissionEmployee’s capabilities.
 Class BasePlusCommissionEmployee also has
instance variable baseSalary.
 Subclass BasePlusCommissionEmployee inherits
CommissionEmployee’s instance variables and methods
 Only CommissionEmployee’s public and protected
members are directly accessible in the subclass.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
9.4.3 Creating a CommissionEmployee–
BasePlusCommissionEmployee Inheritance Hierarchy
(Cont.)
 Each subclass constructor must implicitly or explicitly call
one of its superclass’s constructors to initialize the instance
variables inherited from the superclass.
 Superclass constructor call syntax—keyword super, followed by a
set of parentheses containing the superclass constructor arguments.
 Must be the first statement in the constructor’s body.
 If the subclass constructor did not invoke the superclass’s
constructor explicitly, the compiler would attempt to insert a
call to the superclass’s default or no-argument constructor.
 Class CommissionEmployee does not have such a constructor,
so the compiler would issue an error.
 You can explicitly use super() to call the superclass’s no-
argument or default constructor, but this is rarely done.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
9.4.5 CommissionEmployee–BasePlus-
CommissionEmployee Inheritance Hierarchy
Using private Instance Variables
 Methods earnings and toString each invoke

their superclass versions and do not access instance


variab
 Method earnings overrides class the superclass’s
earnings method.
 Calls superclass’s earnings method with
super.earnings().
 Obtains the earnings based on commission alone
 Placing the keyword super and a dot (.) separator
before the superclass method name invokes the
superclass version of an overridden method.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
9.4.5 CommissionEmployee–BasePlus-
CommissionEmployee Inheritance Hierarchy
Using private Instance Variables (Cont.)
 BasePlusCommissionEmployee’s toString

method overrides class CommissionEmployee’s


toString method.
 It creates part of the String representation by calling

CommissionEmployee’s toString method with


the expression super.toString().

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
9.5 Constructors in Subclasses
 Instantiating a subclass object begins a chain of constructor
calls
 The subclass constructor, before performing its own tasks, explicitly
uses super to call one of the constructors in its direct superclass or
implicitly calls the superclass’s default or no-argument constructor
 If the superclass is derived from another class, the
superclass constructor invokes the constructor of the next
class up the hierarchy, and so on.
 The last constructor called in the chain is always Object’s
constructor.
 Original subclass constructor’s body finishes executing last.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
ConstructorChainTest.java
run:
Create new instance of A:
=> Constructor A
Create new instance of B:
=> Constructor A
=> Constructor B
Create new instance of C:
=> Constructor A
=> Constructor B
=> Constructor C
Extra slides

Abeer alsheddi – extra slides


Superclasses and Subclasses
 Types of inheritance are supported in java

https://www.onlyjavatech.com/inheritance-in-java/:Reference

Abeer alsheddi – extra slides 43


Superclasses and Subclasses (Cont.)
 Types of Inheritance are not supported in java.

https://www.onlyjavatech.com/inheritance-in-java/:Reference

Abeer alsheddi – extra slides 44


Constructors in Subclasses
 Problem:
 When a super-class has an argument constructor.
 Because:
 Creating any object of subclass will (by default)
invoke a default constructor of a super-class.
 Solutions:
 At super-class: Add a default constructor.
OR
 At subclass: Call explicitly an argument constructor
of its direct super-class using super() in the first
statement in a subclass constructor.

Abeer alsheddi – extra slide 45


Self-Reading

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.

© Copyright 1992-2015 by Pearson


Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.
© Copyright 1992-2015 by Pearson
Education, Inc. All Rights Reserved.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy