Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

instanceof  RSS feed

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!!
Given the following class definitions, the expression
(obj instanceof A) && !(obj instanceof C) && !(obj instanceof D)

correctly identifies whether the object referred to by obj was created by instantiating class B rather than classes A, C and D?
class A {}
class B extends A {}
class C extends B {}
class D extends C {}

The answer is false but I think that the answer is true because:
(obj instanceof A) ------------------> true
!(obj instanceof C) ------------------> true
!(obj instanceof D) ------------------> true
true && true && true -------> true.
Can someone help me?
Thank you in advance.

 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jordi,
No the answer is false because this expression allows to say that obj may be an object of class A or B and not only B. Though it does not allow the object to be of class C or D (because of the last two expressions).
If you are not sure with such expressions just try the following.
You have a hierarchy of 4 classes. Just create one instance of any of them and run the expression through your mind...
1: (obj instanceof A)
2: !(obj instanceof C)
3: !(obj instanceof D)
A a = new A();
B b = new B();
C c = new C();
D d = new D();
Case 1: obj = a;
the first subexpression yields true
the second too and the third too, which means that the expression is valid for instances of class A. Here already the answer is false.

Case 2: obj = b;
the first expression is true, as well as the second and the third. Thus the answer to the question is true, the expression is valid for instance of class B.
Case 3: obj = c;
the first expression is true but the second is false. Thus the whole expression yields false for instances of class C
Case 4: obj = d;
The first expression returns true, the second as well but the third yields false.
To summarize you can see that the expression is not only valid for instances of class B but also for instances of class A, thus the answer is FALSE.
Hope this helps
Val
[This message has been edited by Valentin Crettaz (edited September 15, 2001).]
[This message has been edited by Valentin Crettaz (edited September 15, 2001).]
 
Cameron Park
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you run it and find out?
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with you Cameron
I am not talking about Jordi in particular, but many people seem not have given a try to their questions.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are really kind, Jose
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I didn't want to sound rude to anybody.

I can not understand why people post something like what is this program going to print?
I mean obviously that person has a doubt, but why not placing the output in the post. Doing so will give us a clue to answer and sometimes it will stop us from having to run the code.
Sorry again :-(
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Besides that, many people say this code will give an error. Why not pasting the error in the post? That would help.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are absolutely right Jose
Val
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!