I am trying to check if my array A has an instance of object B at the current position, since it is an array that can contain two types of objects (B and C). I do not want to use instanceof, so I'm wondering what other alternatives I have?

if(A[i] instanceof B)
  • 3
    "I do not want to use instanceof ... " Why?
    – yannis
    Commented Feb 11, 2017 at 17:48
  • More context is needed to offer an alternative solution to your problem - you can't just arbitrarily replace instanceof with some other mechanism because the only mechanism equivalent to instanceof is instanceof.
    – Ant P
    Commented Feb 11, 2017 at 17:49
  • 6
    This might be an XY problem. What are you going to do with that object once you identify what type it is?
    – user22815
    Commented Feb 11, 2017 at 17:50
  • Possible duplicate of Replacement for instanceof Java? Commented Feb 11, 2017 at 20:14
  • 1
    Possible duplicate of Replacement for instanceof Java?
    – gnat
    Commented Jan 9, 2019 at 11:21

3 Answers 3


The primary alternative to using instanceof is polymorphism. Rather then ask which type of object you have at the current position you tell the object, whatever it is, to do what you want done. If both objects know how to do that then this works fine, even if they do it differently. The principle at work here is called Tell, don't ask.

If the language you're using doesn't have duck typing you will need types B and C to both explicitly implement the same interface or abstract type that has the method you want to call. This would mean that your array is homogeneous. It's when it's heterogeneous that you're left with no idea if you can call the method safely until you ask.

If you're working in a code base that uses an object oriented paradigm people will get very irritated to see use of instanceof. Not so much if you're in a procedural paradigm.

That isn't to say any one paradigm is always right. But when in Rome...

An oft used example:

At the pet show you array your lovely pets before your audience. To show how well trained they are you say to each, "Speak!"

Your duck quacks.

Your dog barks.

Your cat looks at you funny and then ignores you.

Your pet rock rips a hole in the fabric of reality because the designers of your reality forgot to give rocks any kind of speaking function.

The rock is the one that would put your array in the heterogeneous category. Keep the rocks out of your array and you can avoid instanceof.

  • 2
    Or have rocks do nothing when ordered to speak. This is the idea behind the null object pattern.
    – user22815
    Commented Feb 11, 2017 at 23:35
  • @Snowman correct. And that's pretty much what the cat represents. The null object pattern is actually my favorite pattern. My most often used implementation of it, the empty string: "" Commented Feb 12, 2017 at 7:16

"I am trying to check if my array A has an instance of object B at the current position"

If that is what you want, then using instanceof is the cleanest way to achieve it. You could somehow observe some behaviour of the instance and deduce which class it belongs to, but that is a hack.

Your question should really be "How to I avoid having to know whether A has an instance of object B in the current position". That's what inheritance and virtual methods are there for. If instances of B and C behave differently, but you can make a method call that will work correctly both for instances of B and C, then you don't need to know what class B is an instance of, and your problem disappears.


Polymorphism is the main answer -- just instruct each object in the array to do whatever needs to be done and implement that method in A and B to do whatever you would have done after determining their type.

But if you really want to know the type without immediately executing something, you might as well add a method to them to communicate their type, such as isA() and isB(). You could even put them in an interface, make them both a default method and let them return false. That way, A only needs to override isA() and B only needs to override isB().

Keep in mind that (as stated at the top) usually a nice solution involving polymorphism is possible and preferable. But there are cases in data structure handling where the union-style (isA()/isB()) makes sense. It's essentially a workaround for languages that don't have pattern matching (such as Java).

  • Why would isA() and isB() in an interface be better than simply asking instanceof A or instanceof B?
    – Andres F.
    Commented Feb 12, 2017 at 0:01
  • I'm not sure it would be better, but it's very different. For example, if you ever introduce a C you can let it be treated as an A or B without having to do subclassing.
    – Deckard
    Commented Feb 12, 2017 at 6:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.