aspose file tools*
The moose likes Beginning Java and the fly likes How do I pass a reference of an interface to a different method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How do I pass a reference of an interface to a different method?" Watch "How do I pass a reference of an interface to a different method?" New topic
Author

How do I pass a reference of an interface to a different method?

Zach Shaner
Greenhorn

Joined: Oct 05, 2011
Posts: 6
My previous question seems to have gone out of context which is mostly my fault(I'm terrible at putting things that happen in my head into words ) Remember: This code refers to a Game API

What I want to do:

So I guess I will start by explaining which reference of what interface I'm talking about then describe my problem.
The interface is located in a .java file named "Block". This interface is part of an API outside of my project. Here is the declaration:

In the same API there is a an .java file labeled "Event" for Player interaction. This event gets the x,y,z chords of a clicked block in the game. Here is the method:

Now that we have the methods outside of my project cleared up(if you don't understand what anything above means, ignore it all. It is only there so you know that all the code I'm going to type below WILL work so compiling errors isn't a problem) lets move to my actual project.

Inside a .java file Labeled "PDPlayerListener" I have the following:

this creates a reference to the interface Block named SelBlock and stores the clicked block inside it. My problem is that I do not know how to pass SelBlock to a different method (in a completely different .java file but in the same project). Any help would be greatly appreciated.

-Thanks
Panda
Tina Smith
Ranch Hand

Joined: Jul 21, 2011
Posts: 171
    
    5

You can use interfaces the same way as you use classes when calling methods.

this creates a reference to the interface Block named SelBlock and stores the clicked block inside it.


Not exactly. This returns a reference to a class/object (not an interface). The class/object that is returned implements the Block interface, which just describes what you know the returned class/object can do.

For example, all Block can be clicked.
A RoundBlock can be rolled.
A SquareBlock can be dropped.

getClickedBlock() returns a Block. It could be either a RoundBlock or a SquareBlock, or some other type of Block. You don't know which, but all you need to do with it is click it, so knowing it's a Block is good enough.


Everything is theoretically impossible, until it is done. ~Robert A. Heinlein
Zach Shaner
Greenhorn

Joined: Oct 05, 2011
Posts: 6
Tina Smith wrote:You can use interfaces the same way as you use classes when calling methods.

this creates a reference to the interface Block named SelBlock and stores the clicked block inside it.


Not exactly. This returns a reference to a class/object (not an interface). The class/object that is returned implements the Block interface, which just describes what you know the returned class/object can do.

For example, all Block can be clicked.
A RoundBlock can be rolled.
A SquareBlock can be dropped.

getClickedBlock() returns a Block. It could be either a RoundBlock or a SquareBlock, or some other type of Block. You don't know which, but all you need to do with it is click it, so knowing it's a Block is good enough.



So I would pass SelBlock like I would any other class?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18138
    
    8

Zach Shaner wrote:So I would pass SelBlock like I would any other class?


Well, yeah. Didn't you try it?
Zach Shaner
Greenhorn

Joined: Oct 05, 2011
Posts: 6
Paul Clapham wrote:
Zach Shaner wrote:So I would pass SelBlock like I would any other class?


Well, yeah. Didn't you try it?


Yea. I got a syntax error. It was probably user error. Ill self troubleshoot. Thanks!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18138
    
    8

Zach Shaner wrote:Yea. I got a syntax error. It was probably user error. Ill self troubleshoot. Thanks!


Not "probably" user error. It was definitely your error. (That's nothing personal against you -- it's just part of being a programmer. When you're a programmer, errors are always your errors. ) Anyway don't hesitate to post that syntax error here if you'd like to talk about it.
Zach Shaner
Greenhorn

Joined: Oct 05, 2011
Posts: 6
Lol true. All errors are user errors :P. I just meant I probably typed the syntax wrong. Which I did. Thanks for the help though!

-Panda
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do I pass a reference of an interface to a different method?
 
Similar Threads
Calling method in Java App from Jython
How Iterate Synch'd List of Connections and close them?
Troubles with mouse events in Java 2D
Midi stop working
Interfaces and polymorphism design questions - I would appreciate some input