How is this code able to instantiate a new object from an interface?

In my CS110 class, we are developing an Android application using the Google Maps API, and one of the concepts is being studied by the Observer design pattern. Design template teaches publisher and subscriber concepts. Therefore, when the status of the publisher changes, it notifies all subscribers. In this example, the LocationListener is a subscriber, but the implementation is confusing.

The code snippet is as follows:

LocationListener locationListener = new LocationListener() {
  public void onLocationChanged(Location location) {
  // Called when a new location is found by the network location provider.
  makeUseOfNewLocation(location);
  }
  public void onStatusChanged(String provider, int status, Bundle extras) {}
  public void onProviderEnabled(String provider) {}
  public void onProviderDisabled(String provider) {}
};

LocationListener is an interface, and I understand that you cannot create an interface. However, the operator LocationListener locationListener...creates the object from Interface, how is this possible?

, , . , , , . , , .

, , .

+3
2

LocationListener - !

LocationListener , LocationListener

, , :

LocationListener.. Class A B, B A. .

- , . ,

new LocationListener() {
  public void onLocationChanged(Location location) {
  // Called when a new location is found by the network location provider.
  makeUseOfNewLocation(location);

, LocationListener!

, LocationListener !

, !!

+1

All Articles