0

I'm trying to implement a UserInterface interface, which always needs to run in a thread (so is Runnable). So I have code like this, where SpecificInterface implements UserInterface:

UserInterface myUI = new SpecificInterface(...);
Thread thread = new Thread(myUI);
thread.start();

But this obviously doesn't work, because I can't make UserInterface implement Runnable since interfaces can't implement other interfaces. And I can't just make SpecificInterface runnable since that defeats the point of using interfaces.

How am I supposed to make this work? Do I need to make UserInterface an abstract class, or create a RunnableInterface abstract class which implements UserInterface and Runnable and inherit my UI's from it, or..? I am rather confused as to why the "simple" solution can't work.

Googling was less than helpful, all I find is links telling me how to use the "Runnable interface" :|

2
  • Show the UserInterface and the SpecficInterface class? Commented Aug 28, 2013 at 3:52
  • @JoshM They are way too big to be shown here. But basically they all have a custom event dispatcher loop in the run() method (this is for an exercise). Commented Aug 28, 2013 at 3:57

1 Answer 1

3

Interfaces can extend other interfaces.

interface UserInterface extends Runnable {
    void someOtherFunction();
    // void run() is inherited as part of the interface specification
}

public class SpecificInterface implements UserInterface {
    @Override
    public void someOtherFunction() {
        . . .
    }

    @Override
    public void run() {
        . . .
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

UserInterface would have to extends Runnable. It appears that SpecificInterface is a sub-interface of UserInterface (judging from his declaration statement).
@JoshM - Sorry; I had names backwards.
That works great, thanks! Didn't know about extending interfaces!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.