my dog learned polymorphism
The moose likes Beginning Java and the fly likes Checking for object type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Checking for object type" Watch "Checking for object type" New topic

Checking for object type

T sandya
Ranch Hand

Joined: Aug 03, 2006
Posts: 64
Hi Friends,

public class A{

public void findObject(){
Apple a = new Apple();
Apple b = new Apple();

boolean flag = compare(a,b);

public boolean compare(java.lang.Object o1,java.lang.Object o2){


here my question is from findObject() we are calling compare() method with two apple objects.

In compare() method we are declaring the parameters as java.lang.Object 's.

In compare() method how to fine these two objects are Apple objects?

Thanks in adavance.

Adam Schaible
Ranch Hand

Joined: Oct 04, 2007
Posts: 101
This is probably a larger question that you might realize.

The Java language provides an operator "instanceof" to check the runtime type of an object.

It has been said that, in most cases, instanceof should not be used to test for object equality as it can break the symmetric property of the equals method's equivalence relation. In this case, using the getClass method of Object is the appropriate choice. This makes a lot of sense.

The getClass example would work in a similar manner:

As an even further alternative, you may consider using something like this:

To determine if o1 is a superType pf o2. This may or may not be useful, depending on how you wish to compare o1 and o2
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

if (a instanceof Apple) { ...

[Jess in Action][AskingGoodQuestions]
T sandya
Ranch Hand

Joined: Aug 03, 2006
Posts: 64
hi friends,
here one more thing i need to mention is,
now i can tell the this Apple object, so we can apply InstanceOf on Apple object.

if i dont know it is the Apple object, with which object i need to check the object with instanceOf operator?

thanks in adavance.
srinu reddy

Joined: Oct 21, 2007
Posts: 12
follow this approach

public boolean compare(java.lang.Object o1,java.lang.Object o2){
if (o1.getClass().equals(o2.getClass())) {
//Perform comparisson and return true if so
} else {
return false;
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Originally posted by honey:
follow this approach

I don't think one can be prescriptive like this. There are two possible approaches (treat different subclasses as possibly equal, or treat them as definitely unequal). Each approach could be appropriate in some circumstances and inappropriate in others.

It's one of those cases where you have to think carefully, make your choice, and explain it in the javadoc comments.

If your equals() method is not expecting to deal with different subclasses, you could add an assertion to check that it doesn't get given them. Or, if there is no need for subclasses in your application, you could declare the class "final", and use the javadoc to explain that this is to avoid potential confusion when comparing objects.
[ October 29, 2007: Message edited by: Peter Chase ]

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
I agree. Here's the link:
subject: Checking for object type
It's not a secret anymore!