Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inheritance: object creation

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?

like image 826
Rollerball Avatar asked Mar 02 '13 17:03

Rollerball


People also ask

Can we create object for inheritance?

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.

How many objects are created in inheritance?

In summary - there is only one object and it is created and initialized in a number in stages.

What is the difference between inheritance and object creation?

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.

Can we create object of inheritance in Java?

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.


2 Answers

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.

like image 183
Jon Skeet Avatar answered Sep 30 '22 16:09

Jon Skeet


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:

  • You create a Dog instance, which is also an Animal
  • all the object properties are created and initialized (including head, and after the properties of Animal as well)
  • The implicit constructor of Dog is called (it calls super())
  • The implicit constructor of Animal is called

And 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
------------------------
  • If you have an Animal, you access the head as a variable at address 1,
  • If you have a Dog, you access the name as a variable at address 2,
  • If you have an 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.

like image 26
gaborsch Avatar answered Sep 30 '22 18:09

gaborsch