150 likes | 281 Views
Exam Objective : Demonstrate the use of polymorphism. PRESENTED BY: SRINIVAS VG. Agenda :. Reference Variable Few examples Polymorphic arguments Roles of compiler and JVM in polymorphism. Lets see how polymorphism works???. Polymorphism means “many forms”
E N D
Exam Objective : Demonstrate the use of polymorphism PRESENTED BY: SRINIVAS VG
Agenda : • Reference Variable • Few examples • Polymorphic arguments • Roles of compiler and JVM in polymorphism
Lets see how polymorphism works??? • Polymorphism means “many forms” • Access an object through reference variable • Key things about reference variable • Can be of only one type • Once declared cannot be changed • Its type determines the methods that can be invoked on the object • It can be declared as class type or an interface type • It can refer to any object of the same type or it can refer to any subtype of the declared type ( in other words the reference type can be a superclass of the actual object type )
Examples : • Dog myDog = new Dog(); • Reference variable : myDog • Class : Dog • Cat myDog;---- > • In above reference variable and object are of same type • But in polymorphism they can be different eg. Animal mydog=new Dog();
// Declare an array of type animal that will hold objects of type animal • Animal [] animals = new Animal[3]; • animals [0] = new Dog(); // we can put ANY subclass of animal in the • animals [1]= new Cat(); // animal array • animals [2]= new Wolf(); • for(i=0;I < animals.length; i++ ){ • animals[i].eat(); // i is 0 , a Dog is at index 0 , so we get the Dog’s • eat() method . i is 1 Cat’s eat() method • animals[i].roam(); // same with roam() • }
More examples : Animal Superclass Dog Cat sleep() Mammal Interface
Dog passes the IS-A test for both Animal class and mammal interface • So Dog can be treated polymorphically as one of the 4 things :- - An object - A Animal - A Dog - A mammal • Legal Declarations are :- Dog doberman = new Dog(); • Object o=doberman; • Animal shape=doberman; • Mammal mover=doberman;
Any idea how many objects are formed ?? - Only 1!!! • 4 different types of reference variables , all referring to only one object • Which reference variable can invoke a eat() method ?? - Yup! Only Animal and Dog • Reason : eat() method is present in Animal class Dog class inherits from Animal
Which methods can be invoked when Dog object is being referred to using a reference declared as type mammal ??? - Only the sleep() method
package sam3; • interface Mammal { • publicvoid sleep(); • } • class Animal { • publicvoid eat() { • System.out.println("ofcourse we both eat "); • } • // more code • } • class Dog extends Animal implements Mammal{ • publicvoid makeNoise() { • System.out.println("hmmm...i usually dont bark.."); • } • publicvoid sleep(){ • System.out.println("I dont sleep at night..."); • } • } • class Cat extends Animal { • publicvoid roam() { • System.out.println("I am very lazy....."); • } • // more code • }
publicclass TestAnimals { • /* public static void doShapes(Animal shape) { • shape.eat(); • } • */ • publicstaticvoid main (String[] args) { • Dog doberman = new Dog(); • Object o= doberman; • Animal shape= doberman; • Mammal mover= doberman; • Cat tile = new Cat(); • Object j=tile; • doberman.sleep(); • doberman.eat(); • doberman.makeNoise(); • o.equals(j); • shape.eat(); • mover.sleep(); • // doShapes(player); • // doShapes(tile); • } • }
Polymorphic arguments : Eg. Say you declare a reference variable of supertype say Animal and assign a subcalss to it say Dog….see how it works class Vet { publicvoid qiveShot(Animal a){ //do horrible things to the Animal at // the other end of the 'a' parameter a.makeNoise(); } } class Petowner { publicvoid start () { Vet v = new Vet(); Dog d=new Dog(); Hippo h=new Hippo(); v.giveshot(d); v.giveShot(h); } }
So what’s the big deal ????? • You can write code that doesn’t have to change when you introduce new subclass types into the program • So with the Vet class having arguments declared as type Animal , your code can handle any Animal subclass • If others take advantage of Vet class , their new Animal type need to extend class Animal • Vet methods still work, even though Vet class was written without knowledge of new Animal subtypes
Compiler vs JVM • Compiler only knows about the declared reference type • JVM knows what the object really is • If Dog object’s eat() method is called using Animal reference variable , if Dog class overrides the eat() method , the JVM will invoke the Dog’s version ( ** Not the Animal’s version ** )