Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

using `instanceof`with an "object" parameter

 
Clay Chow
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Marshal
Pie
Posts: 21362
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Clay Chow
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21362
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic