Let's say I have this piece of code:
class Animal {
int legs = 4;
int head = 1;
}
public class Dog extends Animal {
public static void main (String []args) {
Dog dog = new Dog();
}
}
I am aware of the super()
implicitly placed in the first line of the no-args constructor, so I know that the Animal
constructor will be executed and so the Animal
's instance variable will be set.
To this purpose I would like to understand if, once those variables have been initialized by the super constructor (Animal
), those instance variable will be kept there in the Animal object or copied to the subclass (Dog
).
In the first case an object Animal
will be instantiated implicitly by super();
and whenever the instance Dog
will need to access one of those variable it will be done accessing to the variables kept in the instance Animal
(created on background). Or second case, if the object Animal will be temporary created, all the instance variable (in Animal
) copied to the Dog
instance and then deleting the Animal
's instance temporary created.
I personally think that for example a Dog
object would be directly linked to an Animal
object which is directly connected to an object.
Is it in this way?
In inheritance, subclass acquires super class properties. An important point to note is, when a subclass object is created, a separate object of a superclass object will not be created. Only a subclass object is created that has superclass variables.
In summary - there is only one object and it is created and initialized in a number in stages.
creating instance means creating object of the class and by using that object access properties and methods of that class. but by using inheritance you can create another class from that (base) class which contains properties of base class as well as new properties of that class.
In inheritance, subclass acquires super class properties. An important point to note is, when subclass object is created, a separate object of super class object will not be created. Only a subclass object object is created that has super class variables.
There's only one object, which is immediately (right from the start) a Dog
instance. It has all the fields of Dog
(not that you have any here) and all the fields of Animal
.
Initially all the variables will be set to their default values (0, null etc). Then when you get to each class's constructor body (after calling the superclass constructor), the instance variable initializers are executed, then the constructor body for that class is executed.
There's no copying required, as there's only ever one object. So for example, if you were to write an Animal
constructor like this:
public Animal() {
System.out.println(this.getClass());
}
... it would print out Dog
, because the object is already a Dog
, even though its Dog
constructor won't have been executed yet.
Your Dog
extends Animal
, and the head
, legs
variables are not private, so you will access them from the Dog
instance.
In practice, the following happens:
Dog
instance, which is also an Animal
head
, and after the properties of Animal
as well)Dog
is called (it calls super()
)Animal
is calledAnd the result is an Object, which is a Dog
, but implicitly also an Animal
.
How a Dog
object looks like (let's forget about the Object
class for the sake of the example). Let's suppose Dog
has a public String name;
property as well.
Here is a memory map of a Dog instance:
Addr Type Name Defined in:
------------------------
| 0 | int | legs | Animal
| 1 | int | head | Animal
| 2 | String | name | Dog
------------------------
Animal
, you access the head
as a variable at address 1,Dog
, you access the name
as a variable at address 2,Dog
, you access the head
as a variable at address 1
The code that runs in the body of Animal
class can see only addresses 0-1
. The code that runs in the Dog
class can access addresses 0-2
. If a property would be private
to the superclass, then that address would be forbidden for the subclass.
This memory map makes is possible to downcast the objects very easily: the same memory mapping is used, only the treatment (the code and the visibility) is different.
I hope it clarifies a bit.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With