Reputation: 3373
I encountered some Java code:
public class LocationProvider {
public interface LocationCallback {
public void handleNewLocation(Location location);
}
// class constructor
public LocationProvider(Context context, LocationCallback callback){
...
}
}
For the first time in Java, I am encountering a constructor or method with an argument of a "type" that is an interface. Is it possible to create objects of interfaces ? Can you use them like regular objects ?
In C++ I know it's not possible to create objects of an abstract class.
Upvotes: 1
Views: 435
Reputation: 10536
You are confusing a reference's type with the referenced object's type.
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:
new MyInterface()
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:
all interfaces implemented by the object's class or its superclasses
This is called multiple inheritance of type.
Another way to see it:
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.
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
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
.
Upvotes: 0
Reputation: 36304
Ok. Lets go over the basics :).
class A implements X{// where X is an interface
}
class B implements X{
}
now, we have
void someMethodAcceptingInterfaceX(X someInstanceOfX)
{
//do something
}
Now you can do,
X a = new A();
X b = new B();
someMethodAcceptingInterfaceX(a);
someMethodAcceptingInterfaceX(b);
i.e, you can pass anything which is interface X. Any class which implements an interface is said to be an instance of that interface (in a broader context).
Upvotes: 3
Reputation: 393831
You never create an object of "Class that is interface". You can create an object of a class that implements the interface, and pass that object as a parameter to a method that expects an argument of the interface type.
Upvotes: 6