CS2_22_PolymorphismDynamicBinding

CS2_22_PolymorphismDynamicBinding - CS2 Module 22 Category:...

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

View Full Document Right Arrow Icon
CS2 Module 22 Category: OO Concepts Topic: Polymorphism Objectives Polymorphism Dynamic Binding Constant Pool Class Pool
Background image of page 1

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

View Full DocumentRight Arrow Icon
CS 2 Introduction to Object Oriented Programming Module 22 OO Concepts Polymorphism
Background image of page 2
Polymorphism Polymorphism is an important concept that is a part of Object Oriented Programming The impetus is that we often would like to deal with a collection of various types of objects. We’d like to process members of that group in a generic way. Yet in the end, we’d like specific and appropriate behavior to still occur. Example. We have an array of animals, each of which is an instantiation/object of one subclass out of several possible subclasses of Animal. The array is declared to have Animal as it’s element type. Now we’d like to process through the array and have each element invoke a makeNoise() method. How is it that the each object will invoke the correct makeNoise() method? I.e. a Dog object will bark, a Cat object will meow…
Background image of page 3

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

View Full DocumentRight Arrow Icon
Polymorphism Subclasses of a given superclass will typically have many methods in common. (Subclasses of Shape all have an area() method. Subclasses of Animal all have a makeNoise() method.) Such methods conveniently provide the same or similar behavior among the subclasses, yet each method is typically refined to be more appropriate for it’s particular subclass. Luckily when a method call is made, the compiler isn’t too concerned about the specifics of the method being called. It’s question is: is there a method with a matching signature?” e.g. you have a simulated ecology with animals in it (SimZoo). You want to make all the animals make noise But animals make noise in different ways Polymorphism will guarantee that a specific dog or cat or ??? Will indeed make the appropriate noise for it’s type.
Background image of page 4
Polymorphism Polymorphism class Animal { public void makeNoise ( ) { System.out.println("I am an animal."); } // of makeNoise } // of Animal class Fish extends Animal { public void makeNoise ( ) { System.out.println("Glug glug gurgle gurgle"); } // of makeNoise } // of Fish class Bird extends Animal { public void makeNoise ( ) { System.out.println("Tweet tweet flap flap"); } // of makeNoise } // of Bird
Background image of page 5

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

View Full DocumentRight Arrow Icon
Polymorphism (cont’d) Polymorphism (cont’d) class Dog extends Animal { public void makeNoise ( ) { System.out.println("Sniff sniff woof woof"); } // of makeNoise public void bark() { System.out.println("Arf Arf"); } // of bark } // of Dog
Background image of page 6
Polymorphism Polymorphism public class Driver { public static void main (String[ ] argv) { Animal[ ] animalArray = new Animal[3]; int index; animalArray[0] = new Bird( ); animalArray[1] = new Dog( ); animalArray[2] = new Fish( ); for (index = 0; index < animalArray.length; index++) { animalArray[index].makeNoise( ); } // of for } // of main } // of Driver Output: Tweet tweet flap flap Sniff sniff woof woof Glug glug gurgle gurgle the Animal class has makeNoise, so any member of the array can makeNoise
Background image of page 7

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

View Full DocumentRight Arrow Icon
Polymorphism Polymorphism Polymorphism means “ taking many forms” ... a reference of a given class can adapt take the form of any of its subclasses.
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/23/2012 for the course CS 1371 taught by Professor Stallworth during the Fall '08 term at Georgia Institute of Technology.

Page1 / 39

CS2_22_PolymorphismDynamicBinding - CS2 Module 22 Category:...

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

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