I understand what is confusing you. As you have said, RS2Object is an interface, and as such we cannot instantiate it. For example, we cannot do
RS2Object anObject = new RS2Object();
However, we can refer to an RS2Object implementing class, e.g InteractableObject, as an RS2Object. While Objects#closest returns an RS2Object, in reality it is returning an instance of a concrete class which implements RS2Object.
Take this example. Consider some interface:
public interface SomeInterface {
public String someMethod();
}
We can then have some classes implementing this interface. For example:
public class SomeClass implements SomeInterface {
public SomeClass() { ... } // Constructor
@Override
public String someMethod() {
return "Hello from SomeClass";
}
}
... and we can have another class also implementing this interface:
public class SomeOtherClass implements SomeInterface {
public SomeOtherClass() { ... } // Constructor
@Override
public String someMethod() {
return "Hello from SomeOtherClass!";
}
public String someOtherMethod() {
return "ABC";
}
}
We can then refer to both of these classes as their parent interface. For example:
List<SomeInterface> list = new ArrayList<SomeInterface>();
list.add(new SomeClass());
list.add(new SomeOtherClass());
for (SomeInterface item : list) {
System.out.println(list.someMethod());
}
// -- Output -- //
> "Hello from SomeClass!"
> "Hello from SomeOtherClass!"
HOWEVER, note that this would not compile:
SomeInterface example = new SomeOtherClass();
System.out.println(someMethod()); // This works!
System.out.println(someOtherMethod()); // Uh-oh! This does not work as SomeInterface does not have a 'someOtherMethod' method, despite SomeOtherClass having it.
Hopefully that cleared things up. I didn't write the code in an IDE so hopefully I didn't make any silly typos/mistakes...
Let me know if you're still confused
Apa