This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes instanceof Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "instanceof" Watch "instanceof" New topic
Author

instanceof

K Anshul
Ranch Hand

Joined: May 19, 2004
Posts: 71


But C option states that obj is an instance of B and obj is not an instance of D.
What about obj being an instance of A.
Even in that case obj instanceof B will return true. But the question mentions that obj must have been created by instatiating B and not A,C,D.
But with C option we can't be sure if obj was created from A or B.

Edited by Corey McGlone: Removed lengthy line of CODE to preserve formatting.
[ June 23, 2004: Message edited by: Corey McGlone ]
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
and? what's the problem?


42
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729


Prints false


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
And if you try to create obj as a B from an A, like this:

B obj = (B)new A();

then that statement itself causes a ClassCastException at runtime.
[ June 23, 2004: Message edited by: Barry Gaunt ]
K Anshul
Ranch Hand

Joined: May 19, 2004
Posts: 71
Originally posted by Barry Gaunt:


Prints false


But this prints true

K Anshul
Ranch Hand

Joined: May 19, 2004
Posts: 71
From what i could understand from this question it states that object was created by instantiating B.
and then it says how will u find out that object was created by instantiating B and not A, C, D.
Plz correct me if i am wrong.
[ June 23, 2004: Message edited by: Anshul Kayastha ]
 
Don't get me started about those stupid light bulbs.
 
subject: instanceof
 
Similar Threads
PGJC mock exam question
instanceof operator
instanceof prblem help me
How to check the class of an Object given its reference?
Doubt in instanceof operator