{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CS2_22_PolymorphismDynamicBinding

CS2_22_PolymorphismDynamicBinding - CS2 Module 22 Category...

Info icon This 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
Image of page 1

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

View Full Document Right Arrow Icon
CS 2 Introduction to Object Oriented Programming Module 22 OO Concepts Polymorphism
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…
Image of page 3

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

View Full Document Right 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.
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
Image of page 5

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

View Full Document Right 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
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
Image of page 7

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

View Full Document Right 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.
Image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern