wood burning stoves*
The moose likes Beginning Java and the fly likes using `instanceof`with an Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "using `instanceof`with an "object" parameter" Watch "using `instanceof`with an "object" parameter" New topic
Author

using `instanceof`with an "object" parameter

Clay Chow
Ranch Hand

Joined: Nov 09, 2008
Posts: 35

For the below code, I get the following errors:


inconvertible types
found: Object
required: Fruit
if (o instanceof Fruit)
Fruit a (Fruit) a


I am using a java/javac version 1.6. Any idea why this is not working for my compiler ? I assume it should work because all classes inherit from Object. I thought it was referring to the wrong Object class, so I changed the method to public boolean add(java.lang.Object o), which seems to work.



Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18917
    
  40

I thought it was referring to the wrong Object class, so I changed the method to public boolean add(java.lang.Object o), which seems to work.


Well, do you happen to have you own Object class, that is in the same package?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Clay Chow
Ranch Hand

Joined: Nov 09, 2008
Posts: 35
Definitely do not.
Did not make one and

Made sure there are none in the directory and program is not linked to any package.

From the java API i see there's another Object interface, org.omg.CORBA
Interface Object.

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18917
    
  40

From the java API i see there's another Object interface, org.omg.CORBA Interface Object.


But unless you "import" that Object, it wouldn't be an issue. This is why I asked if you had an Object in your package, because the current package (just like java.lang) does not need to be imported.

Henry
 
jQuery in Action, 2nd edition
 
subject: using `instanceof`with an "object" parameter