Comp 249 Programming Methodology Chapter 8 - Polymorphism

Similar documents
5/24/12. Introduction to Polymorphism. Chapter 8. Late Binding. Introduction to Polymorphism. Late Binding. Polymorphism and Abstract Classes

Comp 249 Programming Methodology

Comp 249 Programming Methodology

Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B

Copyright 2007 Pearson Addison-Wesley Copyright 2018 Aiman Hanna All rights reserved

Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A

Comp 249 Programming Methodology Chapter 9 Exception Handling

Copyright 2007 Pearson Addison-Wesley Copyright 2019 Aiman Hanna All rights reserved

Comp 249 Programming Methodology Chapter 11 Recursion

What is Inheritance?

25. Generic Programming

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Object Oriented Programming. Java-Lecture 11 Polymorphism

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

Inheritance -- Introduction

Java Fundamentals (II)

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

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

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

Binghamton University. CS-140 Fall Dynamic Types

CH. 2 OBJECT-ORIENTED PROGRAMMING

Inheritance and Polymorphism

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

Inheritance (continued) Inheritance

Compaq Interview Questions And Answers

Implements vs. Extends When Defining a Class

Module 11. Collections and Iterators. Adapted from Absolute Java, Rose Williams, Binghamton University

CS111: PROGRAMMING LANGUAGE II

Java: introduction to object-oriented features

Java Object Oriented Design. CSC207 Fall 2014

Polymorphism and Interfaces. CGS 3416 Spring 2018

This week. Tools we will use in making our Data Structure classes: Generic Types Inheritance Abstract Classes and Interfaces

9/10/2018 Programming Data Structures Inheritance

This page intentionally left blank

Polymorphism. Object Orientated Programming in Java. Benjamin Kenwright

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

Introduction to Inheritance

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

CompSci 125 Lecture 20. Inheritance: Introduction, Overrides UML: Introduction to Class Diagrams

OBJECT ORİENTATİON ENCAPSULATİON

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

Chapter 15. Polymorphism and Virtual Functions. Copyright 2010 Pearson Addison-Wesley. All rights reserved

CS313D: ADVANCED PROGRAMMING LANGUAGE

First IS-A Relationship: Inheritance

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

C++ Important Questions with Answers

Practice for Chapter 11

Programming II (CS300)

Programming Languages and Techniques (CIS120)

Type Hierarchy. Lecture 6: OOP, autumn 2003

Chapter 5 Object-Oriented Programming

Inheritance and Polymorphism

Object-Oriented Programming

Oops known as object-oriented programming language system is the main feature of C# which further support the major features of oops including:

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism

Data Structures and Other Objects Using C++

What s Conformance? Conformance. Conformance and Class Invariants Question: Conformance and Overriding

VIRTUAL FUNCTIONS Chapter 10

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

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

Lecture Notes on Programming Languages

CS-202 Introduction to Object Oriented Programming

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

More Relationships Between Classes

Inheritance, Polymorphism and the Object Memory Model

Virtual functions concepts

Comp 311 Principles of Programming Languages Lecture 21 Semantics of OO Languages. Corky Cartwright Mathias Ricken October 20, 2010

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

Object oriented programming. Encapsulation. Polymorphism. Inheritance OOP

Casting in C++ (intermediate level)

What are the characteristics of Object Oriented programming language?

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

Implementing Object Equivalence in Java Using the Template Method Design Pattern

CMSC 132: Object-Oriented Programming II

Inheritance and Polymorphism

INHERITANCE. Spring 2019

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.

Absolute C++ Walter Savitch

Generalized Code. Fall 2011 (Honors) 2

Lesson 10B Class Design. By John B. Owen All rights reserved 2011, revised 2014

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

Lecture 5: Inheritance

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

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

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

C++ Inheritance and Encapsulation

Exercise: Singleton 1

CS Programming I: Inheritance

Java Magistère BFA

Chapter 11: Inheritance

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

Contributor. International Edition contributions by. Piyali Sengupta PEARSON. New York San Francisco Upper

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

EECS 1001 and EECS 1030M, lab 01 conflict

Rules and syntax for inheritance. The boring stuff

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

Inheritance and Polymorphism

Inheritance. SOTE notebook. November 06, n Unidirectional association. Inheritance ("extends") Use relationship

Polymorphism. Final Exam. November 26, Method Overloading. Quick Review of Last Lecture. Overriding Methods.

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

Transcription:

Comp 249 Programming Methodology Chapter 8 - Polymorphism Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal, Canada These slides has been extracted, modified and updated from original slides of Absolute Java 3 rd Edition by Savitch; which has originally been prepared by Rose Williams of Binghamton University. Absolute Java is published by Pearson Education / Addison-Wesley. Copyright 2007 Pearson Addison-Wesley Copyright 2018 Aiman Hanna All rights reserved

Introduction to Polymorphism There are three main programming mechanisms that constitute object-oriented programming (OOP) Encapsulation Inheritance Polymorphism Polymorphism is the ability to associate many meanings to one method name It does this through a special mechanism known as late binding or dynamic binding 8-2

Introduction to Polymorphism Inheritance allows a base class to be defined, and other classes derived from it Code for the base class can then be used for its own objects, as well as objects of any derived classes Polymorphism allows changes to be made to method definitions in the derived classes, and have those changes apply to the software written for the base class 8-3

Late Binding The process of associating a method definition with a method invocation is called binding If the method definition is associated with its invocation when the code is compiled, that is called early binding If the method definition is associated with its invocation when the method is invoked (at run time), that is called late binding or dynamic binding 8-4

Late Binding Java uses late binding for all methods (except private, final, and static methods) Because of late binding, a method can be written in a base class to perform a task, even if portions of that task aren't yet defined See Polymorphism1.java 8-5

Pitfall: No Late Binding for Static Methods When the decision of which definition of a method to use is made at compile time, that is called static binding This decision is made based on the type of the variable naming the object Java uses static, not late, binding with private, final, and static methods In the case of private and final methods, late binding would serve no purpose (these methods cannot be overridden, so only one version exists) (Warning:) However, in the case of a static method invoked using a calling object, it does make a difference See Polymorphism2.java 8-6

Pitfall: No Late Binding for Static Methods Example (See Polymorphism2.java): The Vehicle class DisplayNumberOfCreatedObjects () method: public static void DisplayNumberOfCreatedObjects() { System.out.println("The number of created Vehicle objects so far is " + numofcreatedobjects + "."); } The Bus class DisplayNumberOfCreatedObjects () method: public static void DisplayNumberOfCreatedObjects() { System.out.println("The number of created Bus objects so far is " + numofcreatedobjects + "."); } 8-7

Pitfall: No Late Binding for Static Methods Example (See Polymorphism2.java) Continues: In the previous example, the object v1 was created from the Vehicle class, and the object b1 was created from the Bus class, Given the following assignment: v1 = b1; Now the two variables point to the same object 8-8

Pitfall: No Late Binding for Static Methods Example (See Polymorphism2.java) Continues: Given the invocations: v1.displaynumberofcreatedobjects(); b1.displaynumberofcreatedobjects(); The output is: The number of created Vehicle objects so far is 11. The number of created Bus objects so far is 3. Note that here, DisplayNumberOfCreatedObjects is a static method invoked a calling object (i.e. v1, b1) (instead of its class name) Therefore the exact executed method is determined by its variable name, not the object that it references 8-9

Pitfall: No Late Binding for Static Methods There are other cases where a static method has a calling object in a more inconspicuous way For example, a static method can be invoked within the definition of a nonstatic method, but without any explicit class name or calling object In this case, the calling object is the implicit this 8-10

The final Modifier A method marked final indicates that it cannot be overridden with a new definition in a derived class If final, the compiler can use early binding with the method public final void somemethod() {... } A class marked final indicates that it cannot be used as a base class from which to derive any other classes 8-11

Upcasting and Downcasting Upcasting is when an object of a derived class is assigned to a variable of a base class (or any ancestor class) Vehicle v1 = new Vehicle(); //Base class object Bus b1 = new Bus(2, 55000, 37); //Derived class object v1 = b1; Downcasting is when a type cast is performed from a base class to a derived class (or from any ancestor class to any descendent class) Downcasting has to be done very carefully In many cases it doesn't make sense, or is illegal: B1 = v1; //will produce compiler error B1 = (Bus)v1; //will produce run-time error There are times, however, when downcasting is necessary, e.g., inside the equals method for a class See Polymorphism3.java Revisit Object3.java 8-12

Pitfall: Downcasting It is the responsibility of the programmer to use downcasting only in situations where it makes sense The compiler does not check to see if downcasting is a reasonable thing to do Using downcasting in a situation that does not make sense usually results in a run-time error 8-13

Tip: Checking to See if Downcasting is Legitimate Downcasting to a specific type is only sensible if the object being cast is an instance of that type This is exactly what the instanceof operator tests for: object instanceof ClassName It will return true if object is of type ClassName; in particular, it will return true if object is an instance of any descendent class of ClassName 8-14

Pitfall: Limitations of Copy Constructors A copy constructor is supposed to create a good copy of a new object from an existing one However, when polymorphism is used, a copy constructor may have a strong limitation See Polymorphism4.java 8-15

A First Look at the clone Method Every object inherits a method named clone from the Object class The method clone has no parameters It is supposed to return a deep copy of the calling object However, the inherited version of the method is not designed to be used as is Instead, each class is expected to override it with a more appropriate version 8-16

A First Look at the clone Method The heading for the clone method defined in the Object class is as follows: protected Object clone() The heading for a clone method that overrides the clone method in the Object class can differ somewhat from the heading above A change to a more permissive access, such as from protected to public, is always allowed when overriding a method definition Changing the return type from Object to the type of the class being cloned is allowed because every class is a descendent class of the class Object This is an example of a covariant return type 8-17

A First Look at the clone Method If a class has a copy constructor, the clone method for that class can use the copy constructor to create the copy returned by the clone method public Vehicle clone() { return new Sale(this); } and another example: public Bus clone() { return new Bus(this); } See Polymorphism5.java 8-18

Pitfall: Limitations of Copy Constructors Although the clone() methods may in fact use the copy constructors to perform the copying, this works because the method clone has the same name in all classes, and polymorphism works with method names The copy constructors (i.e Vehicle, Bus, RaceCar) have different names, and polymorphism doesn't work with methods of different names 8-19

Pitfall: Sometime the clone Method Return Type is Object Prior to version 5.0, Java did not allow covariant return types, so no changes whatsoever were allowed in the return type of an overridden method Therefore, the clone method for all classes had Object as its return type Consequently, the clone method for any class, i.e. the Vehicle class would have looked like this: public Object clone() { return new Vehicle(this); } Therefore, the result needed to always be type casted when using a clone method written for an older version of Java Vehicle newvec = (Vehicle)originalVec.clone(); 8-20

Pitfall: Sometime the clone Method Return Type is Object It is still perfectly legal to use Object as the return type for a clone method, even with classes defined after Java version 5.0 When in doubt, it causes no harm to include the type cast For example, the following is legal for the clone method of the Vehicle class: Vehicle newvec = originalvec.clone(); However, adding the following type cast produces no problems: Vehicle newvec = (Vehicle)originalVec.clone(); 8-21

Abstract Class Sometimes, it is does NOT make sense to create objects from specific classes In such case, these classes should be created as abstract An abstract class can only be used to derive other classes; you cannot create objects from an abstract class An abstract class must have at least one abstract method 8-22

Abstract Method An abstract method has a complete method heading, to which has been added the modifier abstract It has no method body, and ends with a semicolon in place of its body public abstract long getsernumber(); An abstract method cannot be private See Abstract1.java 8-23

Abstract Class An abstract class can have any number of abstract and/or fully defined methods If a derived class of an abstract class adds to or does not define all of the abstract methods, then it is abstract also, and must add abstract to its modifier A class that has no abstract methods is called a concrete class 8-24

Pitfall: You Cannot Create Instances of an Abstract Class An abstract class constructor cannot be used to create an object of the abstract class However, a derived class constructor will include an invocation of the abstract class constructor in the form of super Although an object of an abstract class cannot be created, it is perfectly fine to have a parameter of an abstract class type This makes it possible to plug in an object of any of its descendent classes 8-25