exam objective : demonstrate the use of polymorphism presented by: srinivas vg

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

Upload: gilbert-osborne-lester

Post on 18-Dec-2015

221 views

Category:

Documents


1 download

TRANSCRIPT

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

Exam Objective : Demonstrate Exam Objective : Demonstrate the use of polymorphismthe use of polymorphism

PRESENTED BY:

SRINIVAS VG

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

Agenda :Agenda :

Reference Variable

Few examples

Polymorphic arguments

Roles of compiler and JVM in polymorphism

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

Lets see how polymorphism works???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 )

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

Examples :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();

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

// 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()

}

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

More examples :More examples :

eat()

makeNoise()

sleep()roam()

Animal

Dog Cat

sleep()Mammal

Interface

Superclass

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

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;

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

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

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

Which methods can be invoked when Dog object is being referred to using a reference declared as type mammal ???

- Only the sleep() method

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

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 }

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

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

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

Polymorphic arguments :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); } }

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

So what’s the big deal ?????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

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

Compiler vs JVMCompiler 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 ** )

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