File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes instanceof prblem help me Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "instanceof prblem help me" Watch "instanceof prblem help me" New topic
Author

instanceof prblem help me

Rishi Yagnik
Ranch Hand

Joined: Jan 04, 2001
Posts: 84
hi
can somebody explain me the flow of programme?
Quest:Given the following class definations,which expressions identifies whether the object referred by obj was created by instantiating class B rather then class A,C and D?
class A{}
class Bextends A{}
class C extends B{}
class D extends A{}
select all valid answers:
1) obj instanceof B
2) obj instanceof A && !(obj instanceof C)
3) obj instanceof B && !(obj instanceof C)
4) !(obj instanceof c | | obj instanceof D)
5) !(obj instanceof A) && !(obj instanceof C) && !(objinstanceof D)
pls explain me the flow of programme?
Cherry Mathew
Ranch Hand

Joined: Dec 26, 2000
Posts: 159
The first checks whether it is object of B but will give true even if itz object of C
second is not correct bcoz it will give true for object of A
third checks for the case given in first thatz it checks whether object of C and returns fasle if it is.
Fourth is not correct bcoz it doesnt check it iz object of B.
Luv Cherry
Weigang Gu
Ranch Hand

Joined: Jan 16, 2001
Posts: 44
This is a question about inheritance. The rule to remember is that an instance object of subclass is an instanceof its superclass (and of course an instanceof itself). Then the question becomes very easy and straight forward.
Look at the hierarchy of all these classes ( right is superclass)
C---B----A
D--------A
Then it is obvious obj (an object of B) is instanceof B, A
and answer 1,2,3,4 are all valid
Quest:Given the following class definations,which expressions identifies whether the object referred by obj was created by instantiating class B rather then class A,C and D?
class A{}
class Bextends A{}
class C extends B{}
class D extends A{}
select all valid answers:
1) obj instanceof B
2) obj instanceof A && !(obj instanceof C)
3) obj instanceof B && !(obj instanceof C)
4) !(obj instanceof c | | obj instanceof D)
5) !(obj instanceof A) && !(obj instanceof C) && !(objinstanceof D)
pls explain me the flow of programme?
[/B]
Weigang Gu
Ranch Hand

Joined: Jan 16, 2001
Posts: 44
Sorry, I misunderstood the question.
It is the right way to think, but my answer is wrong. I thought
it asks which is true when obj is aninstanceof B
Son Le
Greenhorn

Joined: Jan 15, 2001
Posts: 21
Rishi,
We have the inheritance chain:
A <-- B <-- C <-- D
X instanceof Y: return true when X is an instance of Y or a subclass of Y
1) obj instanceof B: not correct since an instance of C or D might satify this condition
2) obj instanceof A && !(obj instanceof C): not correct since an instance of A might satify this condition
3) obj instanceof B && !(obj instanceof C): correct since this is an instance of B and not an instance of subclass of B
4) !(obj instanceof c | | obj instanceof D): not correct since
obj can be an A,B, or any other classes
5) !(obj instanceof A) && !(obj instanceof C) && !(objinstanceof D): can be an instance of any other classes
HungSon Le

-LHS
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
HungSon,
I do not believe from the original post, that D relates to B or C at all. Only to A.
B and D are sibling objects, both direct sub-classes of A. Only C is a next generation sub-class (sort of a grandchild of A, child of B, and a nephew of D)


"JavaRanch, where the deer and the Certified play" - David O'Meara
Son Le
Greenhorn

Joined: Jan 15, 2001
Posts: 21
Cindy,
You're correct. I apologize for not reading the question carefully =).
Here's my correction.
class A{}
class Bextends A{}
class C extends B{}
class D extends A{}

A <-- B <-- C
^
|
D

X instanceof Y: return true when X is an instance of Y or a subclass of Y
1) obj instanceof B: not correct since an instance of C might satify this condition
2) obj instanceof A && !(obj instanceof C): not correct since an instance of A might satify this condition
3) obj instanceof B && !(obj instanceof C): correct since this is an instance of B and not an instance of subclass of B
4) !(obj instanceof c | | obj instanceof D): not correct since
obj can be an A,B, or any other classes
5) !(obj instanceof A) && !(obj instanceof C) && !(objinstanceof D): can be an instance of any other classes
HungSon Le

[This message has been edited by Son Le (edited January 18, 2001).]
sharana sharana
Greenhorn

Joined: Mar 14, 2003
Posts: 24
hi its True that answer c proves Obj is instace of B and not instance of C , But it does not prove B is not an instance of A as well as asked in Question which expressions identifies whether the object referred by obj was created by instantiating class B rather then class A,C and D?
i think expression
(obj instanceof B) && ! ( obj instanceof A || obj instance of C ) can only prove that obj is an instance of B rather than Class A , C and D.
hence none of the answers provided are correct


sharana
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8829
    
    5
Rishi -
Son Le's second answer is correct !
If you run the following code
You'll get 4 rows and 5 columns of output.
Each row represents a type
Each column represents one of the tests
The correct test will be true for B(ear) and false for all other types
The third column is the only column to produce this result.
I also think it's easier to do these problems when the classes are more like real life, so I made them into real-life inheritences - if you do that you can probably just work it out in your head.


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: instanceof prblem help me