So I’m fairly new to programming in the real world (outside of academic projects) and have come across lots of posts saying that using instanceof
is a bad thing to use to determine what class a specific object is.
My situation is that I have three classes, a base product class, one that extends off that and another that extends off that. These are all stored in the same table in a database and I have some code that needs to use the methods on each to pull data off them.
What is the best practice to get around this way of doing it? I have read some things about polymorphism but I can’t find any examples that fix the issue I have. They all usually override a method which for me won’t work as I need to pull different things from the different objects.
Is there a better way to do this or am I stuck with using instanceof
or some sort of reflection to get the fields specific to the objects?
2
The reason instanceof
is discouraged is that it’s not OOP.
There should be no reason for the caller/user of an object to know which concrete class it is an instance of beyond which type the variable it is declared as.
If you need different behavior in subclasses add a method and implement them differently.
4
instanceof
isn’t necessarily a bad thing, however it is something that one should look at.
An example of where it works correctly is in a place where one gets a collection of the base type and you only want the ones of a subtype. Getting the network addresses from NetworkInterface.getNetworkInterfaces()
returns back NetworkInterface objects which have a collection of InetAddress objects, some of which are Inet4Address and some are Inet6Address. If one wants to filter the collection for Inet4Address objects, it is necessary to use instanceof.
In the situation that is being described in the original post, there is a Base class, something that extends that base class and something that extends the extended class. While not completely informative, this seems to have the underpinnings of some less than ideal design.
When you are returning a base class unless there is good reason for it being designed that way (backwards compatibility between specifications of an earlier version) you shouldn’t be trying to peek at the underlying types. If you are returned a Set, you know you are getting a set. It allows the developer to later change his or her mind to return a more specific type (SortedSet) or change the underlying type (HashSet to TreeSet) without breaking anything.
Reconsider your design of how the objects are structured and parented to see if one can make an better class model that is doesn’t require a distinction of types.
2
You can use getClass() method.
Are you sure you need three different classes? Maybe one class with a switch inside will serve better?
13
Typically when I find myself wanting to know the type of something it means I implemented my object structure wrong. Most of these times it comes down to violating LSP.
There are times however where I wish I’d have a way to do dynamic dispatch and save a ton of boiler plate code and futureproof my object structure. C# provides the dynamic keyword in the newer installments of the framework but as far as I know Java still does not have something similar.
That said, instanceof is generally better than comparing classes as it will support inheritence properly. You can also use methods like isAssignableFrom and others from the reflexion API. If you want to implement something like dynamic dispatch it can be done via the reflection API but beware, it will be slow. Use with caution, ideally you should fix the object structure and desing of your app if you can.
Hope this helps
0