CS102 · Polymorphism · Annotated SlidesPolymorphism
Original
Overview

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. 1 Polymorphism
  2. 2 Slide 2
  3. 3 Polymorphism
  4. 4 Example: Suppose we create a program that simulates
  5. 5 Each specific type of responds to a message
  6. 6 With polymorphism, we can design and implement
  7. 7 Example: Quadrilaterals
  8. 8 In the next example, we aim a superclass reference at a
  9. 9 A superclass object cannot be treated as a subclass object,
  10. 10 Slide 10
  11. 11 Slide 11
  12. 12 Slide 12
  13. 13 Slide 13
  14. 14 When a superclass variable contains a reference to a
  15. 15 Abstract classes
  16. 16 Classes that can be used to instantiate objects are called
  17. 17 You make a class abstract by declaring it with keyword
  18. 18 Cannot instantiate objects of abstract superclasses, but
  19. 19 Polymorphism is particularly effective for implementing so-
  20. 20 Use an abstract method and polymorphism to perform
  21. 21 abstract class Employee represents the general
  22. 22 Slide 22
  23. 23 Abstract superclass Employee declares the
  24. 24 Class Employee (Fig. 10.4) provides methods
  25. 25 The diagram in Fig. 10.3 shows each of the five classes
  26. 26 Slide 26
  27. 27 Fig 10_04_09 code
  28. 28 Fig 10_04_09 code
  29. 29 Fig 10_04_09 code
  30. 30 Fig 10_04_09 code
  31. 31 Slide 31
  32. 32 Slide 32
  33. 33 Slide 33
  34. 34 Fig. 10.9 creates an object of each of the four concrete.
  35. 35 All calls to method toString and earnings are
  36. 36 Slide 36
  37. 37 Slide 37
  38. 38 Every object in Java knows its own class and can
  39. 39 There are four ways to assign superclass and subclass
  40. 40 Attempting to assign a superclass reference to a
  41. 41 A method in a superclass cannot be overridden in
  42. 42 A class cannot be a superclass (i.e., a class cannot
  43. 43 An interface describes a set of methods that can be
  44. 44 Interfaces offer a capability requiring that unrelated
  45. 45 what
  46. 46 An interface declaration begins with the keyword
  47. 47 To use an interface, a concrete class must specify that it
  48. 48 Now consider different classes that
  49. 49 A program component that contains headings
  50. 50 To implement a method, a class must
  51. 51 Slide 51
  52. 52 Slide 52
  53. 53 Slide 53
  54. 54 An interface is often used when disparate (i.e.,
  55. 55 An interface is often used in place of an abstract
  56. 56 Once a class implements an interface, all objects of that
  57. 57 Our next example reexamines the payroll system of
  58. 58 Next example builds an application that can determine
  59. 59 Fig. 10.10 shows the accounts payable hierarchy.
  60. 60 Slide 60
  61. 61 Slide 61
  62. 62 When a class implements an interface, it makes a contract
  63. 63 A modified SalariedEmployee class that extends
  64. 64 See code
  65. 65 Objects of any subclasses of a class that an
  66. 66 See Code
  67. 67 Slide 67
  68. 68 Slide 68
  69. 69 Slide 69
  70. 70 Java does not allow subclasses to inherit from more
  71. 71 The Java API’s (Application Programming
Slide 1 / 71

Polymorphism

Slide 1: Polymorphism
Slide 2 / 71

Slide 2

Slide 2: Slide 2
Slide 3 / 71

Polymorphism

Slide 3: Polymorphism

Notes

  1. Polymorphism: write code for the superclass and it works for every subclass (“program in the general”).

Slide 4 / 71

Example: Suppose we create a program that simulates

Slide 4: Example: Suppose we create a program that simulates
Slide 5 / 71

Each specific type of responds to a message

Slide 5: Each specific type of responds to a message

Notes

  1. Same message (move) → different behaviour per object (fish swims, bird flies).

Slide 6 / 71

With polymorphism, we can design and implement

Slide 6: With polymorphism, we can design and implement
Slide 7 / 71

Example: Quadrilaterals

Slide 7: Example: Quadrilaterals
Slide 8 / 71

In the next example, we aim a superclass reference at a

Slide 8: In the next example, we aim a superclass reference at a

Notes

  1. A superclass reference can point to a subclass object; the OBJECT’s type decides which method runs.

Exam angle

PredictionQ1

Employee e = new SalariedEmployee(); e.earnings(); runs which earnings()?

Slide 9 / 71

A superclass object cannot be treated as a subclass object,

Slide 9: A superclass object cannot be treated as a subclass object,
Slide 10 / 71

Slide 10

Slide 10: Slide 10
Slide 11 / 71

Slide 11

Slide 11: Slide 11
Slide 12 / 71

Slide 12

Slide 12: Slide 12
Slide 13 / 71

Slide 13

Slide 13: Slide 13
Slide 14 / 71

When a superclass variable contains a reference to a

Slide 14: When a superclass variable contains a reference to a

Notes

  1. Memory trick Compiler checks the REFERENCE type; JVM runs the OBJECT type.

Slide 15 / 71

Abstract classes

Slide 15: Abstract classes
Slide 16 / 71

Classes that can be used to instantiate objects are called

Slide 16: Classes that can be used to instantiate objects are called
Slide 17 / 71

You make a class abstract by declaring it with keyword

Slide 17: You make a class abstract by declaring it with keyword

Exam angle

PredictionQ1

A class that contains an abstract method must itself be declared abstract.

PredictionQ2

Constructors can be declared abstract.

Slide 18 / 71

Cannot instantiate objects of abstract superclasses, but

Slide 18: Cannot instantiate objects of abstract superclasses, but

Exam angle

PredictionQ1

Given abstract class Shape, which is legal?

Slide 19 / 71

Polymorphism is particularly effective for implementing so-

Slide 19: Polymorphism is particularly effective for implementing so-
Slide 20 / 71

Use an abstract method and polymorphism to perform

Slide 20: Use an abstract method and polymorphism to perform
Slide 21 / 71

abstract class Employee represents the general

Slide 21: abstract class Employee represents the general
Slide 22 / 71

Slide 22

Slide 22: Slide 22
Slide 23 / 71

Abstract superclass Employee declares the

Slide 23: Abstract superclass Employee declares the
Slide 24 / 71

Class Employee (Fig. 10.4) provides methods

Slide 24: Class Employee (Fig. 10.4) provides methods
Slide 25 / 71

The diagram in Fig. 10.3 shows each of the five classes

Slide 25: The diagram in Fig. 10.3 shows each of the five classes
Slide 26 / 71

Slide 26

Slide 26: Slide 26
Slide 27 / 71

Fig 10_04_09 code

Slide 27: Fig 10_04_09 code
Slide 28 / 71

Fig 10_04_09 code

Slide 28: Fig 10_04_09 code
Slide 29 / 71

Fig 10_04_09 code

Slide 29: Fig 10_04_09 code
Slide 30 / 71

Fig 10_04_09 code

Slide 30: Fig 10_04_09 code
Slide 31 / 71

Slide 31

Slide 31: Slide 31
Slide 32 / 71

Slide 32

Slide 32: Slide 32
Slide 33 / 71

Slide 33

Slide 33: Slide 33
Slide 34 / 71

Fig. 10.9 creates an object of each of the four concrete.

Slide 34: Fig. 10.9 creates an object of each of the four concrete.

Notes

  1. Process objects polymorphically; give BasePlusCommission employees a 10% raise using instanceof.

Slide 35 / 71

All calls to method toString and earnings are

Slide 35: All calls to method toString and earnings are
Slide 36 / 71

Slide 36

Slide 36: Slide 36
Slide 37 / 71

Slide 37

Slide 37: Slide 37

Notes

  1. Downcasting to the wrong type throws ClassCastException → check with instanceof first.

Exam angle

PredictionQ1

Before downcasting an Employee reference to BasePlusCommissionEmployee you should check with:

Slide 38 / 71

Every object in Java knows its own class and can

Slide 38: Every object in Java knows its own class and can
Slide 39 / 71

There are four ways to assign superclass and subclass

Slide 39: There are four ways to assign superclass and subclass
Slide 40 / 71

Attempting to assign a superclass reference to a

Slide 40: Attempting to assign a superclass reference to a
Slide 41 / 71

A method in a superclass cannot be overridden in

Slide 41: A method in a superclass cannot be overridden in
Slide 42 / 71

A class cannot be a superclass (i.e., a class cannot

Slide 42: A class cannot be a superclass (i.e., a class cannot

Exam angle

PredictionQ1

A class declared final:

Slide 43 / 71

An interface describes a set of methods that can be

Slide 43: An interface describes a set of methods that can be
Slide 44 / 71

Interfaces offer a capability requiring that unrelated

Slide 44: Interfaces offer a capability requiring that unrelated
Slide 45 / 71

what

Slide 45: what
Slide 46 / 71

An interface declaration begins with the keyword

Slide 46: An interface declaration begins with the keyword

Exam angle

PredictionQ1

Fields declared in an interface are implicitly:

Slide 47 / 71

To use an interface, a concrete class must specify that it

Slide 47: To use an interface, a concrete class must specify that it
Slide 48 / 71

Now consider different classes that

Slide 48: Now consider different classes that
Slide 49 / 71

A program component that contains headings

Slide 49: A program component that contains headings
Slide 50 / 71

To implement a method, a class must

Slide 50: To implement a method, a class must
Slide 51 / 71

Slide 51

Slide 51: Slide 51
Slide 52 / 71

Slide 52

Slide 52: Slide 52
Slide 53 / 71

Slide 53

Slide 53: Slide 53
Slide 54 / 71

An interface is often used when disparate (i.e.,

Slide 54: An interface is often used when disparate (i.e.,
Slide 55 / 71

An interface is often used in place of an abstract

Slide 55: An interface is often used in place of an abstract
Slide 56 / 71

Once a class implements an interface, all objects of that

Slide 56: Once a class implements an interface, all objects of that
Slide 57 / 71

Our next example reexamines the payroll system of

Slide 57: Our next example reexamines the payroll system of
Slide 58 / 71

Next example builds an application that can determine

Slide 58: Next example builds an application that can determine
Slide 59 / 71

Fig. 10.10 shows the accounts payable hierarchy.

Slide 59: Fig. 10.10 shows the accounts payable hierarchy.
Slide 60 / 71

Slide 60

Slide 60: Slide 60
Slide 61 / 71

Slide 61

Slide 61: Slide 61
Slide 62 / 71

When a class implements an interface, it makes a contract

Slide 62: When a class implements an interface, it makes a contract
Slide 63 / 71

A modified SalariedEmployee class that extends

Slide 63: A modified SalariedEmployee class that extends
Slide 64 / 71

See code

Slide 64: See code
Slide 65 / 71

Objects of any subclasses of a class that an

Slide 65: Objects of any subclasses of a class that an
Slide 66 / 71

See Code

Slide 66: See Code
Slide 67 / 71

Slide 67

Slide 67: Slide 67
Slide 68 / 71

Slide 68

Slide 68: Slide 68
Slide 69 / 71

Slide 69

Slide 69: Slide 69
Slide 70 / 71

Java does not allow subclasses to inherit from more

Slide 70: Java does not allow subclasses to inherit from more

Exam angle

PredictionQ1

A Java class can implement more than one interface.

Slide 71 / 71

The Java API’s (Application Programming

Slide 71: The Java API’s (Application Programming