Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 239
Namit Sinha Avatar asked Mar 10 '15 10:03

Namit Sinha


People also ask

What is runtime polymorphism in Java with real time example?

Example of Runtime Polymorphism in Java Since it refers to the subclass object and subclass method overrides the Parent class method, subclass method is invoked at runtime. Here method invocation is determined by the JVM not compiler, So it is known as runtime polymorphism.

Which is runtime polymorphism in Java?

Runtime polymorphism, also known as the Dynamic Method Dispatch, is a process that resolves a call to an overridden method at runtime. The process involves the use of the reference variable of a superclass to call for an overridden method.

What is an example of polymorphism in Java?

Note: The print() method is also an example of polymorphism. It is used to print values of different types like char , int , string , etc. We can achieve polymorphism in Java using the following ways: Method Overriding.

What is polymorphism runtime and compile time polymorphism with example?

Its is a concept by which we can perform single task in multiple ways. There are two types of polymorphism one is Compile-time polymorphism and another is run-time polymorphism. Method overloading is the example of compile time polymorphism and method overriding is the example of run-time polymorphism.


2 Answers

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

like image 121
NCA Avatar answered Sep 22 '22 12:09

NCA


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.

like image 36
codechefvaibhavkashyap Avatar answered Sep 24 '22 12:09

codechefvaibhavkashyap