• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java inheritance question

 
Denis Delic
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm learning for OCJA, came across this question:


And the answer:
Note that 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 (because we are still
initializing A at this point),so its default value i.e. 0 is printed.
This happens because the method print() is non-private, hence polymorphic.


I need help understanding how is B's print() available in A's constructor? First line of class B constructor is super() so nothing of A (expect static) should exist at this point?
Thanks!
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Denis,
Welcome to CodeRanch!

This is tricky. Methods always exist. it's instance variables that don't exist until after the constructor is run. This is why 0 gets printed out. The method is available, but not the instance variable it refers to.

As you might imagine, it is poor practice to call overridden methods in a constructor. Which won't stop it from coming up on the exam!
 
Denis Delic
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I never read that much detail on how constructor's works so I assumed that nothing in class B is available until super() finishes. It's confusing that at some point instance methods exist and instance variables do not (are not initialized).
 
Knute Snortum
Bartender
Pie
Posts: 2912
62
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch! Thanks for using code tags.

You can access B's methods from B's constructor, so it should be possible to access B's methods from A's constructor. I believe that in the constructor the object has been allocated but not initialized (except for statics).

 
Winston Gutkowski
Bartender
Pie
Posts: 10571
64
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Denis Delic wrote:Thanks, I never read that much detail on how constructor's works so I assumed that nothing in class B is available until super() finishes. It's confusing that at some point instance methods exist and instance variables do not (are not initialized).

Oddly enough, this is one place where knowing C++ helps, because in that language a superclass truly is "part of" a subclass (it may well be in Java too; they just don't tell you ).

The main thing to remember is that constructors work from the top down, so while, as Jeanne said, methods are always there, the information they need may well not be - or, as in this case, it may not yet be initialized.

HIH

Winston
 
Dave Tolls
Ranch Hand
Posts: 2112
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JLS is handy for this sort of thing.
Section 12.5.
"...all the instance variables in the new object, including those declared in superclasses, are initialized to their default values (§4.12.5)."
This is before the constructor is executed, so all variables have a value associated with them.

Those defaults are defined here.
 
Denis Delic
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello again, reading SCP for Java 6 Study Guide by Kathy Sierra and Bert Bates, in chapter 2, page 134 they say:

You cannot make a call to an instance method, or access an instance variable, until after the super constructor runs.


Maybe it would better to say after constructor starts?
 
Campbell Ritchie
Sheriff
Posts: 51459
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No longer a nice simple “beginning” question.

You will have to go through the official oracle (small O): the Java Language Specification (=JLS). I think you probably want §12.4. It tells you exactly which order you are executing the parts. It is the official specification and anybody who produces a program called Java must conform to it exectly. So it gives the definitive answer, whereas books often give a simplified answer. But you can usually understand the books

You should also edit that code. It is all squashed together without proper indentation. Copy it again with indents and spare lines before all the methods. Study guides often print code like that in the hope that the incorrect indentation will confuse you into giving the wrong answer. Also identify the constructor in class B. Can't find one? It is there, even if you can't see it. Back to the JLS. Now you see it gives an example of a default constructor. So you add the following constructor:-Now you have got the class the way you want it, you have some chance of understanding the question.
The methods are instance methods so you call the B version of the print method in both cases. You will have to read the JLS to confirm that the i field is uninitialised and has the default value of 0 until after the super(); call completes. The round method will round 0.5 up so Math.round(3.5) gives 4 and Math.round(-3.5) should give -3. I see Math#round(float) returns an int.
 
Denis Delic
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's JLS chapter 12.5:



Here, a new instance of ColoredPoint is created. First, space is allocated for the new ColoredPoint, to hold the fields x, y, and color. All these fields are then initialized to their default values (in this case, 0 for each field). Next, the ColoredPoint constructor with no arguments is first invoked. Since ColoredPoint declares no constructors, a default constructor of the following form is implicitly declared:



Still, feels like its too much for OCA. Thanks everyone.
 
Campbell Ritchie
Sheriff
Posts: 51459
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By 12.5 I presume you mean JLS §12.5
 
Denis Delic
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes
 
Campbell Ritchie
Sheriff
Posts: 51459
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have never sat such an exam; I don't know about the difficulty. I shall try duplicating this thread in the exam forum; maybe somebody will know there.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic