1

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.

2

3 Answers 3

6

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.

Sign up to request clarification or add additional context in comments.

5 Comments

but in this code, LocationCallback is an interface
handleNewLocation is not omplemented. ans also declared interface... what do I miss?
@StefanBeike Spiderman, Superman and Phantom all should be able to do that. But please ... don't confuse people with that kind of comment!
It means that an implemented instance of the Interface should be passed as a parameter there.
@Day_Dreamer The LocationProvider constructor should be passed an instance of a class that implements the LocationCallback interface.
3

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).

6 Comments

maybe this is another question but: in C++ I know the interfaces have pure virtual methods. "virutal" - because they must be implemented by other classes, that when their objects are passes to a function as more general "interface class objects" we should know which overriding method to run. Here in java, even in the code example I gave I don't see the interface method is virtual
@Day_Dreamer - Which version of java are you using?. The answer can vary based on that :P
I don't know which Java is this code example. found it somewhere in SO. which cases exist - I'll check you already gave me enough to understand now :)
"public void handleNewLocation(Location location);" is virtual. :P
@Day_Dreamer - Ok. The keyword abstract is redundant. :P . Check here. and like yellen says, the methods of an interface are implicitly virtual.. You don't have to explictly mark them as abstract :)
|
0

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:

    • all superclasses of the object's class
    • all interfaces implemented by the object's class or its superclasses

      This is called multiple inheritance of type.

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.