1 / 15

Exam Objective : Demonstrate the use of polymorphism

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”

jethro
Download Presentation

Exam Objective : Demonstrate the use of 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. Exam Objective : Demonstrate the use of polymorphism PRESENTED BY: SRINIVAS VG

  2. Agenda : • Reference Variable • Few examples • Polymorphic arguments • Roles of compiler and JVM in polymorphism

  3. 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 )

  4. 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();

  5. // 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() • }

  6. More examples : Animal Superclass Dog Cat sleep() Mammal Interface

  7. 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;

  8. 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

  9. Which methods can be invoked when Dog object is being referred to using a reference declared as type mammal ??? - Only the sleep() method

  10. 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 • }

  11. 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); • } • }

  12. 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); } }

  13. 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

  14. 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 ** )

  15. Thank You

More Related