• Post Reply Bookmark Topic Watch Topic
  • New Topic

Checking for Object Type  RSS feed

 
Philip Poots
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Larry Frissell
Ranch Hand
Posts: 82
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you can use something like
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16058
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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 ]
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
IndividualArtist (or some such.)

How about SoloArtist?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!