Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Polymorphism. Recall We set out to explore a new language, Java, which uses the Object Oriented Paradigm. We claimed that it would allow.

Similar presentations


Presentation on theme: "Introduction to Polymorphism. Recall We set out to explore a new language, Java, which uses the Object Oriented Paradigm. We claimed that it would allow."— Presentation transcript:

1 Introduction to Polymorphism

2 Recall We set out to explore a new language, Java, which uses the Object Oriented Paradigm. We claimed that it would allow us to have superior –Encapsulation –Reusability –Adaptability But as of now that may not be quite so apparent. The LinkedList class that we made held a linked list of StudentRecords but to hold anything else it will require some not insignificant amount of modification We need another tool to allow us to make collections that will hold anything!

3 Scenarios A veterinarian's algorithm might have a list of animals, but each one needs different food or care… we want ONE information system to track all of this without complex logic for each individual kind of animal. A car dealership sells many different types of cars with different features, but each has a price and quantity in stock. A registration system might treat in-state students differently from out-of-state students, graduate students differently from undergraduates, etc.

4 Motivation We’d like to be able to manage objects of different kinds of classes. Since classes within a class hierarchy often share common methods and attributes, we’d like to make use of this fact to make our algorithms simpler.

5 Polymorphism Polymorphism means many or multiple forms. Refers to the ability to tell an object to perform a method without knowing exactly which method is going to get invoked. May initially seem somewhat magical Well, actually it is...

6 A Class Hierarchy Animal DogCatFish

7 A Polymorphic Example Animal Dog Dog myDog; myDog = new Dog(); Animal myAnimal; myAnimal = myDog;

8 Extends Note that when Java uses inheritance the keyword is extends This is because literally making a subclass object in a sense also makes a superclass object!!! Huh? When we say: myDog = new Dog(); the Dog constructor gets called. It, in turn, must call the Animal constructor And here’s the big secret

9 Everything is an Object! That’s right! There is a class Object already defined When you don’t extend anything by default you extend Object Thus the Animal constructor calls the Object constructor Looking at an object in memory it would look something like this Object Animal Dog myDog Dog Object reference

10 Polymorphism Explained The rule is very simple A reference can refer to an object which is either –The same type as the reference –Has a superclass of the same type as the reference So all of the following are legal Dog d = new Dog(); Animal a = new Animal(); Object o = new Object; Object Animal Dog Object reference

11 Dog d; d = new Dog(); Animal a; a = new Dog(); Object o; o = new Dog(); Dog d; d = new Animal(); Animal a; a = new Animal(); Object o; o = new Animal(); Dog d; d = new Object(); Animal a; a = new Object(); Object o; o = new Object(); DogAnimalObject Reference Object Animal Dog Object Animal Dog Object Animal Dog Object Animal Object Animal Object Animal Object REF Could this be some form of Matrix ® ? Dog Animal Object

12 An Illegal Example We are able to assign an object of a sub-class into an object of a super-class as in: Animal MyAnimal = new Dog(); But the reverse is not true. We can’t assign a superclass object into a sub-class object. Dog MyDog = new Animal(); // illegal All dogs are animals but not all animals are dogs All dogs are animals but not all animals are dogs

13 Method Calls and Polymorphism Assume the Dog class extends the Animal class, redefining the “makeNoise” method. Consider the following: Animal MyAnimal = new Dog(); MyAnimal.makeNoise(); Note: The Animal reference is referring to a Dog object. And it’s the Dog’s makeNoise method that gets invoked!

14 Dynamic Binding Very simple rule. No matter what the reference type is, Java will search the object and execute the lowest occurence of a method it finds. class Object has a toString method Assume that both Animal and Dog have overridden the toString method Object toString() Animal toString() Dog toString() Object o Animal a Dog d A Dog Object o.toString(); a.toString(); d.toString();

15 Dynamic Binding Very simple rule. No matter what the reference type is, Java will search the object and execute the lowest occurrence of a method it finds. class Object has a toString method Even if Animal has no toString method! Object toString() Animal Dog toString() Object o Animal a Dog d A Dog Object o.toString(); a.toString(); d.toString();

16 Polymorphism vs. Inheritance Inheritance is required in order to achieve polymorphism (we must have class hierarchies). –Re-using class definitions via extension and redefinition Polymorphism is not required in order to achieve inheritance. –An object of class A acts as an object of class B (an ancestor to A).

17 Processing Collections One of the main benefits of polymorphism is the ability to easily process collections. We will consider a collection (queue) of Objects in the next example... Note: This means we rewrite our Queue class (and all associated classes) to hold Objects.

18 The Banking Class Hierarchy DeluxeSavings BankAccount SavingsAccount CheckingAccount NOWAccount MoneyMarketAccount CDAccount Object

19 A Collection of Bank Accounts Imagine a bank needs to manage all of the different types of accounts. Rather than maintain seven separate queues, one each for: BankAccounts, SavingsAccounts, DeluxeSavings, CDAccounts, CheckingAccounts, NOWAccounts, and MoneyMarketAccounts We can maintain only one queue of Objects.

20 Polymorphic Banking // Assume accounts of various kinds: CheckingAccount johnAaccount; DeluxeSavings paulAccount; CDAccount paulOtherAccount; NOWAccount georgeAccount; MoneyMarket ringoAccount; // Then put them all in a single structure // which is a Queue which holds Objects Queue acctQueue; acctQueue.enqueue(johnAccount); acctQueue.enqueue(paulAccount); acctQueue.enqueue(paulOtherAccount); acctQueue.enqueue(georgeAccount); acctQueue.enqueue(ringoAccount);

21 Polymorphic Banking accountQueue is polymorphic: It is holding accounts of “many forms.” Each of the accounts is “within the family” of the class hierarchy of bank accounts. Each one will have it’s own set of capabilities via inheritance (extension, and/or redefinition).

22 Example of Polymorphic Banking With polymorphism, our main algorithm doesn’t care what kind of account it is processing double sum;... sum = 0.0; while(! acctQueue.isEmpty()) { sum += ((BankAccount)(acctQueue.dequeue())).getBalance(); } System.out.println (“Sum of the balances is: ” + sum );

23 Resolving Polymorphic Method Calls The Java compiler is our friend Since we said the Queue was a Queue which was written to hold Objects what comes out is an Object reference class Object has no getBalance() method so the compiler would give us a friendly error message politely refuse to compile until we cast the object to a BankAccount object As annoying as this may seem it’s for our own good! Again, Java wants to catch as many problems at compile time to avoid errors at run time!

24 Polymorphism This is the “magic” of polymorphism…it keeps track of family members within the inheritance hierarchy for you. Without it, we’d have lots of code sprinkled through out our algorithm choosing among many options: if( it’s a Checking_Account ) call Checking_Account Calc_Interest else if( it’s a Super_Savings ) call Super_Savings Calc_Interest else if( it’s a CD_Account ) call CD_Account Calc_Interest else if( it’s a NOW_Account ) call NOW_Account Calc_Interest...

25 Summary Polymorphism allows objects to represent instances of its own class and any of its subclasses. Polymorphic collections are useful for managing objects since all objects are descendants of class Object No matter what the reference type the actual object knows what type it is When there is a choice dynamic binding goes to the lowest level.

26


Download ppt "Introduction to Polymorphism. Recall We set out to explore a new language, Java, which uses the Object Oriented Paradigm. We claimed that it would allow."

Similar presentations


Ads by Google