Reputation: 3299
Can anyone explain to me what type of structure is this codes? I'm using Google Location and it was interesting to me when I face this code:
private LocationCallback locationCallback;
public final void readyToUpdateLocation() {
locationCallback = new LocationCallback() {
public void onLocationAvailability(LocationAvailability p0) {
//do sth here...
}
public void onLocationResult(LocationResult locationResult) {
//do sth here....
}
};
}
public final void startLocationUpdates() {
try {
FusedLocationProviderClient providerClient = this.fusedLocationClient;
providerClient.requestLocationUpdates(this.locationRequest, this.locationCallback, null);
} catch (Exception var2) {
Handle(car2);
}
}
//What exactly LocationCallback class is!
public class LocationCallback {
public LocationCallback() {
}
public void onLocationResult(LocationResult var1) {
}
public void onLocationAvailability(LocationAvailability var1) {
}
}
As you can see, this is not an abstract class, neither interface to override. No override keyword. So what's this?
Upvotes: 1
Views: 91
Reputation: 2490
It's an anonymous class. You can do anonymous classes to extend classes that aren't abstract when you just want to override some of their methods. It's not limited to interfaces and abstract classes.
LocationCallback is a class that follows the usual logic of "base implementations". Normally the principle would be, define an interface:
interface LocationCallback {
void onLocationResult(LocationResult var1);
void onLocationAvailability(LocationAvailability var1);
}
But as this interface defines two methods and you might be interested on reacting to only one of the two events provided, it's a bit of too much work to require all users to provide implementations to both methods every time they want to react to one.
To reduce the amount of work, typically a base class is provided, that implements all of the interface's methods, and make them do nothing:
public class BaseLocationCallBack implements LocationCallBack {
@Override
public void onLocationResult(LocationResult var1) {
}
@Override
public void onLocationAvailability(LocationAvailability var1) {
}
}
That way, users of the callback can simply extend BaseLocationCallBack and only override the methods they want to override. Makes things simpler.
In your case, they skipped defining an interface altogether, and only defined the base class with zero implementation. That works too.
Upvotes: 1
Reputation: 109547
The overrides are missing in the anonymous class.
locationCallback = new LocationCallback() {
@Override
public void onLocationAvailability(LocationAvailability p0) {
//do sth here...
}
@Override
public void onLocationResult(LocationResult locationResult) {
//do sth here....
}
};
Now the class is for asynchroneous usage: later those two methods are called and the provider gets called back on those two events.
However if the provider is not interested in availability or result, heshe does not need to override the unneeded callback method.
If there is just one callback, often an interface is used instead of a class.
Upvotes: 1