File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Checking for object type

 
T sandya
Ranch Hand
Posts: 64
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Sandaya.
 
Adam Schaible
Ranch Hand
Posts: 101
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if (a instanceof Apple) { ...
 
T sandya
Ranch Hand
Posts: 64
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1970
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic