Replacement for instanceof Java?

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật