Namit Sinha
Namit Sinha

Reputation: 1445

Example of Runtime polymorphism in Java?

How is Runtime polymorphism different from Static polymorphism ?

Can this be an example of Runtime polymorphism ?

public class X
{
    public void methodA() // Base class method
    {
        System.out.println ("hello, I'm methodA of class X");
    }
}

public class Y extends X
{
    public void methodA() // Derived Class method
    {
        System.out.println ("hello, I'm methodA of class Y");
    }
}
public class Z
{
   public static void main (String args []) {
       X obj1 = new X(); // Reference and object X
       X obj2 = new Y(); // X reference but Y object
       obj1.methodA();
       obj2.methodA();
   }
}

The code has been picked from here

Upvotes: 8

Views: 27035

Answers (5)

user2048204
user2048204

Reputation: 759

I changed the main method a bit as below:

public class X
{
    public void methodA() // Base class method
    {
        System.out.println ("hello, I'm methodA of class X");
    }
}

public class Y extends X
{
    public void methodA() // Derived Class method
    {
        System.out.println ("hello, I'm methodA of class Y");
    }
}
public class Z
{
public static void main (String args []) {

    //this takes input from the user during runtime
    System.out.println("Enter x or y");
    Scanner scanner = new Scanner(System.in);
    String value= scanner.nextLine();

    if(value.equals("x"))
        X obj1 = new X(); // Reference and object X
    else if(value.equals("y"))
        X obj2 = new Y(); // X reference but Y object
    else
        System.out.println("Invalid param value");

    obj1.methodA();
    obj2.methodA();
}
}

Now, looking at the code you can never tell which method will be called. Because it depends on what value the user gives during runtime. So, it is only decided during the runtime as to which method will be called. Hence, Runtime polymorphism.

Upvotes: 0

nitz
nitz

Reputation: 27

Its runtime polymorphism as the compiler won't know till the run time about which object method to be called.

however the following line will give you cast exception:

Y obj1 = new X(); //Incorrect way

X obj1 = new Y(); //Correct way

Now obj1.methodA() calls methodA() in Class Y since obj1 is reference variable of object created for class Y

Upvotes: 2

NCA
NCA

Reputation: 820

Yes this is Runtime polymorphism in Java

In static polymorphism, compiler itself determines which method should call. Method overloading is an example of static polymorphism.

In runtime polymorphism, compiler cannot determine the method at compile time. Method overriding(as your example) is an example of runtime polymorphism. Because in Runtime polymorphism (as your example), the signature of methodA() is similar in both the class X(base class) and Y(child class). So compiler cannot determine method at compile time which should execute. Only after object creation(which is a run time process), the runtime environment understand the exact method to call.

It is because of that in this case, obj1.methodA() calls methodA() in Class X since obj1 is reference variable of object created for class X

AND obj2.methodA() calls methodA() in Class Y since obj2 is reference variable of object created for class Y

Upvotes: 9

codechefvaibhavkashyap
codechefvaibhavkashyap

Reputation: 1015

For your better understanding i've tried modulating your code. Note the call for constructor for both the classes.

class X
{
    X(){
        System.out.println("X constructor called");
    }
    public void methodA() //Base class method
    {
        System.out.println ("hello, I'm methodA of class X");
    }
}

class Y extends X
{
    Y(){
         System.out.println("Y constructor called");
    }

    public void methodA() //Derived Class method
    {
        System.out.println ("hello, I'm methodA of class Y");
    }
}

public class Z
{
   public static void main (String args []) {
       X obj1 = new X(); // Reference and object X
       X obj2 = new Y(); // X reference but Y object
       obj1.methodA();
       obj2.methodA();
   }
}

output :-

X constructor called

X constructor called

Y constructor called

hello, I'm methodA of class X

hello, I'm methodA of class Y

Carefully, look where objects have been created. It seems reference of X is being created using y. Method for X's is expected to be called but constructor call of Y for X reference creation says indirectly that memory has been allocated to Y's Object before X's reference is created. Take a look at the consoles for clarification.

Upvotes: 4

Marcus Widegren
Marcus Widegren

Reputation: 540

Yes your example is an example of runtime polymorphism. An example of static polymorphism would be method overloading. Here are some good examples: What is the difference between dynamic and static polymorphism in Java?

Cheers,

Marcus

Upvotes: 0

Related Questions