aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes operator instanceof confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "operator instanceof confusion" Watch "operator instanceof confusion" New topic
Author

operator instanceof confusion

neeta mathur
Greenhorn

Joined: Aug 05, 2004
Posts: 22
Given the following class definitions. which expressions identifies whether the object reffered to by obj was created by instantiating class B rather than classes A,C,D?
class A {}
class B extends A {}
class C extends B {}
class D extends A {}

A) obj instanceof B
B) obj instanceof A && !(obj instanceof C)
C) obj instanceof B && !(obj instanceof C)
D) !(obj instanceof C || obj instanceof D)
E) !(obj instanceof A) && !(obj instanceof C) && !(obj instanceof C)

The correct answer is C
According to me it should be E--becos it clearly test that the obj is NOT (!) instanceof A, C, D
can anyone pl explain me how??
also how can an obj be inctance of cbecos C is-a B but not vice versa
thanx in advance
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Draw yourself an inheritance diagram: A is the parent class at the top, with B and D "sibling" classes that both directly extend A. C is an extension of B.

Now consider that if an object is an instance of a particular class, then it is also an instance of every other class above it. Therefore, (obj instanceof A) will return true for an object instantiated from any of these 4 classes.

We want to know whether a particular object was instantiated from class B. In order for this to be true, (obj instanceof B) must be true. But given this hierarchy, what other types of objects will return true -- giving us a "false positive" that we need to screen out?

The only other possibility is an object instantiated from C, because C is the only class that inherits from B. Indeed, if obj was instantiated from A or D, then (obj instanceof B) will return false. Therefore, we want objects that are instances of B and not instances of C.

(obj instanceof B) && !(obj instanceof C)
[ September 08, 2004: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
neeta mathur
Greenhorn

Joined: Aug 05, 2004
Posts: 22
Hi marc,
after your seeing your message i'm able to understand that we r testing that the obj is not an instance of C and is an instance of B.
But as per question it should NOT be an instance of A, C, D either.
so we must be sure that it not an instance of A also.
How can we test that--if obj is an instance of B it will be an instance of A also right....
the option E tests all the three...
Tom Tolman
Ranch Hand

Joined: Sep 02, 2004
Posts: 83
Actually B IS AN instanceof A.

So you need to find the class which is:
an instanceof A
an instanceof B
not an instance of C
not an instance of D

It happens that anything that derives from A is also A, so you don't need to test both of the top two, just the second will suffice. Also, anything which is not an instanceof will not become one further down on the inheritence tree, so D doesn't need to be tested either (although could be)
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
Hi Neeta,

obj instanceof T will return true if obj is an instance of T or subclass of T.
obj instanceof A will return true for obj: A, B, C or D (B, C and D are subclasses of A)
obj instanceof B will return true for obj: B or C (C is subclass of B)
obj instanceof C will return true for obj: C (no subclass for C)
obj instanceof D will return true for obj: D (no subclass for D)

C) obj instanceof B && !(obj instanceof C
The first part of the expression "obj instanceof B" will be tested first. It will return true if obj is an instance of B or C (C is a subclass of B)

Let say the first part returns true. The second part of the expression "!(obj instanceof C)" will be tested next. It will return true if obj is not an instance of C or subclass of C). Hence, in order to have both expressions to be true, obj must be B.

You can write some code and test it.
For example:



E) !(obj instanceof A) && !(obj instanceof C) && !(obj instanceof C)
Let's look at the first expression "!(obj instanceof A)". It'll return true if obj is not an instance of A or subclass of A. Since B, C and D are subclasses of A, it will return false if obj is A, B, C or D.

Joyce
[ September 08, 2004: Message edited by: Joyce Lee ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Instead of A, B, C, and D, let's consider some examples we can envision: Animal, Bird, Crow, and Dog. Animal is the base class. Bird extends Animal. Crow extends Bird. Dog extends Animal.

Now, if we create a Crow, then we will obviously have an instance of Crow. But because of inheritance, Crow is also an instance of Bird, and Crow is also an instance of Animal.

On the other hand, if we create a Bird, then we will have an instance of Bird. And Bird is also an instance of Animal. But this Bird is not an instance of Crow, because Crow is a "more specific" type of Animal -- lower than Bird on the inheritance tree.

And if we create a Dog, then we will have an instance of Dog, which is also an instance of Animal; but this is not an instance or Bird or Crow.

So, back to the question: We want to determine whether the object referred to by obj was created by instantiating Bird rather than Animal, Crow, or Dog. It's important to note that this not asking whether obj is simply an instance of Bird, rather than Animal, Crow, or Dog. It's asking whether obj was created by instantiating Bird.

Now, if obj was created by instantiating Bird, then it is an instance of Bird, so we expect (obj instanceof Bird) to return true. But a Bird is also an Animal, so testing (obj instanceof Animal) won't tell us anything new.

And if (obj instanceof Bird) returns true, then we also know that obj was not created by instantiating Dog, because a Dog is not a Bird, so (obj instanceof Dog) is not necessary either.

But what if obj was created by instantiating Crow? Then (obj instanceof Bird) will still return true, because a Crow is a Bird. But this would be a false positive. So after we've determined that (obj instanceof Bird) is true, we must eliminate the possibility that obj is a Crow. Therefore, we want (obj instanceof Crow) to be false.

Therefore, (obj instanceof Bird) && !(obj instanceof Crow).
neeta mathur
Greenhorn

Joined: Aug 05, 2004
Posts: 22
i thank all u guys for all ur support
now the topic seems clear
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: operator instanceof confusion