What is runtime polimorphism? give an example program ?

Questions by sadashivarao   answers by sadashivarao

Showing Answers 1 - 5 of 5 Answers

shivalak

  • May 3rd, 2006
 

Hello Rao,    Runtime polymorphism is the concept of identifying the object    reference at run time. This is also called as Late/Dynamic    Binding in C++.      The following example explains you how it works.   class SuperHandler {    public String handleRequest() {        return null;    }   }   class ReservationHandler extends SuperHandler {    public String handleRequest() {        //Implement your Reservation functionality here.        return "Hi, I am responsible for Reservation"; //output 1    }   }   class CancellationHandler extends SuperHandler {    public String handleRequest() {        //Implement your cancellation functionality here.        return "Hi, I am responsible for Cancellation"; //output 2    }   }   public class Client {    public static void main(String arg[]) {        String handlerString = arg[0];        SuperHandler handler = null; // Note 1        if(handlerString.equals("reserve")) {            handler = new ReservationHandler(); // Note 2        } else if(handlerString.equals("cancel")) {            handler = new CancellationHandler(); // Note 3        }        System.out.println(handler.handleRequest()); // Note 4    }   }   1. now compile this program (javac Client.java).   2. then run the program as follows       java Client reserve -- Results output 1("Hi, I am responsible for Reservation")       java Client cancel  -- Results output 2("Hi, I am responsible for Cancellation")           Can you understand what is really going on here,    Note 1. Creating the object for SuperHandler.   Note 2. Creating the object for ReservationHandler and pointing it to SuperHandler.   Note 3. Creating the object for CancellationHandler and pointing it to SuperHandler.   Note 4. Calling the method handleRequest() of the referred object.      All the above things are happening only at run time.    ie. the object reference is decided only after you pass    the input as reserve or cancel.          Hope this could atleast give you some idea.   Keep your hard work. All the best!      Cheers    Shiva.    Reach me at : shivalak@gmail.com   

  Was this answer useful?  Yes

shivalak

  • May 4th, 2006
 

Sorry the previous comments are not properly aligned.
Hello Rao,
    Runtime polymorphism is the concept of identifying the object
reference at run time.This is also called as Late/Dynamic Binding in C++.
The following example explains you how it works.
class SuperHandler {
public String handleRequest() {
    return null;
}
}
class ReservationHandler extends SuperHandler
{
    public String handleRequest() {
    //Implement your Reservation functionality here.
    return "Hi, I am responsible for Reservation";  // output 1
    }
}
class CancellationHandler extends SuperHandler {
    public String handleRequest() {
    //Implement your cancellation functionality here.
    return "Hi, I am responsible for Cancellation"; // output 2
    }
}
public class Client {
    public static void main(String arg[]) {
    String handlerString = arg[0];
    SuperHandler handler = null; // Note 1
    if(handlerString.equals("reserve")) {
        handler = new ReservationHandler(); // Note 2
    } else if(handlerString.equals("cancel")) {
        handler = new CancellationHandler(); // Note 3
    }
     System.out.println(handler.handleRequest()); // Note 4
    }
}
1. now compile this program (javac Client.java).
2. then run the program as follows
java Client reserve -- Results output 1("Hi, I am responsible for Reservation")
java Client cancel -- Results output 2("Hi, I am responsible for Cancellation")
Can you understand what is really going on here,
Note 1. Creating the object for SuperHandler.
Note 2. Creating the object for ReservationHandler and pointing it to SuperHandler.
Note 3. Creating the object for CancellationHandler and pointing it to SuperHandler.
Note 4. Calling the method handleRequest() of the referred object.
All the above things are happening only at run time.
ie. the object reference is decided only after you pass the input as reserve or cancel.

Hope this could atleast give you some idea.
Keep your hard work. All the best!
Cheers
Shiva. Reach me at : shivalak@gmail.com

rams_k

  • May 10th, 2006
 

runtime polymorphism means the compiler checks the object are instantiated to which class at run time i.e it first checks the class if not its super class,its super class until the correct obj instantiation for that method...

this is as per my knowlegge if iam wrong plz clarify me..

ramu.konkimalla@gmail.com

  Was this answer useful?  Yes

chaitanya

  • Aug 2nd, 2006
 

This is good example program

  Was this answer useful?  Yes

satya

  • Aug 3rd, 2006
 

thanks shiva for u r answer but concept of runtime is jvm checks the obj reference at runtime which class is instantiated to the obj it will be a super hierarchy i got it and thanks further

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions