Akash Singh
Akash Singh

Reputation: 67

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.

Upvotes: 5

Views: 1506

Answers (5)

Ra Ka
Ra Ka

Reputation: 3055

Because, this is how method lookup work in Java.

  • First, the variable is accessed. in your case r.
  • Then, the object stored in the variable r is found which is b.
  • The class of the object is found which is B.
  • The class is searched for a method match. Now, B contain callMe() method. So, this method is executed instead of superclass callMe() method. If callMe() was not overidden, then A's callMe() method would have executed.

Also, this is required for polymorphism. For example: Let say, you have a super class Fruit with subclass Apple and Grape.

public class Fruit{
    public String getName() {
        return "Fruit";
    }
}
public class Apple extends Fruit{
    private String name;
    public Apple(String name){
        this.name = name;
    }
    public String getName(){
        return name;
    }
}
public class Grape extends Fruit{
    private String name;
    public Grape(String name){
        this.name = name;
    }
    public String getName(){
        return name;
    }
}

Now, you want to store instances of both Apple and Grape in same list.

Fruit apple = new Apple("apple");
Fruit grape = new Grape("grape");

ArrayList<Fruit> fruits = new ArrayList<Fruit>();
fruits.add(apple);
fruits.add(grape);

Now, you want to iterate through each item of fruit and get their name.

fruits.forEach(item -> {
    System.out.println(item.getName());
});

In this case, the type of item is Fruit and calling getName: you want to have name from respective child class Apple/Grape instead of "Fruit" value from Superclass Fruit.

Upvotes: 0

MC Emperor
MC Emperor

Reputation: 22967

...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

Upvotes: 2

Jiri Tousek
Jiri Tousek

Reputation: 12440

Java methods are virtual, so the run-time (actual) type of the object determines which method is called, not the static (declared) type of the variable.

However, the static (declared) type of the variable determines which methods (and also fields) are visible, i.e. which methods you may call.

Upvotes: 0

Priyantha
Priyantha

Reputation: 5083

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

Upvotes: 1

Aaron Brock
Aaron Brock

Reputation: 4536

I think you might be getting a little confused between Objects and variables. A variable can be thought of as a 'Pointer'. All it does is pointing to an Object.
E.g.

A var = newA(); // var -> Object A

Even though var1 is defined as type A, it can point to subclasses of that type.
E.g.

A var = new B(); // var -> Object B

Now, when you call a method on a variable, it calls that method on whatever object it's pointing to (even if the object is a subclass) E.g.

A var = new B(); // var -> Object B
var.someMethod(): // calls B.someMethod()

Even though var is of type A calling a method on it still invokes B.someMethod() because var points to Object B.

All the type of the Variable does is define what objects that variable can point to, so in are case all var being of type A means it can only point to Objects that are of type A or extend A.

I hope this helps! :)

Upvotes: 0

Related Questions