• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Checking for Object Type

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to test a variable's object type.

ie. is this object of type ObjectType1 / ObjectType 2

here is the scenario:


I would like to print either the name of the artist if an Artist or the names of the members of Band. something like this:


Any help would be appreciated. Does the fact that Band inherits from Artist mean that it is also of type Artist as well as Band? Because then the above probably wouldn't work well as I don't require the band name.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Does the fact that Band inherits from Artist mean that it is also of type Artist as well as Band?

Yes.

when you say "class B extends A", then EVERY 'B' is also an 'A'. Sort of like "class Mustang extends car". Every Mustang is a car, but not every car is a Mustang. anyplace you need a car, you can use a mustang.
[ October 29, 2008: Message edited by: fred rosenberger ]
 
Philip Poots
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks,

so then the final question is what is the correct syntax (if it can be done) of

"artist is of type Artist?"

is instanceOf applicable here?
 
Ranch Hand
Posts: 82
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you can use something like
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what the instanceof operator is for.

Try:

Larry's solution might also work, but is more complex than necessary in this case.

Note however that if you need to do this in an application (checking the type of an object), it's a sign that you are not using the object oriented capabilities of Java fully. Almost always, you can solve this kind of problem in a more object oriented way by using polymorphism.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, what Jesper says. Imagine if "Artist" had a method "addIndividualsToList(ArrayList<Artist> list)". Then the implementation in Artist could do list.add(this), and the implementation in Band could loop over the individual Artists in the Band and add each of them. You could then collect the individuals from a list of Artist without having to test the type of each object at all!

Another thing, by the way: extending a concrete class (like Band extending Artist) often leads to problems; consider making Artist abstract, and introducing a class named IndividualArtist (or some such.) For example, one potential problem here is that given just the two classes, it will be possible for a Band to be a member of a Band!
[ October 29, 2008: Message edited by: Ernest Friedman-Hill ]
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ernest Friedman-Hill:
IndividualArtist (or some such.)


How about SoloArtist?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic