Exam objective demonstrate the use of polymorphism
Download
1 / 15

Exam Objective : Demonstrate the use of polymorphism - PowerPoint PPT Presentation


  • 82 Views
  • Uploaded on

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”

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Exam Objective : Demonstrate the use of polymorphism' - jethro


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

Agenda
Agenda :

  • Reference Variable

  • Few examples

  • Polymorphic arguments

  • Roles of compiler and JVM in polymorphism


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 )


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


  • // 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
More examples : type animal

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 ?? mammal interface

    - 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



  • package referred to using a reference declared as type mammal ??? 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

  • }


  • public referred to using a reference declared as type mammal ???class 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
Polymorphic arguments : referred to using a reference declared as type mammal ???

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
So what’s the big deal ????? referred to using a reference declared as type mammal ???

  • 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 vs JVM referred to using a reference declared as type mammal ???

  • 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 ** )


Thank You referred to using a reference declared as type mammal ???


ad