(Easy?) Java Interface Question

Status
You're currently viewing only dmcnaught's posts. Click here to go back to viewing the entire thread.
Not open for further replies.

dmcnaught

Ars Scholae Palatinae
684
I do this stuff all the time, so you've gotta be missing something simple. ;)

Where is the object that implements EventListener coming from?

There needs to be a class somewhere that implements that interface, and you need an object of that class. You can then call the method through the object.

I have a lot of code that basically does:

code:<HR>
Code:
public interface Foo {
  public void doStuff(int bar);
}<P>...<P>   Foo f = baz.getFooImpl();   // baz is a "factory object"
   f.doStuff(5);<P>

Note that my code doesn't know or care what actual class 'f' points to, as long as it satisfies the contract laid out by the interface definition. The 'getFooImpl()' method needs to refer somehow to an actual class that implements the interface, and return an instance of that class.

If this doesn't help, post more details of your code, and maybe we can give you some wisdom.
 

dmcnaught

Ars Scholae Palatinae
684
Then I don't see why it wouldn't compile. It certainly won't run until the user supplies an implementation.

I created the following:

code:<HR>
Code:
// TestInterface.java<P>public interface TestInterface {
	public void doStuff();
}<P>// Test.java<P>public class Test {
	TestInterface foo = null;<P>	public Test (TestInterface ti)
	{
		foo = ti;
	}<P>	public void doSomething()
	{
		foo.doStuff();
	}
}<P>

They compile just fine with the Sun JDK.
 
Status
You're currently viewing only dmcnaught's posts. Click here to go back to viewing the entire thread.
Not open for further replies.