Thursday, 22 August 2013

Inhertance and Object Creation

Inhertance and Object Creation

This is my first question here and I hope I am up to standards of
Stackoverflow. Well, here it is.
Base Class:
public class Inheritance {
int i;
Inheritance() {
System.out.println("I am in base class" + i);
}
}
Derived Class:
public class TestInheritance extends Inheritance {
TestInheritance() {
System.out.println("I am in derived class");
}
public static void main(String[] args) {
TestInheritance obj = new TestInheritance();
}
}
This is what I have in mind regarding what is going on above.
when I create an object of the derived class by default super() is called
and the constructor of the base class is called and it initializes the
variable i. Now, my question is does the constructor in this case only
initializes the variable i and doesn't create a concrete object of class.
From what I have read so far there is only one object created i.e of the
derived class which has i variable in it. But from the time the
constructor of base class is called and the point in time where
constructor of the derived class is called how(where) is i stored in
memory. And what would be case in which base class is an abstract one. I
would really appreciate if I can know what happens in memory at different
points of time.
If I have said something which is fundamentally incorrect please let me
know. I really want to know how this thing works.

No comments:

Post a Comment