1

As per this answer in the link https://stackoverflow.com/a/6606490/4816065

In some of Sun's implementations of the Java virtual machine, a reference to a class instance is a pointer to a handle that is itself a pair of pointers: one to a table containing the methods of the object and a pointer to the Class object that represents the type of the object, and the other to the memory allocated from the heap for the object data.

So given, a two classes A and B as follows :

class A{
    void m1(){
        System.out.println(" from m1 method ");
    }
}

class B extends A {
    void m2(){
        System.out.println(" from m2 method ");
    }
}

In main method I make object as follows :

A a1 = new A(); // The dispatch table of a1 contains only method m1.
B b1 = new B(); // The dispatch table of b1 contains method m1 and m2.
A a2 = new B(); // What will the dispatch table of a2 be?

Will it have m1 only (since the Object reference is of A type) or will it have m1 and m2 (since the object referred is of B type) ?

Paul Karam
  • 4,052
  • 8
  • 30
  • 53

0 Answers0