I have to work with an API that is accompanied by a few examples. In one of the example, an interface is being directly used to call one of the methods of that interface. However, since Interface do not contain any implementation, I would like to know: How can the methods be used in the example to accomplish anything without defining a class that implements that interface?
Or, can the interface contain complete method definition too? (which seems to be the case here)
No, the interface contains only method signatures. You cannot have an implementation in an interface.
What is (most likely) happening in your case is something like (pseudocode):
InterfaceA {
methodA();
}
class A implements InterfaceA {
methodA() // implementation
}
InterfaceA getInterface() {
// some code which returns an object of a class which implements InterfaceA
}
Calling method:
InterfaceA in = getInterface() // you might get an instance of class A or any other class which implements InterfaceA
in.methodA(); // implementation from whatever class the method returned
Do you mean something like this...
InterfaceA a = getInterface();
a.method();
In this case, a
will be an instance of a class that implements InterfaceA
- what that class is doesn't matter, as all you care about is the interface methods.
- concrete implementation of the interface must exist. It is however referenced by its interface, and that's why it looks like call methods on the interface.
- interfaces cannot have implementations ("complete method definitions")
Look where the object is created. You can have:
public void doSomething() {
MyInterface interface = new MyInterfaceImplementation();
doSomething(interface);
}
public void doSomethingElse(MyInterface interface) {
interface.someMethod();
}
Thus by looking at the doSomethingElse()
method it may appear that it doesn't have an implementation, but whoever called the method provided the implementation (MyInterfaceImplementation
in this case);
Perhaps you mean something like this:
(new Runnable(){
public void run() {
System.out.println("I'm running already!");
}
}).run();
If so, it's called an anonymous inner class.
There's two things you might be talking about:
public static void sort(List list, Comparator c){ ... }
Here, the parameter list
has the interface type List
but anyone who calls the method will have to pass in an instance of a concrete class like ArrayList
that implements the interface, and the code of the method will call the methods of that concrete class - this mechanism is called "dynamic method dispatch" and lies at the heart of the core OO principle of polymorphism.
sort(myList, new Comparator(){
public int compare(Object o1, Object o2){
...
}
});
This is an example of an anonymous class: the code actually defines a new class without a name that implements the Comparator
interface, and at the same time creates an instance of that class. This is used in Java mainly for things where other languages would use language constructs such as function pointers, closures or callbacks: to pass a piece of code to be executed within the method you're calling.
An interface just defines a contract. You must have an implementation to do the actual stuff.
What your are referring to must be the "Program to an interface, not an implementation" which is a best-practice.
Or maybe the API uses Dependency Injection to provide the implementation but you just see how the code is used.
精彩评论