简体   繁体   中英

How does inheritance technical work?

I am about to give a presentation in my company (I am a trainee) about Inheritance in Java. I think I figured it out and I also know how to use it. But one thing is not so sure. Where are the methodes stored in the storage. And how does an object know where the methodes are?

For example:

We have this class.

class Animal {
    private String desc1;
    protected String desc2;

    public void breath() {
    }

    public void eat() {
    }
}

and this class:

  class Dog extends Animal() {
        public void bark() {
        }
    }

we now make an Object of the Dog class:

Dog dog = new Dog();

So now my questions: The classes are loaded in the heap. So Dog and Animal are in the heap. (EDIT: Thats wrong, classes don't get loaded in the heap, look answers below.). So, Lets say we make dog.bark() . How does dog know where the bark method is in the Heap? Knows it where the Dog class is? Next we make dog.eat(): So when dog knows where Dog is, does Dog know where Animal is or does dog know where Animal is? With know I mean it has a address where it is on the heap. And how is it when I overwrite a method? Where is that stored?

thx for helping.

First of all, this is JVM dependent and there is no requirement in the JLS or JVM spec on how these issues should be resolved, only that they should be resolved.

How does dog know where the bark method is in the Heap?

Usually you have what's called a Virtual Method Table , or v-table for short. You can think of this as a table mapping method identifiers, (such as bark ) to function pointers. That is, if you invoke bark the VM will look into it's v-table and call the method pointed to by bark . When you extend a class and override a method, the method pointer is simply swapped to point on the overriding function.

How does dog know where the bark method is in the Heap? Knows it where the Dog class is?

Methods and classes are not stored on the heap in the same sense as instances are. Why would they?

Perhaps you have the same misconception as I had when I started with OO-programming. I thought that if I instantiate 10 Dog , I get 10 bark-methods along with it. That's not true. What you "get" when you instantiate an object, is basically it's member variables and a this reference. You can then see the this reference as an extra argument passed along when invoking a non-static methods.

No, classes are not loaded into the heap (at least not what you could consider the heap in Java) but to a memory section called PermGen space.

I can't tell you exactly how Java stores that information, but I assume it's similar to the way that C++ does it (a vtable ), since it normally is done in JVM native code.

First any object created in java has a class reference somewhere in the object header. Linking "static" (not overriden, the JIT knows if any method is overriden but some subclass) method is easy and it can be inlined or just a call to some specific point where the code resides.

It's way more complicated since the optimized code is called with the values held in the registers while java spec demand them on the stack.

Calling override methods is done in few ways:

  • Dynamic check the instance and dispatch the call like in the static example, it's a single compare/branch call, much faster than
  • Inline caches
  • or already mentioned v-table calls. v-tables are the slowest

Understanding how the code is executed by the processor and the methods invoked/inlined, etc, is a vast topic, though. I guess some other basic concepts are missing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM