Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

subclasses and references

 
Ranch Hand
Posts: 145
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A reference to an object may be cast to a subclass of the object with an explicit cast.

is this statement true or false ?

because as an example of the above statement,  i could have an option something like :
Parent p=new Parent();
Child c  = (Child)p;

but while this may compile without any error, it will throw a classcastexception at runtime.
so is the answer true or false for that statement ?
 
blossom belle
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here assuming Child extends Parent
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

shambhavi sham wrote:so is the answer true or false for that statement ?


That's hard to tell without any context. For me that's a false statement! And you have illustrated the reason why
 
blossom belle
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
actually this was a question in my practice for oca exam . so if i encounter a similar question on the oca , what do i do ?
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you share the actual question (and again QuoteYourSources) because often the context of a question might matter to select the correct answer(s).
 
blossom belle
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is the question.

is this statement correct about polymorphism ?



this is one of the options/choices in the answers, to choose from.

A reference to an object may be cast to a subclass of the object with an explicit cast.


so there's no context as such.
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From that context I would say the statement is correct!
 
blossom belle
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but before you said its a false statement right
then now how do you say its correct ?    
 
author
Posts: 23878
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about this?



This should compile and run fine.

Henry
 
blossom belle
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the statement is not correct for a reference to any object right ?
the statement is correct only if it is :
A reference to a subclass object may be cast to a subclass of the object with an explicit cast.
 
blossom belle
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya henry thats absolutely fine !

but keeping this statement in view :
A reference to an object may be cast to a subclass of the object with an explicit cast.

it could be a reference to any object . so won't this statement about polymorphism be incorrect ? since according to the statement, they've just given reference to an object . so it doesn't apply for all cases right
 
blossom belle
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it would be correct for the case posted by henry
 
blossom belle
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can somebody please help me on the answer and how to reason out such questions on the exam ?
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

shambhavi sham wrote:but before you said its a false statement right
then now how do you say its correct ?    


I am totally aware of what I've said And that's exactly the reason why I asked for the context of the question (which you should have provided in your initial post). You should definitely read this topic which explains in great detail how the "context of a question" can have an influence on the correct answers.
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

shambhavi sham wrote:can somebody please help me on the answer and how to reason out such questions on the exam ?


Each exam question will mention how many correct answers you must select.
 
Enthuware Software Support
Posts: 4450
41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

shambhavi sham wrote:can somebody please help me on the answer and how to reason out such questions on the exam ?


I would suggest you not to worry too much about the answer because the statement is poorly worded. It doesn't make it clear what is meant by "may be cast" - successful compilation or successful execution (which includes successful compilation).

Without any explicit mentioning of compilation or execution, it should be assumed that "may be cast" implies successful execution (i.e. no exception thrown). The given statement is, therefore, false.

In the exam, the context will be clear and you can figure out the answer without any confusion.
 
blossom belle
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot paul !
 
blossom belle
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Roel De Nijs wrote:

shambhavi sham wrote:but before you said its a false statement right
then now how do you say its correct ?    


I am totally aware of what I've said And that's exactly the reason why I asked for the context of the question (which you should have provided in your initial post). You should definitely read this topic which explains in great detail how the "context of a question" can have an influence on the correct answers.




okay i understand that context plays an important role ! i just want you to explain how do you arrive at this answer in this question and what context did you use ?
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

shambhavi sham wrote:i just want you to explain how do you arrive at this answer in this question and what context did you use ?


Your first post was nothing more than the statement "A reference to an object may be cast to a subclass of the object with an explicit cast." with the question true or false? And then the answer is false, because that cast might fail at runtime (as you have illustrated yourself in your first post).

Then I asked what the context of the question was. And then it appears that the question was "what's true about polymorphism" and one of the possible options/answers was "A reference to an object may be cast to a subclass of the object with an explicit cast.". And then this option/answer is correct, because it's simply one of the things you can do (and from the context of the question it doesn't really matter that you might get a ClassCastException at runtime).
 
blossom belle
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmmm..... okay thanks roel ! but its a little confusing on how to interpret these questions.
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On the actual exam the question, the answers and the context will be clear. So if you have a solid and good understanding of all exam topics, you should be able to select the correct answers without any doubts and/or confusion. Also keep in mind that each question will mention how many correct answers you need to select. And that's really valuable information, because even if you are clueless about that question (topic), you have a (slight) chance to answer that question correctly.
 
blossom belle
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh alright , okay thanks a lot for the help roel !!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic