Polymorphic Variables CMPS2143. The Polymorphic Variable A polymorphic variable is a variable that can reference more than one type of object (that is,

Slides:



Advertisements
Similar presentations
Containers CMPS Reusable containers Simple data structures in almost all nontrivial programs Examples: vectors, linked lists, stacks, queues, binary.
Advertisements

Object Oriented Programming with Java
Overriding CMPS Overriding Recall, a method in a child class overrides a method in the parent class, if it has the same name and type signature.
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
A subclass can add new private instance variables A subclass can add new public, private or static methods A subclass can override inherited methods A.
Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
C12, Polymorphism “many forms” (greek: poly = many, morphos = form)
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance and Polymorphism CS180 Fall Definitions Inheritance – object oriented way to form new classes from pre-existing ones –Superclass The.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Static and Dynamic Behavior Fall 2005 OOPD John Anthony.
Chapter 10 Classes Continued
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Inheritance and Polymorphism CS351 – Programming Paradigms.
CSCI-383 Object-Oriented Programming & Design Lecture 15.
Subclasses and Subtypes CMPS Subclasses and Subtypes A class is a subclass if it has been built using inheritance. ▫ It says nothing about the meaning.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
Appendix A.2: Review of Java and Object-Oriented Programming: Part 2 “For the object-oriented project, remember that the primary unit of decomposition.
Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Static and Dynamic Behavior CMPS Power of OOP Derives from the ability of objects to change their behavior dynamically at run time. Static – refers.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
CSSE501 Object-Oriented Development. Chapter 12: Implications of Substitution  In this chapter we will investigate some of the implications of the principle.
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 5, page 1 Sun Certified Java 1.4 Programmer Chapter 5 Notes Gary Lance
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Inheritance in the Java programming language J. W. Rider.
CSSE501 Object-Oriented Development. Chapter 11: Static and Dynamic Behavior  In this chapter we will examine the differences between static and dynamic.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
1 COSC2767: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
Dynamic Binding Object-Oriented Programming Spring
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
Polymorphism CMPS Poly-morphism Means “many-forms” Means different things in biology, chemistry, computer science Means different things to functional.
Coming up: Inheritance
© 2004 Pearson Addison-Wesley. All rights reserved April 14, 2006 Polymorphism ComS 207: Programming I (in Java) Iowa State University, SPRING 2006 Instructor:
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
Inheritance and Polymorphism
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Modern Programming Tools And Techniques-I
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Lecture 12 Inheritance.
COSC2767: Object-Oriented Programming
Inheritance and Polymorphism
Types of Programming Languages
Object Oriented Analysis and Design
Inheritance Basics Programming with Inheritance
Inheritance, Polymorphism, and Interfaces. Oh My
Computer Programming with JAVA
Inheritance and Polymorphism
CIS 199 Final Review.
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Presentation transcript:

Polymorphic Variables CMPS2143

The Polymorphic Variable A polymorphic variable is a variable that can reference more than one type of object (that is, it can hold values of different types during the course of execution). In this chapter we will consider: ▫ Simple polymorphic variables ▫ The receiver variable ▫ Reverse polymorphism ▫ Pure Polymorphism

Simple Polymorphic Variables Example public class Shape{ } public Shape shapes[ ]; public void drawAll(Shape [] shapes) { for (int i = 0; i < numShapes; i++) shapes[i].drawSelf(); } The variable was declared as Shape, but actually held a number of different types.

The Receiver Variable The most common polymorphic variable is the one that holds the receiver during the execution of a method. Call this in C++ and Java, self in Smalltalk and Objective-C, current in Eiffel. Holds the actual value (the dynamic class) during execution, not the static class.

The Receiver Variable in Frameworks The greatest power of the receiver variable comes in the development of software frameworks. In a framework, some methods are implemented in the parent class and not overridden (called foundation methods), others are defined in the parent class, but intended to be overridden (called deferred methods). Consider a class Window with subclasses TextWindow and GraphicsWindow. The combination of foundation and deferred methods allow high level algorithms to be reused and specialized in new situations.

Example, Repainting Window Only the child class knows how to paint the window. The receiver variable is responsible for remembering the actual class of the receiver when executing the method in the parent class. abstract class Window { public void repaint () { // invoke the deferred method paint. Because the implicit // receiver, this, is polymorphic, the method from the // child class will be executed paint (graphicsContext); } abstract public void paint (Graphics g); // deferred private Graphics graphicsContext; } class GraphicsWindow extends Window { public void paint (Graphics g) { // do the appropriate painting job }

Self and Super In Java and Smalltalk there is another pseudo-variable, named super. Super is like self, only when a message is given to super it looks for the method in the parent class of the current class. class Parent { void exampleOne () { System.out.println("In parent method"); } class Child extends Parent { void exampleOne () { System.out.println("In child method"); super.exampleOne(); }

Downcast (Reverse Polymorpism) It is sometimes necessary to undo the assignment to a polymorphic variable. That is, to determine the variables true dynamic value, and assign it to a variable of the appropriate type. This process is termed down casting, or, since it is undoing the polymorphic assignment, reverse polymorphism. Various different syntaxes are used for this, see the book. Parent aVariable =...; Child aCard; if (aVariable instanceof Child) aChild = (Child) aVariable ;

Downcast (Reverse Polymorpism) Examples in Java and C++: Parent aVariable =...; Child aChild; if (aVariable instanceof Child) aChild = (Child) aVariable ; Parent * aVariable = new...; Child * aChild = dynamic_cast (aVariable); if (aChild != 0) {//null if not legal, nonnull if ok. : }

Pure Polymorphism A polymorphic method (also called pure polymorphism) occurs when a polymorphic variable is used as an argument. Different effects are formed by using different types of values. Different objects implement toString differently, so the effects will vary depending upon the argument. class StringBuffer { String append (Object value) { return append(value.toString()); }... }

Chapter Summary A polymorphic Variable is a variable that can reference more than one type of object. Polymorphic variables derive their power from interaction with inheritance, overriding and substitution. A common polymorphic variable is the implicit variable that maintains the receiver during the execution of a method Downcasting is the undoing of a polymorphic assignment Pure polymorphism occurs when a polymorphic variable is used as an argument.