Search code examples
javajvmlate-bindingmethod-resolution-orderearly-binding

Inheritance and memory allocation


for the following code:

class Parent {
    public void say(){
        System.out.println("Parent");
    }
}

class Child extends Parent{
    public void say(){
        System.out.println("Parent");
    }
}

class Test {
    public static void main(String[] args){

        Parent p = new Child(); // What does the compiler check  
        p.say();
    }
}

The following questions have always confused me.

1. what exacly is happening during compile time? How does the comiler know that Parent p = new Child() is a valid code as no object has been created at compile time?

2. what exactly is hapenning at runtime? How is memory allocated

3. The implicit call to parent's constructor creates an Parent Object. A child object is also created. How is it stored and how does jvm use this for method resolution.

4. Lastly, is JVM hardcoded not to ovverride static methods and variables or there is some other reason. Why can't we use Runtime objects instance method() why not runtime objects variables

Where can I read more on these topics?

Thanks


Solution

  • exactly is happening during compile time? How does the comiler know that Parent p = new Child() is a valid code as no object has been created at compile time?

    Well, whole books are written about that subject. In essence, the compiler parses the input source, and it finds that assignment:

    Parent p = new Child(); 
    

    And now the compiler does two things:

    • it checks what it can find out about the types on both sides of the assignment
    • thus it finds: the left hand side needs an object of type `Parent``
    • it also looks at the right hand side, to understand the (potential) result types that comes out of that expression

    So, in the end, the compiler will see: "I need a Parent ... and I get something that is a Child." Then the compiler has to check if an instance of Child satisfies "I need a Parent". And obviously: it does.

    what exactly is happening at runtime? How is memory allocated

    Again, whole books are written about that. See here for example.

    The implicit call to parent's constructor creates an Parent Object. A child object is also created. How is it stored and how does jvm use this for method resolution.

    The JVM "simply" knows two things:

    • when reserving the memory for a Child object, it needs to also consider the memory required for any super class fields
    • so, in the end, you simply have one area of memories large enough to hold all fields of all parent classes, down to the Child class itself

    Finally: static methods are inherented, but yes, there is no polymorphism for them in Java. That is simply a design point of the language, and the semantics that are defined for Java. It could be done differently, but 20+ ago, the fathers of Java decided to do it like they did.