From: Eric Sosman on
On 6/23/2010 1:06 PM, Mike Schilling wrote:
> "laredotornado" <laredotornado(a)zipmail.com> wrote
>>
>> My question is, how can I tell if an object is a direct instantiation
>> of a class A and not a sub-class of the class A?
>
> Having to know this is probably an indication of poor design.[...]

It's often necessary in equals() implementations, so at least
some of the design failings are Java's own.

--
Eric Sosman
esosman(a)ieee-dot-org.invalid
From: Daniel Pitts on
On 6/23/2010 10:06 AM, Mike Schilling wrote:
>
>
> "laredotornado" <laredotornado(a)zipmail.com> wrote in message
> news:b9605e9c-42f7-4ba4-a1e9-ee65ba0c539a(a)x21g2000yqa.googlegroups.com...
>> Hi,
>>
>> I'm using Java 1.6. If I have a object of class "A", I can use the
>> "instanceof" operator to confirm that the object is of this class.
>> But if I have an object of class B (extends A), instanceof will also
>> return true if use "obj instanceof A".
>>
>> My question is, how can I tell if an object is a direct instantiation
>> of a class A and not a sub-class of the class A?
>
> Having to know this is probably an indication of poor design. But if you
> really need to, try
>
> if (a.getClass() == A.class)
There is one use-case I can think of, and that is properly handling the
"equals" contract.

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>