0

I am trying to detect Geofences, and this requires the onConnected() function in the ConnectionCallBacks interface. However I also use this interface in a fragment (separate class), and override the onConnected() method there as well. Is it possible to have two different classes overriding the same method? Or will only one instance of the function be used?

I've been trying to figure out why the Geofences aren't showing up in my notifications since I know they are being created. Any help is much appreciated!

Salman Zaidi
  • 9,342
  • 12
  • 44
  • 61

1 Answers1

0

So, Java support multiple interfaces and so, it allow as many as you like to implements says you have 5 different interfaces in the java class Connect5 and they all have the same method onConnected(). But remember, the object created is bounded to a single Java Class and so there is only one method onConnected() that is exposed to the object. Now, if the object c5 that is of the type Connect5 is also extends from another Java class Connect4 and implements interface IConnect4 that also have an onConnected() method and only implements in Connect4 class but not in Connect5. Now you have an object c5 of class Connect5 and pass to a method that take IConnect4 then invoke ((IConnect4)c5).onConnected() still execute the onConnected() implementation on the Connect5 class. However, you can invoke the c5.super.onConnected() will invoke the implementation on Connect4 class. Now, the danger come when you decided to start overriding these onConnected() methods via anonymous inner class.

TA Nguyen
  • 453
  • 1
  • 3
  • 8