Overview of Java s Support for Polymorphism

Similar documents
Java 8 Parallel Stream Internals (Part 2)

Overview of Java Threads (Part 3)

Java 8 Parallel ImageStreamGang Example (Part 3)

External vs. Internal Iteration in Java 8

Infrastructure Middleware (Part 1): Hardware Abstraction Layer (HAL)

JAVA MOCK TEST JAVA MOCK TEST II

The Java FutureTask. Douglas C. Schmidt

Overview of Advanced Java 8 CompletableFuture Features (Part 3)

Overview of Java Threads (Part 2)

Practice for Chapter 11

Chapter 5. Inheritance

Java Barrier Synchronizers: CountDownLatch (Part 1)

Binghamton University. CS-140 Fall Dynamic Types

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

Java Monitor Objects: Synchronization (Part 1)

The Java Executor (Part 1)

COURSE 4 PROGRAMMING III OOP. JAVA LANGUAGE

Class, Variable, Constructor, Object, Method Questions

Rules and syntax for inheritance. The boring stuff

Chapter 5 Object-Oriented Programming

(12-1) OOP: Polymorphism in C++ D & D Chapter 12. Instructor - Andrew S. O Fallon CptS 122 (April 3, 2019) Washington State University

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

Overview of Activities

The Java ExecutorService (Part 1)

Data Structures and Algorithms Design Goals Implementation Goals Design Principles Design Techniques. Version 03.s 2-1

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

Java Semaphore (Part 1)

Overview of Layered Architectures

Advanced Programming Generics Collections

PROGRAMMING LANGUAGE 2

Dynamic Binding C++ Douglas C. Schmidt

JAVA MOCK TEST JAVA MOCK TEST III

Overview of Java 8 Functional Interfaces

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

Topic 5 Polymorphism. " Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.

Object Oriented Programming. Java-Lecture 11 Polymorphism

Delft-Java Link Translation Buffer

Lecture 6 Collections

What is Inheritance?

15CS45 : OBJECT ORIENTED CONCEPTS

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

A Case Study of Gang of Four (GoF) Patterns : Part 7

SUN Upgrade for the Sun Certified Java Programmer. SE 6.0. Download Full Version :

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

Java Review: Objects

Chapter 1 Introduction to Java

Syllabus & Curriculum for Certificate Course in Java. CALL: , for Queries

5/23/2015. Core Java Syllabus. VikRam ShaRma

MODULE 6q - Exceptions

COMP 401 INITIALIZATION AND INHERITANCE. Instructor: Prasun Dewan

Data Abstraction. Hwansoo Han

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

Java Fundamentals (II)

CLASS DESIGN. Objectives MODULE 4

Outline. Object Oriented Programming. Course goals. Staff. Course resources. Assignments. Course organization Introduction Java overview Autumn 2003

Benefits of Concurrency in Java & Android: Program Structure

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Inheritance and Polymorphism

Java. Classes 3/3/2014. Summary: Chapters 1 to 10. Java (2)

Lecture 5: Inheritance

CSE1720. General Info Continuation of Chapter 9 Read Chapter 10 for next week. Second level Third level Fourth level Fifth level

Computational Applications in Nuclear Astrophysics using Java Java course Lecture 6

Fundamentals of Object Oriented Programming

Object-Oriented Concepts

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

Collections Framework: Part 2

Inheritance and object compatibility

CS Programming I: Inheritance

Chapter 14 Abstract Classes and Interfaces

C++ Inheritance and Encapsulation

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

Inheritance. Benefits of Java s Inheritance. 1. Reusability of code 2. Code Sharing 3. Consistency in using an interface. Classes

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

CSE 401/M501 Compilers

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

Java Magistère BFA

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

STRUCTURING OF PROGRAM

Atelier Java - J1. Marwan Burelle. EPITA Première Année Cycle Ingénieur.

Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object.

The class Object. Lecture CS1122 Summer 2008

Object Oriented Programming: Based on slides from Skrien Chapter 2

Java Barrier Synchronizers: CyclicBarrier (Part 1)

Object-Oriented Programming More Inheritance

Answer1. Features of Java

Java for Programmers Course (equivalent to SL 275) 36 Contact Hours

2 rd class Department of Programming. OOP with Java Programming

Lesson 10. Work with Interfaces and Abstract Classes. Copyright all rights reserved

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

Implements vs. Extends When Defining a Class

Introduction to Java Programming CPIT 202. WEWwwbvxnvbxmnhsgfkdjfcn

Inheritance, cont. Notes Chapter 6 and AJ Chapters 7 and 8

C10: Garbage Collection and Constructors

EKT472: Object Oriented Programming. Overloading and Overriding Method

Chapter 1: Introduction to Computers, Programs, and Java

Method Resolution Approaches. Dynamic Dispatch

1 Shyam sir JAVA Notes

Transcription:

Overview of Java s Support for Polymorphism Douglas C. Schmidt d.schmidt@vanderbilt.edu www.dre.vanderbilt.edu/~schmidt Professor of Computer Science Institute for Software Integrated Systems Vanderbilt University Nashville, Tennessee, USA

Learning Objectives in this Lesson Understand what polymorphism is & how it s supported in Java Abstraction OOP Polymorphism Inheritance 2

Overview of Java s Support for Polymorphism 3

Overview of Java s Support for Polymorphism Inheritance is nearly always used in conjunction with polymorphism Abstraction Polymorphism Inheritance 4

Overview of Java s Support for Polymorphism Polymorphism enables transparent customization of methods inherited from a super class Abstraction OOP Polymorphism Inheritance See en.wikipedia.org/wiki/polymorphism_(computer_science) 5

Overview of Java s Support for Polymorphism Polymorphism & inheritance are essential to the open/closed principle See en.wikipedia.org/wiki/open/closed_principle 6

Overview of Java s Support for Polymorphism Polymorphism & inheritance are essential to the open/closed principle A class should be open for extension, but closed for modification See en.wikipedia.org/wiki/open/closed_principle 7

Overview of Java s Support for Polymorphism 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 more robust, flexible, & reusable See www.dre.vanderbilt.edu/~schmidt/ocp.pdf 8

Overview of Java s Support for Polymorphism The open/closed principle can be applied in conjunction with patterns to enable extensions without modifying existing classes or apps See en.wikipedia.org/wiki/factory_method_pattern 9

Overview of Java s Support for Polymorphism Subclasses defined in accordance with the open/closed principle can define their own custom behaviors that are more suitable for a particular use case <<extends>> <<extends>> 15

Overview of Java s Support for Polymorphism Subclasses defined in accordance with the open/closed principle can define their own custom behaviors that are more suitable for a particular use case While still reusing structure & functionality from super class <<extends>> <<extends>> 16

Overview of Java s Support for Polymorphism Subclasses defined in accordance with the open/closed principle 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 <<extends>> <<extends>> 17

Overview of Java s Support for Polymorphism Subclasses defined in accordance with the open/closed principle 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 <<extends>> <<extends>> 18

Overview of Java s Support for Polymorphism Subclasses defined in accordance with the open/closed principle 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 <<extends>> <<extends>> Methods in each subclass emphasize different List properties See beginnersbook.com/2013/12/difference-between-arraylist-and-vector-in-java 19

Method Overriding in Java Polymorphism 20

Method Overriding in Java Polymorphism Polymorphism in Java occurs when a reference to a super class is used to refer to an object of a subclass AbstractSet<E> TreeSet<E> 21 Concurrent

Method Overriding in Java Polymorphism Polymorphism in Java occurs when a reference to a super class is used to refer to an object of a subclass Subclass methods can override super class methods AbstractSet<E> TreeSet<E> Concurrent Any non-final & non-static 22 methods can be overridden

Method Overriding in Java Polymorphism Polymorphism in Java occurs when a reference to a super class is used to refer to an object of a subclass Subclass methods can override super class methods AbstractSet<E> See docs.oracle.com/javase/8/docs/api/java/util/abstractset.html 23

Method Overriding in Java Polymorphism Polymorphism in Java occurs when a reference to a super class is used to refer to an object of a subclass Subclass methods can override super class methods AbstractSet<E> Returns an iterator that can access each element in a Set 24

Method Overriding in Java Polymorphism Polymorphism in Java occurs when a reference to a super class is used to refer to an object of a subclass Subclass methods can override super class methods AbstractSet<E> Associates the specified element to the Set 25

Method Overriding in Java Polymorphism Polymorphism in Java occurs when a reference to a super class is used to refer to an object of a subclass Subclass methods can override super class methods AbstractSet<E> TreeSet<E> Concurrent Subclasses of AbstractSet can override 26 & differently

Method Overriding in Java Polymorphism Polymorphism in Java occurs when a reference to a super class is used to refer to an object of a subclass Subclass methods can override super class methods TreeSet<E> Concurrent Subclasses of AbstractSet have 27 different time & space tradeoffs

Method Overriding in Java Polymorphism Polymorphism in Java occurs when a reference to a super class is used to refer to an object of a subclass Subclass methods can override super class methods AbstractSet<E> TreeSet<E> Concurrent See docs.oracle.com/javase/8/docs/api/java/util/treeset.html 28

Method Overriding in Java Polymorphism Polymorphism in Java occurs when a reference to a super class is used to refer to an object of a subclass Subclass methods can override super class methods AbstractSet<E> TreeSet<E> Concurrent See docs.oracle.com/javase/8/docs/api/java/util/hashset.html 29

Method Overriding in Java Polymorphism Polymorphism in Java occurs when a reference to a super class is used to refer to an object of a subclass Subclass methods can override super class methods AbstractSet<E> TreeSet<E> Concurrent See Java8/ex19/src/main/java/utils/ConcurrentHashSet.java 30

Method Overriding in Java Polymorphism // Create AbstractSet subclass AbstractSet<String> absset = makeset(settype);... // Dispatch method absset.add(element); AbstractSet<E> TreeSet<E> Concurrent A factory method creates the appropriate 31 concrete set subclass instance

Method Overriding in Java Polymorphism // Create AbstractSet subclass AbstractSet<String> absset = makeset(settype);... // Dispatch method absset.add(element); AbstractSet<E> TreeSet<E> Concurrent The appropriate method is dispatched 32 at runtime based on the subclass object

Method Overriding in Java Polymorphism // Create AbstractSet subclass AbstractSet<String> absset = makeset(settype);... // Dispatch method absset.add(element); AbstractSet<E> TreeSet<E> Concurrent The appropriate method is dispatched 33 at runtime based on the subclass object

Method Overriding in Java Polymorphism // Create AbstractSet subclass AbstractSet<String> absset = makeset(settype);... // Dispatch method absset.add(element); AbstractSet<E> TreeSet<E> Concurrent The appropriate method is dispatched 34 at runtime based on the subclass object

Method Overriding in Java Polymorphism // Create AbstractSet subclass AbstractSet<String> absset = makeset(settype);... // Dispatch method absset.add(element); AbstractSet<E> TreeSet<E> Concurrent 35 See en.wikipedia.org/wiki/factory_method_pattern & en.wikipedia.org/wiki/open/closed_principle

Example of Inheritance & Polymorphism in Java 36

Example of Inheritance & Polymorphism in Java 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.; it.hasnext();) System.out.println ("item = " + iter.next()); See github.com/douglascraigschmidt/cs891/tree/master/ex/dynamicbinding 37

Example of Inheritance & Polymorphism in Java 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(...); Factory method creates a HashSet, TreeSet, or ConcurrentHashSet, etc. } set.put("i"); set.put("am"); set.put("ironman"); set.put("ironman"); for(iterator<string> it = set.; it.hasnext();) System.out.println ("item = " + iter.next()); 38

Example of Inheritance & Polymorphism in Java 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"); The put() method & iterator that are dispatched are based on the concrete type of the set } for(iterator<string> it = set.; it.hasnext();) System.out.println ("item = " + iter.next()); 39

Implementing Dynamic & Static Dispatching in Java 40

Implementing Dynamic Dispatching in Java You needn t know how polymorphism is implemented to use it properly 41

Implementing Dynamic Dispatching in Java You needn t know how polymorphism is implemented to use it properly Understanding how polymorphism works will help you become a full stack developer See www.laurencegellert.com/2012/08/what-is-a-full-stack-developer 42

Implementing Dynamic Dispatching in Java 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 Flexibility Efficiency 43

Implementing Dynamic Dispatching in Java Polymorphism is implemented by the Java compiler & Java Virtual Machine 44 See docs.oracle.com/javase/specs/jvms/se7/html/jvms-6.html#jvms-6.5.invokevirtual

Implementing Dynamic Dispatching in Java Polymorphism is implemented by the Java compiler & Java Virtual Machine See en.wikipedia.org/wiki/dynamic_dispatch 45

Implementing Dynamic Dispatching in Java Dynamically dispatched methods correspond to an object s subclass AbstractSet<E> TreeSet<E> Concurrent 46 See en.wikipedia.org/wiki/inheritance_(object-oriented_programming)#subclasses_and_super classes

Implementing Dynamic Dispatching in Java Dynamically dispatched methods correspond to an object s subclass Subclasses can override & customize methods inherited from super classes AbstractSet<E> TreeSet<E> Concurrent 47 See en.wikipedia.org/wiki/inheritance_(object-oriented_programming)#subclasses_and_super classes

Implementing Dynamic Dispatching in Java Dynamic dispatching is also known as virtual method invocation AbstractSet<E> TreeSet<E> Concurrent See docs.oracle.com/javase/tutorial/java/iandi/polymorphism.html 48

Implementing Dynamic Dispatching in Java Dynamic dispatching is also known as virtual method invocation AbstractSet<E> TreeSet<E> Concurrent Dynamic dispatching is the default method 49 dispatching mechanism in Java

Implementing Dynamic Dispatching in Java Dynamic dispatching is also known as virtual method invocation A subclass can override any method defined in a super class AbstractSet<E> TreeSet<E> 50 Concurrent

Implementing Dynamic Dispatching in Java 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 AbstractSet<E> TreeSet<E> 51 Concurrent

Implementing Dynamic Dispatching in Java Each Java class typically has a virtual table (vtable) that contains addresses of dynamically dispatched methods 0 4 8 12 16 20 HashSet Pointer to vtable keyset field 1 values field 2 table field 1 iterator field 2 size field 3 other fields 0 4 8 Pointer to method 1 Pointer to method 2 Pointer to method n Method 1 bytecode Method 2 bytecode Method n bytecode AbstractSet Pointer to vtable keyset field 1 values field 2 See en.wikipedia.org/wiki/virtual_method_table 52 Pointer to method 1 Pointer to method 2 Pointer to method n Method 1 bytecode Method 2 bytecode Method n bytecode

Implementing Dynamic Dispatching in Java Each Java object contains a pointer to the vtable (vptr) of its class 0 4 8 12 16 20 HashSet Pointer to vtable keyset field 1 values field 2 table field 1 iterator field 2 size field 3 other fields 0 4 8 Pointer to method 1 Pointer to method 2 Pointer to method n Method 1 bytecode Method 2 bytecode Method n bytecode AbstractSet Pointer to vtable keyset field 1 values field 2 Pointer to method 1 Pointer to method 2 Pointer to method n Method 1 bytecode Method 2 bytecode Method n bytecode See en.wikipedia.org/wiki/polymorphism_(computer_science)#subtyping 53

Implementing Dynamic Dispatching in Java 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 0 4 8 12 16 20 HashSet Pointer to vtable keyset field 1 values field 2 table field 1 iterator field 2 size field 3 other fields 0 4 8 Pointer to method 1 Pointer to method 2 Pointer to method n Method 1 bytecode Method 2 bytecode Method n bytecode 54 AbstractSet Pointer to vtable keyset field 1 values field 2 Pointer to method 1 Pointer to method 2 Pointer to method n Method 1 bytecode Method 2 bytecode Method n bytecode

Implementing Dynamic Dispatching in Java 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 0 4 8 12 16 20 HashSet Pointer to vtable keyset field 1 values field 2 table field 1 iterator field 2 size field 3 other fields 0 4 8 Pointer to method 1 Pointer to method 2 Pointer to method n Method 1 bytecode Method 2 bytecode Method n bytecode AbstractSet Pointer to vtable keyset field 1 values field 2 Pointer to method 1 Pointer to method 2 Pointer to method n Method 1 bytecode Method 2 bytecode Method n bytecode See en.wikipedia.org/wiki/late_binding#late_binding_in_java 55

Implementing Static Dispatching in Java Java also supports static method dispatching, where a method implementation is selected at compile-time AbstractMap<K, V> entryset() put(k, V) eq(object, Object) See en.wiktionary.org/wiki/static_dispatch 56

Implementing Static Dispatching in Java 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) Statically dispatched methods can be implemented 57 & optimized more efficiently

Implementing Static Dispatching in Java 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); } See docs.oracle.com/javase/8/docs/api/java/util/abstractmap.html 58

Implementing Static Dispatching in Java Statically dispatched methods play an important role in Java apps that value performance more than extensibility e.g., apps where the right answer delivered 59 too late becomes the wrong answer

End of Overview of Java s Support for Polymorphism 60