Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does dynamic method dispatching work in Java

Can a superclass variable access an overridden method of a subclass. For ex:

class A {
    void callMe() {
        System.out.println("Inside A");
    }
}

class B extends A {
    void callMe() {
        System.out.println("Inside B");
    }
}

class Dispatch {
    public static void main(String args[]) {
        A a = new A();
        B b = new B(); // Object of type B
        A r; // Obtain a reference of type A

        r = a; // Refers to A object
        r.callMe(); // Calls A's version of callMe()

        r = b; // Refers to B object
        r.callMe(); // calls B's version of callMe() and my question is on this
    }
}

I learned earlier that a superclass variable that is referencing a subclass object can access only those parts of an object that are defined by the superclass. Then how can the second r.callMe() call B's version of callMe()? It should only call A's version of callMe() again.

like image 731
Akash Singh Avatar asked Mar 28 '17 06:03

Akash Singh


People also ask

How dynamic method dispatch is accomplished in Java?

Runtime Polymorphism in Java is achieved by Method overriding in which a child class overrides a method in its parent.

Is dynamic method dispatch necessary in Java?

Dynamic dispatch method is important because it is a process through which Java implements runtime polymorphism.

Why do we need dynamic method dispatch?

With inheritance / polymorphism we don't know the concrete runtime type of an expression, thus which method to be called must be "dynamically" determined during runtime. Without dynamic dispatch, virtual methods wouldn't make sense, which is central for abstraction and encapsulation.

How the methods are resolved dynamically at runtime?

Dynamic method dispatch is a mechanism by which a call to an overridden method is resolved at runtime. This is how java implements runtime polymorphism. When an overridden method is called by a reference, java determines which version of that method to execute based on the type of object it refer to.


2 Answers

...a superclass variable that is referencing a subclass object can access only those parts of object that are defined by superclass

That's not entirely correct. In the end, the runtime calls the actual type of the object, regardless of the reference type. So r.callme() will actually call callme() defined in B, because r is a B object.

new B();       // <-- The object in memory is of type B and its type never
               //         changes.
A a = new B(); // <-- The object in memory is of type B; the reference type
               //         is A. But that effectively does only matter at
               //         compile-time, I believe.

In the example above, B is called the object type, and A is called the reference type.

See Java Language Specification § 15.12.4.4:

Let X be the compile-time type of the target reference of the method invocation.

[...]

If the invocation mode is virtual, and the declaration in S overrides X.m (§8.4.8.1), then the method declared in S is the method to be invoked, and the procedure terminates.


Let me make a rough guess at what they mean by "access only those parts [...] defined by superclass":

class A {
    void doSomething() { }
}
class B extends A {
    void doAnotherThing() { }
}
A a = new B();
a.doAnotherThing(); // Not valid, because doAnotherthing()
                    // is defined in class B.

In order to call doAnotherThing(), a type cast must be used:

((B) a).doAnotherThing(); // Valid
like image 87
MC Emperor Avatar answered Oct 18 '22 23:10

MC Emperor


In your question

r=b;

now r catch "new B()" object.When u call r.callme() then run callme method in B class. Because r has B object.

Any program will throw a compile time error since reference type of super class doesn't have a method by the name of sub class.

As the example

class Animal {
  public void move() {
     System.out.println("Animals can move");
  }
}

class Dog extends Animal {
  public void move() {
     System.out.println("Dogs can walk and run");
  }

  public void bark() {
     System.out.println("Dogs can bark");
  }
 }

 public class TestDog {

  public static void main(String args[]) {
    Animal a = new Animal();   // Animal reference and object
    Animal b = new Dog();   // Animal reference but Dog object

    a.move();   // runs the method in Animal class
    b.move();   // runs the method in Dog class
    b.bark();
 }
}

Output

TestDog.java:26: error: cannot find symbol
  b.bark();
   ^
 symbol:   method bark()
 location: variable b of type Animal
 1 error
like image 45
Priyantha Avatar answered Oct 18 '22 22:10

Priyantha