Page 1 of 1

About Question enthuware.ocajp.i.v7.2.1138 :

Posted: Sun Mar 23, 2014 3:16 pm
by fasty23
calling System.out.print cause the toString method called or making a new object from the class SubClass? or both?

Re: About Question enthuware.ocajp.i.v7.2.1138 :

Posted: Sun Mar 23, 2014 8:27 pm
by admin
The line of code is System.out.println( new SubClass() ); so first a new SubClass object will be created and that object will be passed to the println method. The method will then cause the toString method on that object to be invoked.

Re: About Question enthuware.ocajp.i.v7.2.1138 :

Posted: Wed Jan 06, 2016 5:58 am
by AristPhil
Sirs, the exact answer to this questions should be 43 (i.e. option 1). The (console) output is 43 - not "43".
So, why is option 1 (i.e. 43) wrong?

I. Hypothesis:
I guess that you do not differentiate between the asked output and your supposed return value of System.out.println( new SubClass() ) in SubClass's main method. Although the return value is a String, the program prints 43 - not "43".

II. Explanation:
You explain your desired answer (i.e. option 5 - None of the above.) like this:
So the final value is "43".
Nevertheless, the question asks for the print out - not the final value:
What will the following program print when run?
III. Suggestion:
If my reasoning is correct, wouldn't it be helpful either to ask for the return value (w/o return type) or to change the correct answer to option 1?

AristPhil

Re: About Question enthuware.ocajp.i.v7.2.1138 :

Posted: Wed Jan 06, 2016 8:59 am
by admin
The question asks, "What will the following program print when run?" and the correct option i.e. option 1, says 43. The option does not say "43".

Yes, the explanation says "43" because it is explaining about the final value of the statement super.toString()+"3";
Not sure what you see wrong with that.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1138 :

Posted: Wed Jan 06, 2016 10:15 am
by AristPhil
Sorry, my fault: I misinterpreted the tool's evaluation view! Initially, I answered the question wrong. During my review the tool highlighted the correct option in green, i.e. option 1 saying 43, but I thought this was my (wrong) answer. It was the correct answer instead which I had selected during my review and which I should have selected during the exam as well. Sorry for the confusion, Paul! My text above says nothing else than your correct answer and explanation.

Re: About Question enthuware.ocajp.i.v7.2.1138 :

Posted: Wed Oct 18, 2017 11:22 pm
by CagriKahraman
Hi Admin,

I just passed the OCA exam, and I got at least 3 questions about toString overriding and I have a suggestion about this questions. I wouldn't understand the reason how come when you print the object as (new Something()), it prints out the objects with toString method until I solve this toString question on Last day test. It's explanation help a lot to understands how toString methods works, So you would add this explanation to this question.
Here is your explanation of enthuware.ocajp.i.v8.2.1308 :

"The toString method for class Object returns a String consisting of the name of the class of which the object is an instance, the at-sign character '@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of: getClass().getName() + '@' + Integer.toHexString(hashCode())"

My suggestion is adding some more detail about how the implementation of toString method in the class and when you print out the object, how toString method effects it.

Thanks a lot, we are lucky to have Enthuware!!!

Re: About Question enthuware.ocajp.i.v7.2.1138 :

Posted: Wed Oct 18, 2017 11:42 pm
by admin
Thank you for the feedback, Cagri. We will incorporate it into the questions asap.
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1138 :

Posted: Sat May 26, 2018 5:42 am
by sadhiunni
isn't Super a keyword?
so can we give Super as a class name?

Re: About Question enthuware.ocajp.i.v7.2.1138 :

Posted: Mon May 28, 2018 9:18 pm
by admin
Super isn't a keyword. super is.

Re: About Question enthuware.ocajp.i.v7.2.1138 :

Posted: Fri Jul 30, 2021 10:00 am
by enthunoob
I thought the toString method of the Super class is overridden and therefor 'replaced'. However, appearently it is kept alive as SubClass's toString method refers to it. Is that correct?
And, as Object is the super class of Super class and also had the toString method, is it possible to call Object's toString method and still print SubClass@hexcode from the main method (in other words, is it possible to invoke the Object's toString method or is that replaced because it's not referred to from the class Super)?

Re: About Question enthuware.ocajp.i.v7.2.1138 :

Posted: Fri Jul 30, 2021 12:44 pm
by admin
>I thought the toString method of the Super class is overridden and therefor 'replaced'.

yes, 'replaced' in the subclass. So, SubClass has its own version of toString. Super class still has its own version, which can be invoked by SubClass's toString using super.toString() syntax.

You might have misunderstood the "replace" thing. If a subclass overrides a method then that methods gets replaced in the subclass. i.e. the subclass doesn't inherit super class's version of the method anymore (because it has its own version of that method now). The method doesn't get replaced in the super class. Java provides a special syntax (super dot) using which a subclass method can invoke the super class's version of the overridden method. But this syntax allows you to go only one level up. You can't do super.super.toString().

So, you cannot invoke Object's version of toString from SubClass's version of toString, in this case.

Re: About Question enthuware.ocajp.i.v7.2.1138 :

Posted: Sat Sep 04, 2021 8:20 pm
by qazwsx922
I thought System.out.println( new SubClass() ); this line should be System.out.println(new SubClass().toString()); to print 43

how new SubClass() calls toString method?

Re: About Question enthuware.ocajp.i.v7.2.1138 :

Posted: Sat Sep 04, 2021 10:27 pm
by admin
new SubClass() does NOT call toString method. The println method calls toString on the object that is passed to it.

Please see this for exact details: https://docs.oracle.com/en/java/javase/ ... ng.Object)