Polymorphism. Hsuan-Tien Lin. OOP Class, April 8, Department of CSIE, NTU. H.-T. Lin (NTU CSIE) Polymorphism OOP 04/08/ / 26

Similar documents
QUIZ. Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed?

Data Abstraction: Status of an Object

Polymorphism. Zimmer CSCI 330

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

Microsoft Visual Basic 2005: Reloaded

C++ Programming: Polymorphism

Polymorphism. Miri Ben-Nissan (Kopel) Miri Kopel, Bar-Ilan University

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

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

Inheritance, polymorphism, interfaces

15: Polymorphism & Virtual Functions

Inheritance and Polymorphism

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

Inheritance, Polymorphism and the Object Memory Model

Lecture 18 CSE11 Fall 2013 Inheritance

Introduction to Programming Using Java (98-388)

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

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

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

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

Object-oriented Programming. Object-oriented Programming

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

Inheritance and Polymorphism

Programming II (CS300)

Making Inheritance Work: C++ Issues

Making Inheritance Work: C++ Issues

VIRTUAL FUNCTIONS Chapter 10

Inheritance and Polymorphism

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

Data Abstraction. Hwansoo Han

First IS-A Relationship: Inheritance

Polymorphism. Object Orientated Programming in Java. Benjamin Kenwright

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

Exercise: Singleton 1

OBJECT ORİENTATİON ENCAPSULATİON

Chapter 5 Object-Oriented Programming

Today. Book-keeping. Inheritance. Subscribe to sipb-iap-java-students. Slides and code at Interfaces.

Project. C++: Inheritance III. Plan. Project. Before we begin. The final exam. Advanced Topics. Project. This week in the home stretch

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

type conversion polymorphism (intro only) Class class

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Derived and abstract data types. TDT4205 Lecture 15

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1

Object-Oriented Programming

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Objective Questions. BCA Part III Paper XIX (Java Programming) page 1 of 5

Polymorphism Part 1 1

Chapter 2: Java OO II X I A N G Z H A N G

Introduction to Object-Oriented Programming

A Short Summary of Javali

Java Object Oriented Design. CSC207 Fall 2014

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

Absolute C++ Walter Savitch

Introduction to OOP. Procedural Programming sequence of statements to solve a problem.

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

1 Shyam sir JAVA Notes

8. Polymorphism and Inheritance

C++ Crash Kurs. Polymorphism. Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck

Programming using C# LECTURE 07. Inheritance IS-A and HAS-A Relationships Overloading and Overriding Polymorphism

Concepts of Programming Languages

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

RAJIV GANDHI COLLEGE OF ENGINEERING AND TECHNOLOGY DEPARTMENT OF INFORMATION TECHNOLOGY OBJECT ORIENTED PROGRAMMING QUESTION BANK UNIT I 2 MARKS

Course Text. Course Description. Course Objectives. StraighterLine Introduction to Programming in C++

9: Polymorphism. OOP = abstraction + inheritance + polymorphism

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

More Relationships Between Classes

Records. ADTs. Objects as Records. Objects as ADTs. Objects CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 15: Objects 25 Feb 05

Instantiation of Template class

Preface to the Second Edition Preface to the First Edition Brief Contents Introduction to C++ p. 1 A Review of Structures p.

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017

Comp 249 Programming Methodology Chapter 8 - Polymorphism

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

ITI Introduction to Computing II

Introduction to Inheritance

More About Objects. Zheng-Liang Lu Java Programming 255 / 282

G Programming Languages - Fall 2012

CH. 2 OBJECT-ORIENTED PROGRAMMING

CS107 Handout 37 Spring 2007 May 25, 2007 Introduction to Inheritance

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

Object Oriented Programming. Java-Lecture 11 Polymorphism

ITI Introduction to Computing II

What are the characteristics of Object Oriented programming language?

KLiC C++ Programming. (KLiC Certificate in C++ Programming)

Programming II (CS300)

CS11 Introduction to C++ Fall Lecture 7

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08

25. Generic Programming

7: Polymorphism. Interface & Implementation. Hands-on Java Seminar 2000 Bruce Eckel page 1. A Circle.

JAVA MOCK TEST JAVA MOCK TEST II

Creating an Immutable Class. Based on slides by Prof. Burton Ma

Inheritance, Polymorphism, and Interfaces

Informatik II. Tutorial 6. Mihai Bâce Mihai Bâce. April 5,

Increases Program Structure which results in greater reliability. Polymorphism

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

Tentative Teaching Plan Department of Software Engineering Name of Teacher Dr. Naeem Ahmed Mahoto Course Name Computer Programming

OOP THROUGH C++(R16) int *x; float *f; char *c;

Transcription:

Polymorphism Hsuan-Tien Lin Department of CSIE, NTU OOP Class, April 8, 2013 H.-T. Lin (NTU CSIE) Polymorphism OOP 04/08/2013 0 / 26

Polymorphism: The Motto One Thing, Many Shapes... H.-T. Lin (NTU CSIE) Polymorphism OOP 04/08/2013 1 / 26

One Variable, Many Values 1 char a ; 2 switch ( a ) { 3 case 1: 4 r e t u r n 1 1; 5 case 2: 6 r e t u r n 2 2; 7 case 3: 8 r e t u r n 3 3; 9... 10 case 127: 11 r e t u r n 127 127; 12 } better ways? mechanism? raw memory interpretation H.-T. Lin (NTU CSIE) Polymorphism OOP 04/08/2013 2 / 26

One Class, Many Instances 1 Student s ; 2 i f ( s equals student1 ) 3 show ( score1 ) ; 4 else i f ( s equals student2 ) 5 show ( score2 ) ; 6... 7 else i f ( s equals student100 ) 8 show ( score100 ) ; better ways? mechanism? data abstraction H.-T. Lin (NTU CSIE) Polymorphism OOP 04/08/2013 3 / 26

One Method Name, Many Parameter Lists 1 / / no overloading 2 System. out. p r i n t S t r i n g ( " abc " ) ; 3 System. out. p r i n t I n t ( 3 ) ; 4 System. out. printdouble ( 4. 0 ) ; 5 / / overloading 6 System. out. p r i n t ( " abc " ) ; 7 System. out. p r i n t ( 3 ) ; 8 System. out. p r i n t ( 4. 0 ) ; mechanism? signature by name + parameter types H.-T. Lin (NTU CSIE) Polymorphism OOP 04/08/2013 4 / 26

A Twist in Method Overloading 1 / / overloading 2 System. out. p r i n t ( " abc " ) ; 3 System. out. p r i n t ( 3 ) ; 4 System. out. p r i n t ( 4. 0 ) ; 5 / / a t w i s t ( of course, not e x a c t l y workable 6 S t r i n g ( " abc " ). p r i n t ( ) ; 7 I n t e g e r ( 3 ). p r i n t ( ) ; 8 Double ( 4. 0 ). p r i n t ( ) ; mechanism? just write print() for every class (we ll see) H.-T. Lin (NTU CSIE) Polymorphism OOP 04/08/2013 5 / 26

Polymorphic Behavior all cases above: some polymorphic behavior BUT almost no one calls them real polymorphism Why Not? H.-T. Lin (NTU CSIE) Polymorphism OOP 04/08/2013 6 / 26

Polymorphic Behavior on Known Stuff polymorphic behavior of known primitive-type variables polymorphic behavior of known classes (extended types) polymorphic behavior of known parameter types H.-T. Lin (NTU CSIE) Polymorphism OOP 04/08/2013 7 / 26

Unknown Stuff: Future Extensions Backward Compatibility inheritance hierarchy Forward Advance newly added variables/methods H.-T. Lin (NTU CSIE) Polymorphism OOP 04/08/2013 8 / 26

Polymorphism of Instance Content one advanced content, many compatible ways to access H.-T. Lin (NTU CSIE) Polymorphism OOP 04/08/2013 9 / 26

Polymorphism of Instance References one compatible reference, many advanced contents to point to H.-T. Lin (NTU CSIE) Polymorphism OOP 04/08/2013 10 / 26

Reference Upcast versus Reference Downcast 1 CSIEProfessor c = new CSIEProfessor ( ) ; 2 Professor p = c ; 3 / / Professor p = new CSIEProfessor ( ) ; Upcast simple (backward compatibility) 1 CSIEProfessor c = new CSIEProfessor ( ) ; 2 Professor p = c ; 3 CSIEProfessor c2 = ( CSIEProfessor ) p ; Downcast need to check whether content fits (forward advance) need RTTI (run-time type information/identification) to make downcast work (where did we see it?) H.-T. Lin (NTU CSIE) Polymorphism OOP 04/08/2013 11 / 26

Content/Reference Polymorphism: Summary backward compatibility handled forward advance: only via downcast (RTTI) simpler mechanism for forward advance? H.-T. Lin (NTU CSIE) Polymorphism OOP 04/08/2013 12 / 26

Our Needs in Forward Advance new instance variables for advanced state new instance methods to manipulate new variables give new meanings to existing instance variables give new meanings to existing instance methods write an updated but compatible version of existing method H.-T. Lin (NTU CSIE) Polymorphism OOP 04/08/2013 13 / 26

Method Overriding (Virtual Function) calls the updated version through an upper-level reference H.-T. Lin (NTU CSIE) Polymorphism OOP 04/08/2013 14 / 26

Method Invocation Polymorphism one method (via upper-level reference), many possible extended behaviors H.-T. Lin (NTU CSIE) Polymorphism OOP 04/08/2013 15 / 26

Object.equals 1 class Coordinate extends Object { 2 double x, y ; 3 4 bool equals ( Object o ) { 5 i f ( o i n s t a n c e o f Coordinate ) { 6 Coordinate c = ( Coordinate ) o ; 7 r e t u r n ( c. x == t h i s. x && c. y == t h i s. y ) ; 8 } 9 r e t u r n f a l s e ; 10 } 11 } H.-T. Lin (NTU CSIE) Polymorphism OOP 04/08/2013 16 / 26

Object.toString 1 Coordinate c = new Coordinate ( ) ; 2 System. out. p r i n t l n ( c ) ; H.-T. Lin (NTU CSIE) Polymorphism OOP 04/08/2013 17 / 26

Twist Revisited 1 System. out. p r i n t ( " abc " ) ; 2 System. out. p r i n t ( 3 ) ; 3 System. out. p r i n t ( 4. 0 ) ; 4 / / a t w i s t ( of course, not e x a c t l y workable 5 S t r i n g ( " abc " ). p r i n t ( ) ; 6 I n t e g e r ( 3 ). p r i n t ( ) ; 7 Double ( 4. 0 ). p r i n t ( ) ; System.out.print(Object) can have polymorphic behavior by internally calling the updated Object.print() (actually, Object.toString()) without overloading H.-T. Lin (NTU CSIE) Polymorphism OOP 04/08/2013 18 / 26

V-Table: A Possible Mechanism of Method Overriding again, not the only mechanism H.-T. Lin (NTU CSIE) Polymorphism OOP 04/08/2013 19 / 26

RTTI revisited all we need is a link to the class area (stores name, vtable, etc.) where is the link? java.lang.object how to access? Object.getClass() each area is an instance of java.lang.class H.-T. Lin (NTU CSIE) Polymorphism OOP 04/08/2013 20 / 26

Summary on Polymorphism one thing, many shapes important in strongly-typed platforms with inheritance view from content: one advanced content with many compatible access view from reference: one compatible reference can point to many advanced contents view from method: one compatible method contract, many different method realization H.-T. Lin (NTU CSIE) Polymorphism OOP 04/08/2013 21 / 26

Abstract Class (1/3) 1 p u b l i c class Professor ( ) { 2 p u b l i c void teach ( ) { 3 System. out. p r i n t l n ( " not sure of what to teach! " ) ; 4 } 5 } 6 class CSIEProfessor extends Professor { 7 p r i v a t e void teach_oop ( ) { / l a l a l a / } 8 p u b l i c void teach ( ) { teach_oop ( ) ; } 9 } 10 class EEProfessor extends Professor { 11 p r i v a t e void teach_elec ( ) { / l u l u l u / } 12 p u b l i c void teach ( ) { teach_elec ( ) ; } 13 } 14 / / i n other places 15 Professor p = new Professor ( ) ; 16 p. teach ( ) ; / / not sure of what to teach! teach is a place-holder in Professor, expected to be overridden allows constructing a professor without any teaching ability! absurd in some sense H.-T. Lin (NTU CSIE) Polymorphism OOP 04/08/2013 22 / 26

Abstract Class (2/3) 1 p u b l i c a b s t r a c t class Professor ( ) { 2 p u b l i c a b s t r a c t void teach ( ) ; 3 } 4 class CSIEProfessor extends Professor { 5 p r i v a t e void teach_oop ( ) { / l a l a l a / } 6 p u b l i c void teach ( ) { teach_oop ( ) ; } 7 } 8 class EEProfessor extends Professor { 9 p r i v a t e void teach_elec ( ) { / l u l u l u / } 10 p u b l i c void teach ( ) { teach_elec ( ) ; } 11 } 12 / / i n other places 13 Professor p = new Professor ( ) ; / / hahaha!! 14 Professor p = new CSIEProfessor ( ) ; / / okay teach is a place-holder in Professor, expected to be overridden but cannot construct a pure Professor instance anymore! H.-T. Lin (NTU CSIE) Polymorphism OOP 04/08/2013 23 / 26

Key Point: Abstract Class a contract for future extensions H.-T. Lin (NTU CSIE) Polymorphism OOP 04/08/2013 25 / 26

Final Words static final variable: accessed through class, and assigned once (in declaration or static constructor) final instance variable: accessed through instance, and assigned once (in declaration or every instance constructor) final instance method: cannot be overriden ( assigned once) static final method: cannot be hidden by inheritance ( assigned once) final class: cannot be inherited (and hence all methods final) H.-T. Lin (NTU CSIE) Polymorphism OOP 04/08/2013 26 / 26