Preparing SCJP 1.5
Preparing SCJP 1.5
SCJP 5.0<br />SCWCD 1.4
Originally posted by Naseem Khan:
Thanks everyone. I got this now.
Naseem
The Best way to predict your future is to create it - Every great individual common man
"The return type is incompatible with CovariantTest.getObject()".
The Best way to predict your future is to create it - Every great individual common man
"The return type is incompatible with CovariantTest.getObject()".
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
But object c is type CovariantTest. Therefore, in the place of c.getObject(), the return type is considered as type A. 6 is printed.
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Originally posted by ram gaurav:
Above i think that you are breaking the overriding policy straight forward.
class SubCovariantTest extends CovariantTest
in this class you override , getObject() , and you just change the return type of the methord , which is just compile time error , there is no question of executing this code.
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Originally posted by Barry Gaunt:
No. In Java 5.0 you can do this, but not in Java 1.4 or previous versions.
The Best way to predict your future is to create it - Every great individual common man
You can't wake a person who is <b><i>pretending</i></b> to be asleep.<br />Like what <b>"it"</b> does not like - <i> Gurdjieff </i>
Originally posted by Ankur Sharma:
That means I was correct at my point, that this is a compile time exception But why it was running and giving me wrong output which was not desirable...
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Originally posted by Arun Maalik:
Sir your program written there is completly wrong. It will create a compile time error that you are not overwriding the method.
Arun
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
I use "instanceof" test and it IS an object of type B is returned.
so c.getObject definitetly instantiates Object B (Which in turn instantiates A as it is a superclass)
SCJP 5
Warm Regards<br />Ajit Amitav Das<br />SCJP 1.5
A = HARDWORK B = LUCK/FATE If C=(A+B) then C=SUCCESSFUL IN LIFE else C=FAILURE IN LIFE
SCJP 1.4
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
c.getObject().x
Pinkal Patel<br />SCJP 1.5<br />Preparing for SCWCD
Originally posted by Naseem Khan:
You will get compilation time error at line 2...
It means what compiler sees c.getObject() as of type A.
Naseem[/QB]
SCJP 5
Originally posted by nitin pokhriyal:
hi naseem,
can you clarify your answer a bit more? at runtime getObject() of subCovarient will be called. am i right? if yes then how can it return object of A?
Regards,
nitin
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Don't get me started about those stupid light bulbs. |