Overview of Java’s Support for Inheritance & Polymorphism...
Transcript of Overview of Java’s Support for Inheritance & Polymorphism...
![Page 1: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/1.jpg)
Douglas C. [email protected]
www.dre.vanderbilt.edu/~schmidtProfessor of Computer Science
Institute for Software Integrated Systems
Vanderbilt University Nashville, Tennessee, USA
Overview of Java’s Support for Polymorphism
![Page 2: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/2.jpg)
2
• Understand what polymorphism is & how it’s supported in JavaLearning Objectives in this Lesson
OOP
Abstraction
InheritancePolymorphism
![Page 3: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/3.jpg)
3
Overview of Java’s Support for Polymorphism
![Page 4: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/4.jpg)
4
• Inheritance is nearly always used in conjunction with polymorphism
Overview of Java’s Support for Polymorphism
Abstraction
InheritancePolymorphism
![Page 5: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/5.jpg)
5
• Polymorphism enables transparent customization of methods inherited from a super class
OOP
Abstraction
InheritancePolymorphism
Overview of Java’s Support for Polymorphism
See en.wikipedia.org/wiki/Polymorphism_(computer_science)
![Page 6: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/6.jpg)
6
• Polymorphism & inheritance are essential to the “open/closed principle”Overview of Java’s Support for Polymorphism
See en.wikipedia.org/wiki/Open/closed_principle
![Page 7: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/7.jpg)
7
• Polymorphism & inheritance are essential to the “open/closed principle”• “A class should be open for extension,
but closed for modification”
Overview of Java’s Support for Polymorphism
See en.wikipedia.org/wiki/Open/closed_principle
![Page 8: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/8.jpg)
8
• Polymorphism & inheritance are essential to the “open/closed principle”• “A class should be open for extension,
but closed for modification”• Insulating clients of a class from
modifications makes software morerobust, flexible, & reusable
Overview of Java’s Support for Polymorphism
See www.dre.vanderbilt.edu/~schmidt/OCP.pdf
![Page 9: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/9.jpg)
9
• The “open/closed principle” can be applied in conjunction with patterns to enable extensions without modifying existing classes or apps
Overview of Java’s Support for Polymorphism
See en.wikipedia.org/wiki/Factory_method_pattern
![Page 10: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/10.jpg)
15
• Subclasses defined in accordance with the open/closedprinciple can define their own custom behaviors that are more suitable for a particular use case
Overview of Java’s Support for Polymorphism
<<extends>><<extends>>
![Page 11: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/11.jpg)
16
Overview of Java’s Support for Polymorphism
<<extends>><<extends>>
• Subclasses defined in accordance with the open/closedprinciple can define their own custom behaviors that are more suitable for a particular use case• While still reusing structure
& functionality from super class
![Page 12: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/12.jpg)
17
Overview of Java’s Support for Polymorphism
<<extends>><<extends>>
• Subclasses defined in accordance with the open/closedprinciple can define their own custom behaviors that are more suitable for a particular use case• While still reusing structure
& functionality from super class • e.g., Vector & ArrayList both
inherit AbstractList methods
![Page 13: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/13.jpg)
18
Overview of Java’s Support for Polymorphism
<<extends>><<extends>>
• Subclasses defined in accordance with the open/closedprinciple can define their own custom behaviors that are more suitable for a particular use case• While still reusing structure
& functionality from super class • e.g., Vector & ArrayList both
inherit AbstractList methods
![Page 14: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/14.jpg)
19
Overview of Java’s Support for Polymorphism
<<extends>><<extends>>
See beginnersbook.com/2013/12/difference-between-arraylist-and-vector-in-java
• Subclasses defined in accordance with the open/closedprinciple can define their own custom behaviors that are more suitable for a particular use case• While still reusing structure
& functionality from super class • e.g., Vector & ArrayList both
inherit AbstractList methods
Methods in each subclass emphasize different List properties
![Page 15: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/15.jpg)
20
Method Overriding in Java Polymorphism
![Page 16: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/16.jpg)
21
• Polymorphism in Java occurs when areference to a super class is used torefer to an object of a subclass
HashSet<E>…iterator()add(E e)
ConcurrentHashSet<E>
…iterator()add(E e)
TreeSet<E>…iterator()add(E e)
AbstractSet<E>
…iterator()add(E e)
Method Overriding in Java Polymorphism
![Page 17: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/17.jpg)
22
• Polymorphism in Java occurs when areference to a super class is used torefer to an object of a subclass• Subclass methods can override
super class methods
AbstractSet<E>
…iterator()add(E e)
Method Overriding in Java Polymorphism
HashSet<E>…iterator()add(E e)
ConcurrentHashSet<E>
…iterator()add(E e)
TreeSet<E>…iterator()add(E e)
Any non-final & non-static methods can be overridden
![Page 18: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/18.jpg)
23
• Polymorphism in Java occurs when areference to a super class is used torefer to an object of a subclass• Subclass methods can override
super class methods
AbstractSet<E>
…iterator()add(E e)
Method Overriding in Java Polymorphism
See docs.oracle.com/javase/8/docs/api/java/util/AbstractSet.html
![Page 19: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/19.jpg)
24
• Polymorphism in Java occurs when areference to a super class is used torefer to an object of a subclass• Subclass methods can override
super class methods
AbstractSet<E>
…iterator()add(E e)
Method Overriding in Java Polymorphism
Returns an iterator that can access each
element in a Set
![Page 20: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/20.jpg)
25
• Polymorphism in Java occurs when areference to a super class is used torefer to an object of a subclass• Subclass methods can override
super class methods
AbstractSet<E>
…iterator()add(E e)
Method Overriding in Java Polymorphism
Associates the specified element to the Set
![Page 21: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/21.jpg)
26
• Polymorphism in Java occurs when areference to a super class is used torefer to an object of a subclass• Subclass methods can override
super class methods
AbstractSet<E>
…iterator()add(E e)
Method Overriding in Java Polymorphism
HashSet<E>…iterator()add(E e)
ConcurrentHashSet<E>
…iterator()add(E e)
TreeSet<E>…iterator()add(E e)
Subclasses of AbstractSet can override add(E e) & iterator() differently
![Page 22: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/22.jpg)
27
• Polymorphism in Java occurs when areference to a super class is used torefer to an object of a subclass• Subclass methods can override
super class methods
Method Overriding in Java Polymorphism
Subclasses of AbstractSet have different time & space tradeoffs
HashSet<E>…iterator()add(E e)
ConcurrentHashSet<E>
…iterator()add(E e)
TreeSet<E>…iterator()add(E e)
![Page 23: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/23.jpg)
28
• Polymorphism in Java occurs when areference to a super class is used torefer to an object of a subclass• Subclass methods can override
super class methods
AbstractSet<E>
…iterator()add(E e)
Method Overriding in Java Polymorphism
See docs.oracle.com/javase/8/docs/api/java/util/TreeSet.html
HashSet<E>…iterator()add(E e)
ConcurrentHashSet<E>
…iterator()add(E e)
TreeSet<E>…iterator()add(E e)
![Page 24: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/24.jpg)
29
• Polymorphism in Java occurs when areference to a super class is used torefer to an object of a subclass• Subclass methods can override
super class methods
AbstractSet<E>
…iterator()add(E e)
Method Overriding in Java Polymorphism
See docs.oracle.com/javase/8/docs/api/java/util/HashSet.html
HashSet<E>…iterator()add(E e)
ConcurrentHashSet<E>
…iterator()add(E e)
TreeSet<E>…iterator()add(E e)
![Page 25: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/25.jpg)
30
AbstractSet<E>
…iterator()add(E e)
Method Overriding in Java Polymorphism
HashSet<E>…iterator()add(E e)
ConcurrentHashSet<E>
…iterator()add(E e)
TreeSet<E>…iterator()add(E e)
See Java8/ex19/src/main/java/utils/ConcurrentHashSet.java
• Polymorphism in Java occurs when areference to a super class is used torefer to an object of a subclass• Subclass methods can override
super class methods
![Page 26: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/26.jpg)
31
AbstractSet<E>
…iterator()add(E e)
Method Overriding in Java Polymorphism// Create AbstractSet subclass
AbstractSet<String> absSet = makeSet(setType);
...// Dispatch add(E e) methodabsSet.add(element);
HashSet<E>…iterator()add(E e)
ConcurrentHashSet<E>
…iterator()add(E e)
TreeSet<E>…iterator()add(E e)
A factory method creates the appropriate concrete set subclass instance
![Page 27: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/27.jpg)
32
AbstractSet<E>
…iterator()add(E e)
Method Overriding in Java Polymorphism// Create AbstractSet subclass
AbstractSet<String> absSet = makeSet(setType);
...// Dispatch add(E e) methodabsSet.add(element);
HashSet<E>…iterator()add(E e)
ConcurrentHashSet<E>
…iterator()add(E e)
TreeSet<E>…iterator()add(E e)
The appropriate method is dispatched at runtime based on the subclass object
![Page 28: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/28.jpg)
33
AbstractSet<E>
…iterator()add(E e)
Method Overriding in Java Polymorphism
The appropriate method is dispatched at runtime based on the subclass object
// Create AbstractSet subclass
AbstractSet<String> absSet = makeSet(setType);
...// Dispatch add(E e) methodabsSet.add(element);
HashSet<E>…iterator()add(E e)
ConcurrentHashSet<E>
…iterator()add(E e)
TreeSet<E>…iterator()add(E e)
![Page 29: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/29.jpg)
34
AbstractSet<E>
…iterator()add(E e)
Method Overriding in Java Polymorphism// Create AbstractSet subclass
AbstractSet<String> absSet = makeSet(setType);
...// Dispatch add(E e) methodabsSet.add(element);
HashSet<E>…iterator()add(E e)
ConcurrentHashSet<E>
…iterator()add(E e)
TreeSet<E>…iterator()add(E e)
The appropriate method is dispatched at runtime based on the subclass object
![Page 30: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/30.jpg)
35
AbstractSet<E>
…iterator()add(E e)
Method Overriding in Java Polymorphism// Create AbstractSet subclass
AbstractSet<String> absSet = makeSet(setType);
...// Dispatch add(E e) methodabsSet.add(element);
See en.wikipedia.org/wiki/Factory_method_pattern & en.wikipedia.org/wiki/Open/closed_principle
HashSet<E>…iterator()add(E e)
ConcurrentHashSet<E>
…iterator()add(E e)
TreeSet<E>…iterator()add(E e)
![Page 31: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/31.jpg)
36
Example of Inheritance & Polymorphism in Java
![Page 32: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/32.jpg)
37
• The implementation of these methods is selected at run-time based on the object’s type
static void main(String[] args) {SimpleAbstractSet<String> set = makeSet(...);
set.put("I");set.put("am"); set.put("Ironman");set.put("Ironman");
for(Iterator<String> it = set.iterator();
it.hasNext();)System.out.println("item = " + iter.next());
}
See github.com/douglascraigschmidt/CS891/tree/master/ex/DynamicBinding
Example of Inheritance & Polymorphism in Java
![Page 33: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/33.jpg)
38
• The implementation of these methods is selected at run-time based on the object’s type
static void main(String[] args) {SimpleAbstractSet<String> set = makeSet(...);
set.put("I");set.put("am"); set.put("Ironman");set.put("Ironman");
for(Iterator<String> it = set.iterator();
it.hasNext();)System.out.println("item = " + iter.next());
}
Factory method creates a HashSet, TreeSet, or
ConcurrentHashSet, etc.
Example of Inheritance & Polymorphism in Java
![Page 34: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/34.jpg)
39
• The implementation of these methods is selected at run-time based on the object’s type
static void main(String[] args) {SimpleAbstractSet<String> set = makeSet(...);
set.put("I");set.put("am"); set.put("Ironman");set.put("Ironman");
for(Iterator<String> it = set.iterator();
it.hasNext();)System.out.println("item = " + iter.next());
}
The put() method & iterator that are dispatched are based on the concrete type of the set
Example of Inheritance & Polymorphism in Java
![Page 35: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/35.jpg)
40
Implementing Dynamic & Static Dispatching in Java
![Page 36: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/36.jpg)
41
• You needn’t know how polymorphism is implemented to use it properly
Implementing Dynamic Dispatching in Java
![Page 37: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/37.jpg)
42
• You needn’t know how polymorphism is implemented to use it properly• Understanding how polymorphism
works will help you become a “full stack developer”
Implementing Dynamic Dispatching in Java
See www.laurencegellert.com/2012/08/what-is-a-full-stack-developer
![Page 38: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/38.jpg)
43
• You needn’t know how polymorphism is implemented to use it properly• Understanding how polymorphism
works will help you become a “full stack developer”
• Also helps you strike a balance between flexibility & efficiency
Implementing Dynamic Dispatching in Java
Flexibility Efficiency
![Page 39: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/39.jpg)
44
• Polymorphism is implemented by the Java compiler & Java Virtual MachineImplementing Dynamic Dispatching in Java
See docs.oracle.com/javase/specs/jvms/se7/html/jvms-6.html#jvms-6.5.invokevirtual
![Page 40: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/40.jpg)
45
• Polymorphism is implemented by the Java compiler & Java Virtual MachineImplementing Dynamic Dispatching in Java
See en.wikipedia.org/wiki/Dynamic_dispatch
![Page 41: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/41.jpg)
46
• Dynamically dispatched methods correspond to an object’s subclass
Implementing Dynamic Dispatching in Java
See en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_super classes
AbstractSet<E>
…iterator()add(E e)
HashSet<E>…iterator()add(E e)
ConcurrentHashSet<E>
…iterator()add(E e)
TreeSet<E>…iterator()add(E e)
![Page 42: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/42.jpg)
47
• Dynamically dispatched methods correspond to an object’s subclass• Subclasses can override &
customize methods inherited from super classes
Implementing Dynamic Dispatching in Java
See en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)#Subclasses_and_super classes
AbstractSet<E>
…iterator()add(E e)
HashSet<E>…iterator()add(E e)
ConcurrentHashSet<E>
…iterator()add(E e)
TreeSet<E>…iterator()add(E e)
![Page 43: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/43.jpg)
48
• Dynamic dispatching is also known as “virtual method invocation”
Implementing Dynamic Dispatching in Java
See docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html
AbstractSet<E>
…iterator()add(E e)
HashSet<E>…iterator()add(E e)
ConcurrentHashSet<E>
…iterator()add(E e)
TreeSet<E>…iterator()add(E e)
![Page 44: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/44.jpg)
49
• Dynamic dispatching is also known as “virtual method invocation”
Implementing Dynamic Dispatching in Java
Dynamic dispatching is the default method dispatching mechanism in Java
AbstractSet<E>
…iterator()add(E e)
HashSet<E>…iterator()add(E e)
ConcurrentHashSet<E>
…iterator()add(E e)
TreeSet<E>…iterator()add(E e)
![Page 45: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/45.jpg)
50
• Dynamic dispatching is also known as “virtual method invocation”• A subclass can override any
method defined in a super class
Implementing Dynamic Dispatching in JavaAbstractSet<E>
…iterator()add(E e)
HashSet<E>…iterator()add(E e)
ConcurrentHashSet<E>
…iterator()add(E e)
TreeSet<E>…iterator()add(E e)
![Page 46: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/46.jpg)
51
• Dynamic dispatching is also known as “virtual method invocation”• A subclass can override any
method defined in a super class• Unless that method is declared
as private, final, or static
Implementing Dynamic Dispatching in JavaAbstractSet<E>
…iterator()add(E e)
HashSet<E>…iterator()add(E e)
ConcurrentHashSet<E>
…iterator()add(E e)
TreeSet<E>…iterator()add(E e)
![Page 47: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/47.jpg)
52
• Each Java class typically has a virtual table (vtable) that contains addresses of dynamically dispatched methods
Implementing Dynamic Dispatching in Java
Pointer to method1
Pointer to method2
Pointer to methodn
Method2 bytecode Methodn bytecode
Method1 bytecode
Pointer to vtable0
table field112iterator field216size field320other fields……
HashSet
keySet field14values field28
Pointer to method1
Pointer to method2
Pointer to methodn
Pointer to vtable0keySet field14values field28
AbstractSet
Method2 bytecode Methodn bytecode
Method1 bytecode
See en.wikipedia.org/wiki/Virtual_method_table
![Page 48: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/48.jpg)
53
Implementing Dynamic Dispatching in Java
Pointer to method1
Pointer to method2
Pointer to methodn
Method2 bytecode Methodn bytecode
Method1 bytecode
Pointer to vtable0
table field112iterator field216size field320other fields……
HashSet
keySet field14values field28
Pointer to method1
Pointer to method2
Pointer to methodn
Pointer to vtable0keySet field14values field28
AbstractSet
Method2 bytecode Methodn bytecode
Method1 bytecode
See en.wikipedia.org/wiki/Polymorphism_(computer_science)#Subtyping
• Each Java object contains a pointer to the vtable (vptr) of its class
![Page 49: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/49.jpg)
54
• Each Java object contains a pointer to the vtable (vptr) of its class• The JVM uses the vptr to locate
& dynamically dispatch a virtual method
Implementing Dynamic Dispatching in Java
Pointer to method1
Pointer to method2
Pointer to methodn
Method2 bytecode Methodn bytecode
Method1 bytecode
Pointer to vtable0
table field112iterator field216size field320other fields……
HashSet
keySet field14values field28
Pointer to method1
Pointer to method2
Pointer to methodn
Pointer to vtable0keySet field14values field28
AbstractSet
Method2 bytecode Methodn bytecode
Method1 bytecode
![Page 50: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/50.jpg)
55
• Each Java object contains a pointer to the vtable (vptr) of its class• The JVM uses the vptr to locate
& dynamically dispatch a virtual method
Implementing Dynamic Dispatching in Java
Pointer to method1
Pointer to method2
Pointer to methodn
Method2 bytecode Methodn bytecode
Method1 bytecode
Pointer to vtable0
table field112iterator field216size field320other fields……
HashSet
keySet field14values field28
Pointer to method1
Pointer to method2
Pointer to methodn
Method2 bytecode Methodn bytecode
Method1 bytecode
Pointer to vtable0keySet field14values field28
AbstractSet
See en.wikipedia.org/wiki/Late_binding#Late_binding_in_Java
![Page 51: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/51.jpg)
56See en.wiktionary.org/wiki/static_dispatch
• Java also supports static method dispatching, where a method implementation is selected at compile-time
Implementing Static Dispatching in JavaAbstractMap<K, V>
…entrySet()put(K, V)eq(Object, Object)
![Page 52: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/52.jpg)
57Statically dispatched methods can be implemented & optimized more efficiently
• Java also supports static method dispatching, where a method implementation is selected at compile-time• e.g., Java private, final, & static methods
Implementing Static Dispatching in JavaAbstractMap<K, V>
…entrySet()put(K, V)eq(Object, Object)
![Page 53: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/53.jpg)
58
Implementing Static Dispatching in Java
See docs.oracle.com/javase/8/docs/api/java/util/AbstractMap.html
• Java also supports static method dispatching, where a method implementation is selected at compile-time• e.g., Java private, final, & static methods
AbstractMap<K, V>
…entrySet()put(K, V)eq(Object, Object)
static boolean eq(Object o1, Object o2) {return o1 == null ? o2 == null : o1.equals(o2);
}
![Page 54: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/54.jpg)
59
• Statically dispatched methods play an important role in Java apps that value performance more than extensibility
Implementing Static Dispatching in Java
e.g., apps where the right answer delivered too late becomes the wrong answer
![Page 55: Overview of Java’s Support for Inheritance & Polymorphism ...schmidt/cs891f/2017-PDFs/L1-pt4-polymorphism.pdf• Polymorphism in Java occurs when a reference to a super class is](https://reader030.fdocuments.us/reader030/viewer/2022040820/5e6873cf3a2f00660a7db0ec/html5/thumbnails/55.jpg)
60
End of Overview of Java’s Support for Polymorphism