posted 22 years ago
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).]