1 / 15

More on Polymorphism

More on Polymorphism. Ever have one of those days?. Review. Primitives Exhibit no polymorphic behavior Are required to be cast if a potential loss of information is possible Objects All objects are descended from a common ancestor, class Object

welshj
Download Presentation

More on Polymorphism

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. More on Polymorphism

  2. Ever have one of those days?

  3. Review • Primitives • Exhibit no polymorphic behavior • Are required to be cast if a potential loss of information is possible • Objects • All objects are descended from a common ancestor, class Object • When objects are created or instantiated some dynamic memory is allocated to the object. In this memory can be found “zones” representing each ancestor class starting from Object down to the actual type of the object. • A reference of any type in the ancestry of the object can refer to the object

  4. Review • Objects (continued) • During the compilation process each method invocation on a reference is checked to make sure that an object of the reference type has that method (includes searching up the inheritance tree). If not casting must be used. • During program execution each method invocation on a reference causes Java to find the lowest occurence of a method in the actual object. This is known as dynamic binding.

  5. Generic Collection Classes • We want to be able to make collection classes like: LinkedList, Set, Stack, Queue, etc. • We only want to write them once. • What are the problems we will face? • How can these problems be solved?

  6. Solutions • Problem: If we write our class to hold a specific type of object it will have to be rewritten for each different type object we wish to hold • Solution: Make collection classes hold Objects. Since all objects are children of Object we can polymorphically hold anything

  7. Solutions • Problem: If we need to tell if two objects are the same we can’t compare them using == since this just tells us if the references are referring to the same object. • Solution: Include in the class a method with the following signature • public boolean equals(Object o) • Have this method perform the check in whatever fashion is appropriate for the class • This will take on one of two forms

  8. equals form 1 • Given a class (class ID) which holds a name and a social security number you decide that objects of this class are equal if the SSN matches. • Add this method public boolean equals(Object o) { if(getSsn() == ((ID)o).getSsn()) return true; else return false; } • Why the cast?

  9. equals form 2 • You have a class LLNode and you want to be able to compare Nodes. The nodes contain an Object reference to the data and a LLNode reference to the next node. The nodes are equal if the data elements are equal. public boolean equals(Object o) { if(getData().equals(((LLNode)o).getData()) return true; else return false; }

  10. Solution • Problem: I want my collection to keep objects in order. A simple equals method is not enough. • Solution: Take CS 1312. There you will learn to use a method that looks like this: public int compareTo(Object o) • You will also learn about a lot of additional stuff like interfaces plus you’ll make new friends

  11. Solution • Problem: If I make my generic collection class hold Objects I’ll get errors when I remove objects and try to invoke their methods • Solution: As you remove the object cast it to what it needs to be to understand the method call • This is also a good place to use abstract classes and a well designed class hierarchy

  12. Example • You are creating an exhibit for an amusement park. • It will feature robotic animals • Some of the animals can speak, others cannot. • Those that speak do so differently • Lions roar • Birds chirp • Hyenas laugh • Basenjis say nothing

  13. Solution • Create a hierarchy like this: Animal Lion Bird Hyena Basenji • Add a method to Animal called speak • In each individual animal class override speak to do the appropriate thing for each animal

  14. Solution • Write code like this Queue q = new Queue(); q.enqueue(new Lion()); q.enqueue(new Hyena()); q.enqueue(new Bird()); q.enqueue(new Basenji()); while(! q.isEmpty()) { (Animal)(q.dequeue()).speak(); }

More Related