This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
That code will work, but it is not object-oriented programming to test the type like that.
Do a search for "instanceof" and you will find other examples; I wrote about it on this forum about a week ago.