1

I've read about interfaces in Java and now I want to make an interface which allows me to create the construction like below:

public class MyClass implements View.OnTouchListener {
    //Implementation
    public boolean onTouch(View arg0, MotionEvent arg1) {
       /*SOME CODE*/    
       return true;
    }
}

So I want to create similar interface (class). How can I invoke onTouch method from OnTouchListener interface if I don't know which class implements it? And how can I know which classes implements this interface if I decided to separate classes from interface (different libraries)? Hope you understand what I mean.

Thanks a lot!

Nolesh
  • 6,848
  • 12
  • 75
  • 112

3 Answers3

1

Any class that implements an interface must override the methods laid out in the interface, so you can rest easy when it comes to knowing if a class is going to have an implementation for the method you call from an interface object.

To call onTouch from an onTouchListener you just need to pass in the object to a reference of type onTouchListener and go from there

onTouchListener myOnTouchListener = new MyClass();
myOnTouchListener.onTouch(arg0, arg1);

For the second part of your question, I think this StackOverflow question should be useful to you.

Community
  • 1
  • 1
David B
  • 2,688
  • 18
  • 25
1

I feel like I understand part of your question.

how can I know which classes implements this interface if I decided to separate classes from interface (different libraries)

using a setup like

MyClass myObject = new MyClass();
if(myObject instanceof myInterface){  //if the object implements the interface
    /*Do Something*/
}
else{
    /*Do Something Else Not Including the Method*/
}

the instanceof reserve word will tell you if the given object is an instance of that class--whether it be through inheritance or implementation

David B. answered the first half of your question--call the onTouch(arg0, arg1) on the object itself.

myObject.onTouch(arg0,arg1);

Just saw your edit--if you use the instanceof keyword, you can check if the given object implements the interface (which means it would have the onTouch method). See the above code block for such an implementation.

user1320716
  • 170
  • 6
1

How can I invoke onTouch method from OnTouchListener interface if I don't know which class implements it?

The interfaces don't invoke methods. As it was described to me, they form a contract between the interface and the developer that, no matter what object implements this interface, it will have the following methods with the following signatures.

In MyClass, you would invoke onTouch the same way you would any other function - pass in a View and MotionEvent to it.

And how can I know which classes implements this interface if I decided to separate classes from interface (different libraries)?

Javadoc is your friend here (as the developer). Alternatively, if you're doing something on a collection, such as ArrayList, Stack, Queue, or something that holds Objects, you can use the following syntax to guarantee that, no matter what, the collection you're using contains only objects that implement your interface:

ArrayList<? extends View.OnTouchListener> myArrayList;
Makoto
  • 104,088
  • 27
  • 192
  • 230