Zeeshan
Zeeshan

Reputation: 12421

How methods are inherited in java

I am having the following Classes:

package com.zee.main;
import com.zee.sub.Sub;

public class MainClass {
    public static void main(String[] args) {
        Sub sub = new Sub();
        sub.add();
    }
}

package com.zee.sup;
public class Super 
{
    private int a = 2;
    private int b = 3;

    public void add(){
        System.out.println("Result: "+(a+b));
        System.out.println("Class:" +this.getClass().getName());        
    }
}

package com.zee.sub;
import com.zee.sup.Super;

public class Sub extends Super {
}

Output:

Result: 5
Class:com.zee.sub.Sub

How do a Subclass inherit the method of its Superclass? I was in the assumption that the method defined in the Superclass becomes the part of Subclass but when I checked the compiled bytecode of Sub, I found that there is no add method that was defined in the Super class.

// Compiled from Sub.java (version 1.6 : 50.0, super bit)
public class com.zee.sub.Sub extends com.zee.sup.Super {

  // Method descriptor #6 ()V
  // Stack: 1, Locals: 1
  public Sub();
    0  aload_0 [this]
    1  invokespecial com.zee.sup.Super() [8]
    4  return
      Line numbers:
        [pc: 0, line: 5]
      Local variable table:
        [pc: 0, pc: 5] local: this index: 0 type: com.zee.sub.Sub
}

Following are my questions:

  1. Since there is no add method in the compiled bytecode of Sub, the add method of Superclass must have been called at runtime.

  2. Contradicting the first point is the this.getClass().getName() which returns the runtime class of this Object, but its printing Sub as the runtime class.

  3. If the runtime class is Sub, then how it is accessing the private fields a and b of the Superclass in its add method?

Can someone please explain. Thanks in advance

Upvotes: 0

Views: 240

Answers (3)

arcy
arcy

Reputation: 13103

Since there is no add method in the compiled bytecode of Sub, the add method of Superclass must have been called at runtime.

Yes, method calls are by definition a runtime procedure.

Contradicting the first point is the this.getClass().getName() which returns the runtime class of this Object, but its printing Sub as the runtime class.

There is no contradiction here. The add() method is defined in the superclass, it is not redefined in the subclass, and so when add is called on the subclass, the method from the superclass is invoked.

If the runtime class is Sub, then how it is accessing the private fields a and b of the Superclass in its add method?

It is accessing them by virtue of being a method within the superclass, which has access to that class' private instance variables. Normal scope rules apply.

This may help you overall: as I understand it, when a class is put together, there is what we may think of as a list of methods in a table. When a method is invoked, really what happens is that the address of the correct method is retrieved from this "jump table". In the case at hand, the place in the jump table for add() is occupied by the address from the superclass method. That method, in turn, has offsets for accessing things like the private variables; the setup of the class cleverly puts them at the same offsets in both the subclass and superclass so they'll work in both places.

So the add method invoked for the subclass is really the same method, using the same instance variable locations, as it would be for the superclass.

I hope that helps explain things.

Upvotes: 2

user3735278
user3735278

Reputation: 261

Sub can access private fields from Superclass because Sub is of type Superclass and it gains access to these private fields when calling upon methods defined in Superclass. It, however, can not call upon these fields when you attempt to make methods that will only belong to the Sub class due to Superclass privatizing "direct" access to these fields from its children.

Upvotes: -2

Your "questions" aren't exactly questions, but to respond to them:

  • If you disassembled the main method, you'd see a call to invokevirtual for add(). This tells the JVM to look up the appropriate method to call in the class hierarchy, starting with the subclass and working its way up if it doesn't find an implementation there.

  • Object#getClass() is one of the special infrastructure methods in Java. It's implemented with native code and hooks into the JVM internals to identify the class of the object in question.

  • The class Sub doesn't access the private members. The code in Super accesses them.

Upvotes: 3

Related Questions