Which keyword makes a class inherit from another?
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 Inheritance
- 2 Learning Outcomes
- 3 Introduction
- 4 Introduction (Cont.)
- 5 Introduction (Cont.)
- 6 Inheritance
- 7 Superclasses and Subclasses
- 8 Slide 8
- 9 Superclasses and Subclasses (Cont.)
- 10 Slide 10
- 11 Relationship
- 12 Superclasses and Subclasses (Cont.)
- 13 Superclasses and Subclasses (Cont.)
- 14 Example Without Using Inheritance
- 15 See code Without Using Inheritance
- 16 Relationship between Superclasses and Subclasses
- 17 Example Using Inheritance
- 18 See Code Using Inheritance
- 19 Creating and Using a CommissionEmployee Class
- 20 Creating and Using a CommissionEmployee Class (Cont.)
- 21 Creating and Using a BasePlus- CommissionEmployee Class
- 22 Creating and Using a BasePlus- CommissionEmployee Class (Cont.)
- 23 Creating and Using a BasePlus- CommissionEmployee Class (Cont.)
- 24 Creating a CommissionEmployee – BasePlusCommissionEmployee Inheritance Hierarchy
- 25 Creating a CommissionEmployee – BasePlusCommissionEmployee Inheritance Hierarchy (Cont.)
- 26 Creating a CommissionEmployee – BasePlusCommissionEmployee Inheritance Hierarchy (Cont.)
Introduction (Cont.)
Exam angle
Introduction (Cont.)
Notes
Direct superclass: the one you extend. Indirect: higher up. Every class ultimately extends Object.
Exam angle
Java supports multiple inheritance of classes (a class can extend two classes).
- Single inheritance; use interfaces for more.
Relationship
Notes
is-a = inheritance (a Car is a Vehicle). has-a = composition (a Car has an Engine).
Exam angle
“A Manager is an Employee” suggests:
Creating and Using a CommissionEmployee Class
Exam angle
Constructors are inherited by subclasses.
Creating and Using a CommissionEmployee Class (Cont.)
Notes
Trap Overloading = same name, different parameters (same class). Overriding = same signature in a subclass.
Exam angle
A subclass method with the same signature as a superclass method:
Creating a CommissionEmployee – BasePlusCommissionEmployee Inheritance Hierarchy
Notes
class BasePlusCommissionEmployee extends CommissionEmployee: only the base salary is new.
Creating a CommissionEmployee – BasePlusCommissionEmployee Inheritance Hierarchy (Cont.)
Exam angle
Where must the call super(first, last, ssn) appear in a subclass constructor?
Creating a CommissionEmployee – BasePlusCommissionEmployee Inheritance Hierarchy (Cont.)
Notes
A subclass can’t access the superclass’s PRIVATE fields directly → use get methods (or protected).
Exam angle
A subclass can directly read its superclass’s private instance variables.
- Use getters or protected access.