Presentation is loading. Please wait.

Presentation is loading. Please wait.

Exam Objective : Demonstrate the use of polymorphism PRESENTED BY: SRINIVAS VG.

Similar presentations


Presentation on theme: "Exam Objective : Demonstrate the use of polymorphism PRESENTED BY: SRINIVAS VG."— 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 : eat() makeNoise() sleep() roam() Animal Dog Cat sleep() Mammal Interface Superclass

7  Dog passes the IS-A test for both Animal class and mammalinterface  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 { public void sleep(); } class Animal { public void eat() { System.out.println("ofcourse we both eat "); } // more code } class Dog extends Animal implements Mammal{ public void makeNoise() { System.out.println("hmmm...i usually dont bark.."); } public void sleep(){ System.out.println("I dont sleep at night..."); } class Cat extends Animal { public void roam() { System.out.println("I am very lazy....."); } // more code }

11 public class TestAnimals { /* public static void doShapes(Animal shape) { shape.eat(); } */ public static void 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 { public void qiveShot(Animal a){ //do horrible things to the Animal at // the other end of the 'a' parameter a.makeNoise(); } class Petowner { public void 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


Download ppt "Exam Objective : Demonstrate the use of polymorphism PRESENTED BY: SRINIVAS VG."

Similar presentations


Ads by Google