Smatik
Smatik

Reputation: 407

Dynamic Method Dispatch and inheritance

I was reading a JAVA-book and came across the Dynamic Method Dispatch. But it was a little confusing for me(maybe because I am a newb). The book said it was based on the principle: a superclass reference variable can refer to a subclass object.

    class X{
    void display()
    {
       System.out.println("This is class X");
    }
    }

    class Y extends X{
    void display()
    {
        System.out.println("This is class Y");
    }
    void play()
    {
        System.out.println("PLAY!");
    }
    }

    class k{
    public static void main(String args[]){
    X obj1 = new X();
    Y obj2 = new Y();
    X ref  = new X();

    ref = obj1;
    ref.display();
    //output is :This is class X

    ref = obj2;   //Using the principle stated above
    ref.display();
    //output is :This is class Y

    ref.play();  //Compiler error:Play not found 
    //well it must be because ref is of X type and for X no methods of its subclass "Y"
    //is visible
    }
    }

So i wanted to ask that if play() is not visible then why display() of Y is visible??

Upvotes: 2

Views: 1502

Answers (4)

Alexei Kaigorodov
Alexei Kaigorodov

Reputation: 13515

Before execution, Java programs are processed by compiler. Compiler makes sure, among many other things, that only existing methods are called. In the case above, compiler only knows that ref is of type X, as declared, and that type X has no method play. It does not track all the assignments and does not takes into consideration that ref was assigned by obj2 of type Y - such tracking is impossible in general case, as it may depend on runtime data, unavailable at compile time.

On the other hand, method invocation at runtime relies on actual type, which can be extension of declared type. The very same code, issued by the compiler, can call different implementations of method display, depending on the actual type of the object referenced by the variable ref. Compiler, however, checks that method display exists for all possible types of objects which may be assigned to ref.

Upvotes: 0

Juned Ahsan
Juned Ahsan

Reputation: 68715

A parent class reference holds the property of a child object which are inherited from parent. Hence you can call all those methods from a parent reference holding a child object, which are inherited and overriden by the child class.

In short, reference will decide what methods can be called, the object it is holding will decide, which method(parent/subclass) will be called.

So i wanted to ask that if play() is not visible then why display() of Y is visible??

display is visible because that is a method of Parent reference. But play belongs to only child, hence it is not visible to the Parent.

Upvotes: 0

Donal Fellows
Donal Fellows

Reputation: 137557

Think of it in terms of promises. X ref promises that what it holds (if anything) is of type X. Of course, the Y is also of type X, that is what subclassing means: every instance of Y is an instance of X as well (plus some adjustments and extra features). However, the play method can't be called through it as the contents are not known to be a Y, according to the language definition.

You can explicitly cast the ref to a Y and then call play on it:

((Y) ref).play();

This is safe because each object in Java knows its own real type. If the object reference was to an instance of the wrong type (e.g., obj1) you will get a ClassCastException when you run the code. (If you know C, this is completely different.)

Upvotes: 1

user2545918
user2545918

Reputation:

Static type checking ensures, that you can call those methods only, which belong to the static (declared) type of the reference. That is why you cannot call .play() via a reference with type X.

However, dynamic method dispatch ensures, that if a method is overridden in a subclass, then that specific method will be called dynamically (at run time).

Upvotes: 0

Related Questions