Re: How to check if object is an instanceof a class, but not a
sub-class ?
On Thu, 24 Jun 2010 12:45:02 +0000, Andreas Leitgeb wrote:
Eric Sosman <esosman@ieee-dot-org.invalid> wrote:
On 6/23/2010 5:27 PM, Daniel Pitts wrote:
[... using getClass() in equals() ...] Actually, I would use:
if (other == this) { return true; }
if (other == null || other.getClass() != getClass() { return false; }
Hmm. I usually write
if (other == null || other.getClass() != Thing.class)
Is there a reason to prefer one or the other?
I'm rather surprised to see .equals() being called an exceptional
usecase for exact class-check.
From previous discussions here and elsewhere, I had gathered, that
the "really correct" way to handle (non-trivial!) .equals() in a class
hierarchy would be to make all non-leaf-classes abstract.
Making all non-leaf classes abstract is an example of poor design.
public class Bird {
public boolean getCanFly() {
return true;
}
}
public class Penguin extends Bird {
public boolean getCanFly() {
return false;
}
}
Are you saying that class Bird ought not to be instantiable? Are you
saying I ought to add a class:
public class InstantiableBird extends Bird {} ?
which has no new semantic content, which overrides or specialises
nothing, and that's 'really correct'?
--
;; Semper in faecibus sumus, sole profundam variat