Help with objects
Hi, I'm Chris, I'm a pretty new Java programmer, and I need a bit of help with this.
I have a main program (call it Main.java) and a class (Class.java) and then I have another class (Class2.java).
Main.java creates a new Class.java. We'll say Class.java is a sprite library, allowing you to manage sprites. In Class2.java manages a car, for example, which creates a car sprite, and contains all the driving code, etc.
in Class2, it sets Class.java's Object obj variable (sprite.setObj( this ) ). A third class, Class3.java contains a LinkedList of all the sprites created. It doesn't contain a list of the Class2.java classes connected to the sprites however.
Now I need to call a function on Class2.java, but I only have the sprite object. So I should be able to use sprite.getObj().function(); and it should work fine, right? Except Netbeans doesn't know that sprite.getObj() will have that command, and can't compile!
How can I get around this? Thanks!
Chris K.
[1014 byte] By [
cbk1994a] at [2007-11-27 7:36:43]

If you want to call a method on an object, that method has to exist, AND it has to be declared as existing.
So declare the variable holding a reference to the object in question, to have the type of the object. Alternatively, you can declare it to be a superclass (a superclass that also implements the method in question) or you can declare an interface, say that the object implements that interface, and then declare the variable to hold references to that interface.
It would be a lot easier to follow your questions, and to respond to them, if you used meaningful names that weren't already in use by the JDK. (i.e., not "Class" and "Class2")
Okay, I have an actual Object--that is the type, and the var name is obj.
So, I can hold any type of object in it.
I don't know what type of object is in it, but I know that it has a function called hurtObject() and I know what params it takes.
Netbeans doesn't, and won't compile.
How do I make it so I can do hurtObject off of that. So for example ...
sprite.getObj().hurtObject( 5 );
Netbeans doesn't know if getObj() will have a hurtObject() command, so I need to tell it it will, or some other way trigger that.
Thanks
java.lang.Object doesn't have a method called "hurtObject()".
If you know that the actual object that is there will have such a method, then create an interface called, say, "Hurtable", with a method called "hurtObject()".
(Although I'd argue that the "Object" in "hurtObject" doesn't really add any info and should be dropped -- just call it "hurt". For that matter, calling your variable name "obj" also provides no real information.)
Then declare the "obj" variable (or whatever you name it) to be of type "Hurtable". Then all those objects -- the ones that you know have a "hurtObject" method -- declare them to implement the Hurtable interface.
You need to create an interface that declares the hurtObject method, then whatever class you are setting obj to must implement that interface.
Right now you know that the object has a hurt method, but there's no way for Java to know this. That's what interfaces are for - it tells Java that whatever obj is, it will have this method.
That is:
public interface Hurtable {
public void hurtObject( int amountOfPain );
}
public class Dude implements Hurtable {
public void hurtObject( int amountOfPain ) {
System.out.println("Ouch. That hurts!");
}
}
public class Monster implements Hurtable {
public void hurtObject( int amountOfPain ) {
System.out.println("Argggg... I will hurt you back!!!!");
}
}
public class Sprite {
Hurtable obj;
// some other methods
}
// Then you can do
sprite.obj.hurtObject(5);
Jemiah
Hahahahah! You just made me laugh!Anyway, I have the interface set up, but my Sprite class doesn't like Hurtable--doesn't know what it is!Hurtable is in Hurtable.java in the right folder, do I need to import it somehow?ThanksChris
> Anyway, I have the interface set up, but my Sprite
> class doesn't like Hurtable--doesn't know what it
> is!
Please quote the actual error message you're getting.
> Hurtable is in Hurtable.java in the right folder, do
> I need to import it somehow?
Not if it's in the same folder and you're referring to it correctly.
Here is the error message:
init:
deps-jar:
Compiling 3 source files to /Users/Home/Desktop/projects/CarDestiny/build/classes
/Users/Home/Desktop/projects/CarDestiny/src/chess/Sprite.java:26: cannot find symbol
symbol : class Hurtable
location: class chess.Sprite
Hurtable obj;
1 error
BUILD FAILED (total time: 0 seconds)
Figured that out, they were in the wrong package. Sorry!
Anyway, here's my next problem.
Hurtable obj;
[...]
public void setObj( Object object )
{
obj = object;
}
Here is the error
init:
deps-jar:
Compiling 2 source files to /Users/Home/Desktop/projects/CarDestiny/build/classes
/Users/Home/Desktop/projects/CarDestiny/src/chess/Sprite.java:30: incompatible types
found: java.lang.Object
required: chess.Hurtable
obj = object;
1 error
BUILD FAILED (total time: 0 seconds)
Thanks
Chris
You have to propagate the information that you're building Hurtable objects all the way back to where they're assigned.
Well, you don't have to. You could just cast the variable before you invoke the method. But you get stronger code (well, stronger-typed code) if you just stay formal and consistent with the types.
Generally speaking you shouldn't be using things typed as "Object".
Not quite sure what you mean by that. Care to explain more?
Here's what I have.
public class Car implements Hurtable
{
Sprite sprite;
Screen screen;
public Car( Screen window )
{
screen = window;
sprite = screen.newSprite( "/car_1.png", 300, 300 );
sprite.setObj( this );
}
Problem is, setObj() wants a Hurtable not a Car.
Help please!
Chris
That shouldn't be a problem. If Car implements Hurtable, then every method that wants a Hurtable will take a Car.Maybe your compiled classes got out of sync. Try deleting all your .class files and recompiling your .java files.
Thank you everyone for your help! I got it working, thanks to help from a lot of people :)Thanks guys!