lect13polymorphism_4up - Polymorphism CMPT 126: Lecture 10...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CMPT 126: Lecture 10 Polymorphism Tamara Smyth, tamaras@cs.sfu.ca School of Computing Science, Simon Fraser University November 20, 2007 1 Polymorphism Polymorphism means having many forms . How does this related to object oriented programming? Consider the following reference: ChessPiece bishop; The bishop variable may be used to reference an object of type ChessPiece (an instantiation of the ChessPiece class). It is also possible however, to have this variable reference a compatible type. That is, the relationship between a reference variable and the object it refers to is more Fexible, and need not be exactly the same type. CMPT 126: Polymorphism, Lecture 10 2 Binding Since a polymorphic reference can refer to di±erent types of objects over time the speci²c method it invokes can also change from one invocation to the next. At a certain point, the method invocation is bound to the method de²nition, that is, a commitment is made to execute certain code. Though binding often occurs at compile-time, the binding of a method invocation to its de²nition cannot be made till run time for a polymorphic reference. Consider obj.doIt(); If obj is polymorphic, it can refer to di±erent types of objects at di±erent times, thus calling a di±erent de²nition of doIt() each time it is invoked. CMPT 126: Polymorphism, Lecture 10 3 Late or Dynamic Binding ³or polymorphic references, the method de²nition that is used depends on the object that is being referred to by the reference variable at that moment. This binding decision cannot be made until run time and is thus called late or dynamic binding. Though it is less e´cient for bindings to occur at run time rather than compile time, it is considered to be an acceptable overhead given the Fexibility of a polymorphic reference. There are two ways to create a polymorphic reference: 1. using inheritance 2. using an interface CMPT 126: Polymorphism, Lecture 10 4
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Polymorphism via Inheritance A reference variable of a particular type can refer to any object of that class or any of its subclasses. Example: if a Horse is a subclass of Mammal , then a Mammal reference can be used to refer to an object of the Horse class. Mammal pet; Horse stallion = new Horse(); pet = stallion; //a valid assignment The reverse is also allowed, but would require an explicit cast, and is generally considered to be more prone to problems (the Horse has all the functionality of a Mammal , but the reverse is not necessarily true). CMPT 126: Polymorphism, Lecture 10
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/07/2010 for the course CMPT 125 taught by Professor Diana during the Spring '08 term at Simon Fraser.

Page1 / 5

lect13polymorphism_4up - Polymorphism CMPT 126: Lecture 10...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online