• Post Reply Bookmark Topic Watch Topic
  • New Topic

Working with inheritance  RSS feed

 
Urs Waefler
Ranch Hand
Posts: 192
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do not fully understand the following code:
The output is:
0
4

The method print() is overridden in class B. Due to polymorphism, the method to be executed is selected depending on the class of the actual object. Here, when an object of class B is created, first A's constructor is called, which in turn calls print(). Now, since the class of actual object is B, B's print() is selected. At this point of time, variable i has not been initialized.

At this point I have a question. To me it seems that the variable i looks at this point as local variable; local variables do not have a default value.

The object B exists, so the variable i exists too; but it has not assigned the value 4 yet. Thus it has the default value, which is 0. Nevertheless I am not sure what happens here exactly. I would kindly ask for a clarification.
 
s ravi chandran
Ranch Hand
Posts: 579
6
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seeing the code I have few pointers.

Field at line 6 is not a local variable, but an instance variable.

Next, when you override the parent method, the child method will be called. ( Runtime Polymorphism).

From what I remember, before child object is initialized, parent object would be initialized.

At this point, print() from A should be called. But because that method is overridden, it should call the overridden method. And because class A doesnt have any value for variable i, it will default to 0 and print.
 
Urs Waefler
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And because class A doesnt have any value for variable i, it will default to 0 and print.

This I do not understand. It is clear that A does not have a variable i, thus maybe you might expext a compiler error; A does not have a variable i.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Urs Waefler wrote:thus maybe you might expext a compiler error;

You identified already in your first post that it outputs something. So there is no point in talking about compile time errors here, but rather how you end up with this output.
 
Paul Clapham
Sheriff
Posts: 22823
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Urs Waefler wrote:The object B exists, so the variable i exists too; but it has not assigned the value 4 yet. Thus it has the default value, which is 0. Nevertheless I am not sure what happens here exactly. I would kindly ask for a clarification.


Yes, that's correct. When you create a B object, you run its constructor. And since B extends A, the first thing which happens there is that A's constructor gets run.

And yes, at this point B's instance variables have not yet been initialized. That happens after B's constructor is complete. BUT... A's constructor calls print(). And since the object doing all of this stuff is a B object, and B's print() method overrides the one in A, that means that B's print() method is the one which gets run. And since B's instance variables haven't been initialized, in particular the variable i is still zero. Which is what you see when the print() method writes it to the console.

(Which is why you should avoid calling instance methods from a constructor if they could conceivably been overridden. This is an extremely annoying bug to track down -- as I can tell you from personal experience -- and it's doubly annoying because fixing it is hard to do.)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!