Forums Register Login

Java inheritance question

+Pie Number of slices to send: Send
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!
2
+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
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).
+Pie Number of slices to send: Send
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).

1
+Pie Number of slices to send: Send
 

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
(1 cow) 1
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
By 12.5 I presume you mean JLS §12.5
+Pie Number of slices to send: Send
yes
+Pie Number of slices to send: Send
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.
And then the flying monkeys attacked. My only defense was this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1229 times.
Similar Threads
Initialization
Overidden method from JQ5Plus exam
static, instance, constructor call order
some simple Q....
Dan exam doubt 17
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 02:00:00.