0

The program has the next code:

public class RxTaskHandler<T> implements OnSuccessListener<T>{

   public RxTaskHandler(MaybeEmitter<T> emitter){
        this.emitter = emitter;
    }

     public static void assignTask(MaybeEmitter<T> emitter){
          RxTaskHandler rxTaskHandler = new RxTaskHandler(emitter);
          task.addOnSuccessListenerr(rxTaskHandler);
    } 

   @Override
    public void onSuccess(Object o) {
        emitter.onNext();
    }

}

The question about this line - task.addOnSuccessListenerr(rxTaskHandler). We must add OnSuccessListener interface, but instead of this we add the instance of the class which implements OnSuccessListener. It means we can replace instance of the class (implements necessary interface) by instance of the interface?

Delphian
  • 1,650
  • 3
  • 15
  • 31
  • 3
    There is no such thing as instance of an interface, since interfaces cannot be instantiated. There are only instances of classes that implement the interface. – Eran Sep 14 '17 at 07:35
  • 2
    No, you can replace name of supertype (e.g., interface) with name of its' subtype (e.g., `RxTaskHandler`), not other way around. – Victor Sorokin Sep 14 '17 at 07:35
  • 2
    You can not instantiate the interfaces. So, you always use the class objects that are implementing the interface. – Procrastinator Sep 14 '17 at 07:39
  • Eran, Procrastinator we can create the instance of the interface https://stackoverflow.com/questions/4000062/can-we-create-an-object-of-an-interface. This instance will be a simple reference object – Delphian Sep 14 '17 at 07:47
  • Victor, I thought this only applies to classes (when using extends, not implements). – Delphian Sep 14 '17 at 07:58
  • 1
    @Delphian, it’s a dispute about words. The way I (and most, I believe) use the words, an object is always an instance of exactly one class (and no interfaces). At the same time, it has the *types* of any interfaces that the class implements, as well as of any superclasses of the class. – Ole V.V. Sep 14 '17 at 08:11
  • Ole, this mean that if a class implements an interface, can I use an instance of this class instead of an interface instance? – Delphian Sep 14 '17 at 08:15
  • Yes, @Delphian; you not only can, you have to. – Ole V.V. Sep 14 '17 at 22:58

1 Answers1

1

Yes,

It follows the rule of inheretance. Here interface acts as parent class and class which implements interface acts as child class.

Swapnil Raut
  • 101
  • 6