-1

I see some earlier questions similar to this, but I am looking for more specific answers.

1) In this case how/when does or what makes JVM to do perform dynamic binding?

  • I try to call derived class method derivedFunc1() using super class reference variable superobj. It fails (My Assumption for fail: The compiler says the member derivedFunc1() does not fall under the memory that is allocated(or restricted) for super class reference variable.)

  • I try to call method superFunc1() using super class reference variable superobj. Here inspite of having superFunc1() in super class it "somehow" calls superFunc1() from derived class. (This is contradicting my assumption, as accessing beyond its restricted scope of memory).

So my questions:

1) How superobj is able access derived class member superFunc1() but not able to access the other derived class member derivedFunc1().

2) Who/what makes a call for JVM to do perform dynamic binding?

enter image description here Edit: The image might not be valid as I came to know "derived class object does not create super class object"

import java.util.*;  
import java.text.*;

public class MyLocale {
    public static void main(String[] args) {
        MySuper superobj=new MyDerived();
        superobj.derivedFunc1(); //Statment1: fails because it cannot call functions from  derived class. (Okay I understand this part) 
        superobj.superFunc1(); //Statment2: has 2 options (one in super, one in derived) to call from and here it calls derived function. 

    }

}

class MySuper {
    void superFunc1() {
        System.out.println("Super");
    }

}
class MyDerived extends MySuper{
    void derivedFunc1() {
        System.out.println("Derived");
    }
    void superFunc1() {
        System.out.println("Derived");
    }
}
sql_dummy
  • 715
  • 8
  • 23
  • 2
    You should read this thing https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html and fix your terminology so it's clear what you're asking. Try to stick to 'method', don't say things like 'it fails' without being explicit about what happens and clarify the relationship you want your classes to have. 'Superclass' and 'Base class' are synonyms, for instance. – pvg Jan 09 '18 at 14:52

1 Answers1

1

There's a few things happening.

1) MyBase is a subclass of MySuper which means that superFunc1() of MyBase is overriding superFunc1() of MySuper.

2) MySuper superobj=new MyBase(); is taking a MyBase instance but telling the compiler to treat it as a MySuper.

3) MySuper only has one method (superFunc1), so that's the only method that you can invoke on superobj

4) however, at runtime, the JVM does a dynamic lookup (invokevirtual) of superFunc1 on superobj which is actually an instance of MyBase, so it invokes MyBase.superFunc1.

Relevant reading: https://www.javaworld.com/article/2076949/learn-java/how-the-java-virtual-machine-handles-method-invocation-and-return.html

Alejandro C.
  • 3,771
  • 15
  • 18
  • okay but whats restricting `superobj` to access `baseFunc1()` but okay to access `superFunc1`. – sql_dummy Jan 09 '18 at 14:59
  • @sql_dummy the compiler: the compiler treats `superobj` as an instance of `MySuper` (because that's what `superobj` is declared as). `MySuper` doesn't have `baseFunc1`, so the compiler gives you an error. – Alejandro C. Jan 09 '18 at 15:01
  • So in the case of `superFunc1()` why dint the compiler just resolve to func in super class but instead switch to dynamic binding. (Am sorry I am having trouble understanding the article you provided, I will try to go through it few more times, but meanwhile I accept the help with comments). – sql_dummy Jan 09 '18 at 15:15
  • The compiler doesn't actually do function invocation. At compile-time it just checks whether the code is valid and then at runtime the jvm uses an `invokedynamic` call to resolve `superFunc1`. `invokedynamic` will look at the instance its invoked on (`MyBase`). For `baseFunc1` it doesn't even get to that point (if it did, it would work), because the compiler emits an error first. – Alejandro C. Jan 09 '18 at 15:39