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.

Vic Seedoubleyew
  • 9,888
  • 6
  • 55
  • 76
Day_Dreamer
  • 3,311
  • 7
  • 34
  • 61

3 Answers3

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.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • but in this code, LocationCallback is an interface – Day_Dreamer Mar 09 '15 at 10:40
  • handleNewLocation is not omplemented. ans also declared interface... what do I miss? – Day_Dreamer Mar 09 '15 at 10:41
  • @StefanBeike Spiderman, Superman and Phantom all should be able to do that. But please ... don't confuse people with that kind of comment! – RaviH Mar 09 '15 at 10:41
  • 1
    It means that an implemented instance of the Interface should be passed as a parameter there. – Yellen Mar 09 '15 at 10:42
  • 4
    @Day_Dreamer The `LocationProvider` constructor should be passed an instance of a class that implements the `LocationCallback` interface. – Eran Mar 09 '15 at 10:44
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).

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
  • 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 Mar 09 '15 at 10:57
  • @Day_Dreamer - Which version of java are you using?. The answer can vary based on that :P – TheLostMind Mar 09 '15 at 10:59
  • 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 :) – Day_Dreamer Mar 09 '15 at 11:00
  • 2
    "public void handleNewLocation(Location location);" is virtual. :P – Yellen Mar 09 '15 at 11:01
  • 2
    @Day_Dreamer - Ok. The *keyword* `abstract` is *redundant*. :P . Check [here](http://stackoverflow.com/questions/7202616/java-abstract-interface). and like *yellen* says, the methods of an interface are *implicitly* virtual.. You don't have to explictly mark them as *abstract* :) – TheLostMind Mar 09 '15 at 11:03
  • @yellen - We all start at level *-1* :P – TheLostMind Mar 09 '15 at 11:09
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.

Community
  • 1
  • 1
Vic Seedoubleyew
  • 9,888
  • 6
  • 55
  • 76