• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Checking for object type

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
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
if (a instanceof Apple) { ...
 
T sandya
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
}
}
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic