09 polymorphism


Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

09 polymorphism

  1. 1. Using IS-A and HAS-A<br />LIS4930 © PIC<br />When you want to know if one thing should extend another; apply the IS-A test.<br />Triangle IS-A Shape.<br />YES<br />Cat IS-A Feline.<br />YES<br />Bathroom has a reference to a Tub, but Bathroom does not extend Tub and vice-versa.<br />Surgeon IS-A Doctor.<br />YES<br />Tub IS-A Bathroom.<br />NO<br />Bathroom HAS-A Tub.<br />Tub and Bathroom are related, but not through inheritance: Bathroom HAS-A Tub instance variable.<br />
  2. 2. Wait! There’s More!<br />LIS4930 © PIC<br />If class B extends A, class B IS-A class A. This is true anywhere in the inheritance tree. If class C extends class B, class C passes the IS-A test for both B and A.<br />Canine extends Animal<br />Wolf extends Canine<br />Wolf extends Animal<br />Canine IS-A Animal<br />Wolf IS-A Canine<br />Wolf IS-A Animal<br />Keep in mind that the inheritance IS-A relationship works in only ONE direction!<br />Animal<br />makeNoise()<br />eat()<br />sleep()<br />roam()<br />Canine<br />roam()<br />Wolf<br />makeNoise()<br />eat()<br />
  3. 3. Test Yourself!<br />LIS4930 © PIC<br />Oven extends Kitchen<br />Guitar extends Instrument<br />Person extends Employee<br />Ferrari extends Engine<br />FriedEgg extends Food<br />Beagle extends Pet<br />Container extends Jar<br />Metal extends Titanium<br />Beverage extends Coke<br />
  4. 4. Access Level Control<br />LIS4930 © PIC<br />A subclass inherits all public instance variables and methods of the superclass, but does not inherit the private instance variables and methods of the superclass.<br />public members are inherited<br />private members are not inherited<br />Mammal<br />private weight<br />public color<br />private eat()<br />private roam()<br />public sleep()<br />Elephant<br />makeNoise()<br />
  5. 5. Polymorphism! What is that?<br />LIS4930 © PIC<br />I think this is best explained with an example. Let’s step back and look at the way we normally declare a reference and create an object…<br />myDog<br />Declare a reference variable<br />Dog myDog<br />1<br />Dog<br />
  6. 6. Polymorphism! What is that?<br />LIS4930 © PIC<br />Dog<br />Create an object<br />Dog myDog = new Dog()<br />2<br />Dog object<br />
  7. 7. Polymorphism! What is that?<br />LIS4930 © PIC<br />myDog<br />Dog<br />Link the object and the reference<br />Dog myDog= new Dog()<br />3<br />Dog<br />Dog object<br />
  8. 8. Polymorphism! What is that?<br />LIS4930 © PIC<br />myDog<br />Dog<br />Dog<br />Dog object<br />The important point is that the reference type AND the object type are the same.<br />
  9. 9. Polymorphism! What is that?<br />LIS4930 © PIC<br />myDog<br />Dog<br />Animal<br />Dog object<br />But with polymorphism, the reference and the object can be different.<br />Animal myDog= new Dog()<br />
  10. 10. Polymorphic Example<br />LIS4930 © PIC<br /> Animal[ ] animals = new Animal[5];<br /> animals[0] = new Dog();<br /> animals[1] = new Cat();<br /> animals[2] = new Wolf();<br /> animals[3] = new Hippo();<br /> animals[4] = new Lion();<br />for(inti = 0; i < animals.length; i++){<br />animals[i].eat();<br />animals[i].roam();<br /> } <br />
  11. 11. You can have polymorphic arguments and return types<br />LIS4930 © PIC<br />class Vet {<br /> public void giveShot(Animal a) {<br />a.makeNoise();<br /> }<br />}<br />a<br />class PetOwner {<br /> public void start() {<br /> Vet v = new Vet();<br /> Dog d = new Dog();<br /> Hippo h = new Hippo();<br />v.giveShot(d);<br />v.giveShot(h);<br /> }<br />}<br />Animal<br />
  12. 12. Taking Advantage of Polymorphism<br />LIS4930 © PIC<br />With polymorphism, you can write code that doesn’t have to change when you introduce new subclass types in the program.<br />That means if others want to take advantage of your Vet class, all they have to do is make sure their new Animal types extend class Animal.<br />
  13. 13. Keeping the Contract: Rules for Overriding<br />LIS4930 © PIC<br />Arguments must be the same, and return types must be compatible<br />1<br />This is NOT an override. Can’t change the arguments in an overriding method!<br />This is actually a legal overLOAD, but not an overRIDE.<br />Appliance<br />booleanturnOn()<br />Toaster<br />booleanturnOn(int level)<br />
  14. 14. Keeping the Contract: Rules for Overriding<br />LIS4930 © PIC<br />The method can’t be less accessible.<br />2<br />NOT LEGAL! It’s not a legal override because we restricted the access level. Nor is it a legal overLOAD because we didn’t change arguments.<br />Appliance<br />public booleanturnOn()<br />Toaster<br />private booleanturnOn()<br />
  15. 15. Overloading A Method<br />LIS4930 © PIC<br />Method overloading is nothing more than having two methods with the same name but different argument lists. Period. It has nothing to do with inheritance and polymorphism. An overloaded method is NOT the same as an overridden method.<br />1<br />2<br />3<br />The return types can be different<br />public class Overloads {<br /> String uniqueID;<br /> public intaddNums(int a, intb) {<br /> return a + b;<br /> }<br /> public double addNums(double a, double b) {<br /> return a + b;<br /> } <br /> public void setUniqueID(String ID){<br />uniqueID = theID;<br /> }<br />private void setUniqueID(intssNumber){<br /> String numString = “” + ssNumber;<br />setUniqueID(numString);<br /> }<br />}<br />You can’t change ONLY the return type<br />You can vary the access levels in any direction<br />