Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

instanceof prblem help me

 
Rishi Yagnik
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Son Le
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bert Bates
author
Sheriff
Posts: 8900
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic