You are confusing a reference's type with the referenced object's type.
What is going on
Instantiating a class into an object, and having a reference of a given type are two different things:
indeed, you cannot instantiate an interface. This means:
- you cannot call
new MyInterface()
- no object will ever have a type
MyInterface
(bear in mind that I am talking about the object here, not the reference to it).
conversely, a reference can have any type that is a supertype of the type of the object it is referencing. Supertypes of a given type are:
Another way to see it:
- An interface cannot be instantiated
- But an interface is a valid type
In code this means :
MyInterface i; // This is valid, only says that the type of i is MyInterface
i = new MyInterface(); // This is not valid, cannot instantiate the interface
You can read about the difference between a reference type and an object's type here.
Example
To give you an example, with the Integer
class, which extends the Number
class and implements the Serializable
class :
Integer i = new Integer(1); // The object referenced by i is of type Integer, forever
// i is a reference to that object,
// its type is a reference to Integer
Number n = i; // Now n is also referencing the same object.
// The type of n is a reference to a Number.
// The referenced object hasn't changed, its type is still Integer
// This is possible because Number is a supertype of Integer
Serializable s = i; // Same, s is now referencing the same object.
// The object is still the same, its type hasn't changed
// The type of s is a reference to a Serializable.
// This is possible because Serializable is a supertype of Integer
Application to your case
The constructor definition
public LocationProvider(Context context, LocationCallback callback)
requires that the second argument be a reference to a LocationCallback
.
This doesn't mean that the referenced object should be of that type, and indeed this is impossible. It only means that the reference passed should be a subtype of LocationCallback
, ultimately referencing an object whose type is a class which implements LocationCallback
.