Employee e = new SalariedEmployee(); e.earnings(); runs which earnings()?
Polymorphism
Programming in the general: superclass references to subclass objects, dynamic binding, abstract classes and methods, the polymorphic payroll system (Employee hierarchy), instanceof and downcasting, final methods and classes, and interfaces (Measurable, Payable) for unrelated classes.
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 71 slides
- 1 Polymorphism
- 2 Slide 2
- 3 Polymorphism
- 4 Example: Suppose we create a program that simulates
- 5 Each specific type of responds to a message
- 6 With polymorphism, we can design and implement
- 7 Example: Quadrilaterals
- 8 In the next example, we aim a superclass reference at a
- 9 A superclass object cannot be treated as a subclass object,
- 10 Slide 10
- 11 Slide 11
- 12 Slide 12
- 13 Slide 13
- 14 When a superclass variable contains a reference to a
- 15 Abstract classes
- 16 Classes that can be used to instantiate objects are called
- 17 You make a class abstract by declaring it with keyword
- 18 Cannot instantiate objects of abstract superclasses, but
- 19 Polymorphism is particularly effective for implementing so-
- 20 Use an abstract method and polymorphism to perform
- 21 abstract class Employee represents the general
- 22 Slide 22
- 23 Abstract superclass Employee declares the
- 24 Class Employee (Fig. 10.4) provides methods
- 25 The diagram in Fig. 10.3 shows each of the five classes
- 26 Slide 26
- 27 Fig 10_04_09 code
- 28 Fig 10_04_09 code
- 29 Fig 10_04_09 code
- 30 Fig 10_04_09 code
- 31 Slide 31
- 32 Slide 32
- 33 Slide 33
- 34 Fig. 10.9 creates an object of each of the four concrete.
- 35 All calls to method toString and earnings are
- 36 Slide 36
- 37 Slide 37
- 38 Every object in Java knows its own class and can
- 39 There are four ways to assign superclass and subclass
- 40 Attempting to assign a superclass reference to a
- 41 A method in a superclass cannot be overridden in
- 42 A class cannot be a superclass (i.e., a class cannot
- 43 An interface describes a set of methods that can be
- 44 Interfaces offer a capability requiring that unrelated
- 45 what
- 46 An interface declaration begins with the keyword
- 47 To use an interface, a concrete class must specify that it
- 48 Now consider different classes that
- 49 A program component that contains headings
- 50 To implement a method, a class must
- 51 Slide 51
- 52 Slide 52
- 53 Slide 53
- 54 An interface is often used when disparate (i.e.,
- 55 An interface is often used in place of an abstract
- 56 Once a class implements an interface, all objects of that
- 57 Our next example reexamines the payroll system of
- 58 Next example builds an application that can determine
- 59 Fig. 10.10 shows the accounts payable hierarchy.
- 60 Slide 60
- 61 Slide 61
- 62 When a class implements an interface, it makes a contract
- 63 A modified SalariedEmployee class that extends
- 64 See code
- 65 Objects of any subclasses of a class that an
- 66 See Code
- 67 Slide 67
- 68 Slide 68
- 69 Slide 69
- 70 Java does not allow subclasses to inherit from more
- 71 The Java API’s (Application Programming
Polymorphism
Notes
Polymorphism: write code for the superclass and it works for every subclass (“program in the general”).
Each specific type of responds to a message
Notes
Same message (move) → different behaviour per object (fish swims, bird flies).
In the next example, we aim a superclass reference at a
Notes
A superclass reference can point to a subclass object; the OBJECT’s type decides which method runs.
Exam angle
When a superclass variable contains a reference to a
Notes
Memory trick Compiler checks the REFERENCE type; JVM runs the OBJECT type.
You make a class abstract by declaring it with keyword
Exam angle
A class that contains an abstract method must itself be declared abstract.
Constructors can be declared abstract.
Cannot instantiate objects of abstract superclasses, but
Exam angle
Given abstract class Shape, which is legal?
Fig. 10.9 creates an object of each of the four concrete.
Notes
Process objects polymorphically; give BasePlusCommission employees a 10% raise using instanceof.
Slide 37
Notes
Downcasting to the wrong type throws ClassCastException → check with instanceof first.
Exam angle
Before downcasting an Employee reference to BasePlusCommissionEmployee you should check with:
A class cannot be a superclass (i.e., a class cannot
Exam angle
A class declared final:
An interface declaration begins with the keyword
Exam angle
Fields declared in an interface are implicitly:
Java does not allow subclasses to inherit from more
Exam angle
A Java class can implement more than one interface.