aspose file tools
The moose likes Java in General and the fly likes runtime polymorphism ? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "runtime polymorphism ?" Watch "runtime polymorphism ?" New topic
Author

runtime polymorphism ?

vianyrajnish rajnish
Ranch Hand

Joined: Apr 22, 2007
Posts: 70
what is run time polymorphism ? and also give rationale for my below question on the example.........

ex :
class A {

void act() {
System.out.println("in class A : super");
}
}

class B {
void act() {
System.out.println("in clas B : subclass");
}
}

class Testclass {
public static void main(String args[])
{
A a = new B();
a.act(); // this will print "in class B : subclass"
}
}

In above example the output would be " in class B : sublass " .
because reference is pointing to Object B.
My question is this statement in above ex is the compile time
A a = new B();

how could you justify it is runtime ?


please anyone answer this question,,,,,,,,,,,,,,,,,


THANKS,
VINAY RAJNISH
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3327

Hi Vinay,

Its about the ability to decide which version of the method to be invoked * during runtime *. Means, at runtime whatever the actual object is being pointed to by the reference variable "a", would be invoked.

During compile time, it is not throwing any error because,
  • A reference variable to the parent/super class variable can hold the objects of any of its child/sub class. Thats why,


  • is allowed and compiled without any error.

  • Since because the method invoked with respect to the super class variable "a" has the method, the next line is compiled without an error. Does not matter whatever the reference variable "a" is assigned to, the statement invoking the method "act" with respect to "a" would be allowed because "a" is of type "A" and the class "A" has the method.

  • Try this code,


    The above code would definitely compile without any errors. Only at runtime, it checks for the object being pointed by the reference variable "a" and tries to invoke the method "act()" wrt to that object. At this time it finds the refernece variable is not pointing to, rather not assigned any actual object reference and it cannot invoke any method on a null reference. So you get the NullPointerException (NPE) at the runtime and NOT during the compile time.



    Hope this helps.

    HtH.


    Everything has got its own deadline including one's EGO!
    [CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
    Stan James
    (instanceof Sidekick)
    Ranch Hand

    Joined: Jan 29, 2003
    Posts: 8791
    When you write these two lines together:

    it looks like you have everything you need to know right now, and the compiler could make some "compile time" decisions. When you split them up, perhaps you don't even know who is going to write the "new B()" part ...

    we don't know at compile time whether the argument is an instance of class A or some subclass. That means it has to be a runtime decision to invoke the method from class A or B or some other that we've never heard of.


    A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
     
    I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
     
    subject: runtime polymorphism ?
     
    Similar Threads
    doubt regarding assigning superclass ref var to subclass var
    Complie time polymorphism vs Runtime polymorphism
    Inherited Method Calls from Mock
    Polymorphism
    Runtime Polymorphism