• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Dynamic Discovery of Class Type?  RSS feed

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've got a situation where I need to be able to dynamically discover the actual instantiated type of a Class.

For example, say I have the following classes:

Animal
Dog extends Animal
Cat extends Animal

I instantiate a Dog as follows: Animal dog = new Dog();

From within ANIMAL, I want to be able to tell what type of "Animal" object I have. It's very important to note here that in my actual example, I WILL NOT KNOW what subclass types are possible (so I can't code anything along the lines of if(Animal instanceof Dog), etc.

Any help you can provide is greatly appreciated.
 
Marshal
Posts: 24473
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I want to be able to tell what type of "Animal" object I have.

Why do you want to do that? Wouldn't it be sufficient to simply assign the object to a variable whose type is Animal? If not, why not?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every Object has a getClass() method that you can use to, um, get the class. See the link for other methods common to all Objects. In many cases, if you need behavior that depends on the class, it's better to put that behavior into the specific subclasses. But if you really need getClass(), the method is available for you.
[ October 31, 2007: Message edited by: Jim Yingst ]
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Following up on Paul's question, what would you do by knowing this type you've never heard of before? You can't really to this:

We usually try to get to something more like:

Look into Strategy and State patterns or even plain old delegation, for how various subclasses could plug different behavior into a common method.
 
Java Cowboy
Posts: 16084
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you get into a situation where you find that you need runtime type information, be it using "instanceof" or using reflection or getClass(), it should raise a red flag that maybe the design of your code isn't really object oriented.

In most cases you can, and should, replace such a mechanism with polymorphism: create a common base class or interface and call the methods in concrete subclasses or implementations through the base class or interface.
 
Aaaaaand ... we're on the march. Stylin. Get with it tiny ad.
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!