CS102 · Inheritance · Annotated SlidesInheritance
Original
Overview

Inheritance

Superclasses and subclasses, direct vs. indirect superclasses and Object, is-a vs. has-a, the CommissionEmployee / BasePlusCommissionEmployee example with and without inheritance, constructors and super(...), overriding with @Override, and private members in a hierarchy.

How to read this page

Every original slide is shown unchanged. Yellow marks on a slide point to a numbered note: hover or tap a mark to jump to its note, and hover a note to see its marks. Questions tagged Past exam appeared on a real paper; Prediction questions are likely angles, not confirmed. Pick an answer to each question: a wrong pick tells you why it is wrong, so you can try again.

All 26 slides
  1. 1 Inheritance
  2. 2 Learning Outcomes
  3. 3 Introduction
  4. 4 Introduction (Cont.)
  5. 5 Introduction (Cont.)
  6. 6 Inheritance
  7. 7 Superclasses and Subclasses
  8. 8 Slide 8
  9. 9 Superclasses and Subclasses (Cont.)
  10. 10 Slide 10
  11. 11 Relationship
  12. 12 Superclasses and Subclasses (Cont.)
  13. 13 Superclasses and Subclasses (Cont.)
  14. 14 Example Without Using Inheritance
  15. 15 See code Without Using Inheritance
  16. 16 Relationship between Superclasses and Subclasses
  17. 17 Example Using Inheritance
  18. 18 See Code Using Inheritance
  19. 19 Creating and Using a CommissionEmployee Class
  20. 20 Creating and Using a CommissionEmployee Class (Cont.)
  21. 21 Creating and Using a BasePlus- CommissionEmployee Class
  22. 22 Creating and Using a BasePlus- CommissionEmployee Class (Cont.)
  23. 23 Creating and Using a BasePlus- CommissionEmployee Class (Cont.)
  24. 24 Creating a CommissionEmployee – BasePlusCommissionEmployee Inheritance Hierarchy
  25. 25 Creating a CommissionEmployee – BasePlusCommissionEmployee Inheritance Hierarchy (Cont.)
  26. 26 Creating a CommissionEmployee – BasePlusCommissionEmployee Inheritance Hierarchy (Cont.)
Slide 1 / 26

Inheritance

Slide 1: Inheritance
Slide 2 / 26

Learning Outcomes

Slide 2: Learning Outcomes
Slide 3 / 26

Introduction

Slide 3: Introduction
Slide 4 / 26

Introduction (Cont.)

Slide 4: Introduction (Cont.)

Exam angle

PredictionQ1

Which keyword makes a class inherit from another?

Slide 5 / 26

Introduction (Cont.)

Slide 5: Introduction (Cont.)

Notes

  1. Direct superclass: the one you extend. Indirect: higher up. Every class ultimately extends Object.

Exam angle

PredictionQ1

Java supports multiple inheritance of classes (a class can extend two classes).

Slide 6 / 26

Inheritance

Slide 6: Inheritance
Slide 7 / 26

Superclasses and Subclasses

Slide 7: Superclasses and Subclasses
Slide 8 / 26

Slide 8

Slide 8: Slide 8
Slide 9 / 26

Superclasses and Subclasses (Cont.)

Slide 9: Superclasses and Subclasses (Cont.)
Slide 10 / 26

Slide 10

Slide 10: Slide 10
Slide 11 / 26

Relationship

Slide 11: Relationship

Notes

  1. is-a = inheritance (a Car is a Vehicle). has-a = composition (a Car has an Engine).

Exam angle

PredictionQ1

“A Manager is an Employee” suggests:

Slide 12 / 26

Superclasses and Subclasses (Cont.)

Slide 12: Superclasses and Subclasses (Cont.)
Slide 13 / 26

Superclasses and Subclasses (Cont.)

Slide 13: Superclasses and Subclasses (Cont.)
Slide 14 / 26

Example Without Using Inheritance

Slide 14: Example Without Using Inheritance
Slide 15 / 26

See code Without Using Inheritance

Slide 15: See code Without Using Inheritance
Slide 16 / 26

Relationship between Superclasses and Subclasses

Slide 16: Relationship between Superclasses and Subclasses
Slide 17 / 26

Example Using Inheritance

Slide 17: Example Using Inheritance
Slide 18 / 26

See Code Using Inheritance

Slide 18: See Code Using Inheritance
Slide 19 / 26

Creating and Using a CommissionEmployee Class

Slide 19: Creating and Using a CommissionEmployee Class

Exam angle

PredictionQ1

Constructors are inherited by subclasses.

Slide 20 / 26

Creating and Using a CommissionEmployee Class (Cont.)

Slide 20: Creating and Using a CommissionEmployee Class (Cont.)

Notes

  1. Trap Overloading = same name, different parameters (same class). Overriding = same signature in a subclass.

Exam angle

PredictionQ1

A subclass method with the same signature as a superclass method:

Slide 21 / 26

Creating and Using a BasePlus- CommissionEmployee Class

Slide 21: Creating and Using a BasePlus- CommissionEmployee Class
Slide 22 / 26

Creating and Using a BasePlus- CommissionEmployee Class (Cont.)

Slide 22: Creating and Using a BasePlus- CommissionEmployee Class (Cont.)
Slide 23 / 26

Creating and Using a BasePlus- CommissionEmployee Class (Cont.)

Slide 23: Creating and Using a BasePlus- CommissionEmployee Class (Cont.)
Slide 24 / 26

Creating a CommissionEmployee – BasePlusCommissionEmployee Inheritance Hierarchy

Slide 24: Creating a CommissionEmployee – BasePlusCommissionEmployee Inheritance Hierarchy

Notes

  1. class BasePlusCommissionEmployee extends CommissionEmployee: only the base salary is new.

Slide 25 / 26

Creating a CommissionEmployee – BasePlusCommissionEmployee Inheritance Hierarchy (Cont.)

Slide 25: Creating a CommissionEmployee – BasePlusCommissionEmployee Inheritance Hierarchy (Cont.)

Exam angle

PredictionQ1

Where must the call super(first, last, ssn) appear in a subclass constructor?

Slide 26 / 26

Creating a CommissionEmployee – BasePlusCommissionEmployee Inheritance Hierarchy (Cont.)

Slide 26: Creating a CommissionEmployee – BasePlusCommissionEmployee Inheritance Hierarchy (Cont.)

Notes

  1. A subclass can’t access the superclass’s PRIVATE fields directly → use get methods (or protected).

Exam angle

PredictionQ1

A subclass can directly read its superclass’s private instance variables.