Search code examples
javagenericsextendsinterface-implementation

Can an Implementing Class of Foo be called as an extending class of Foo?


This question has been baffling to me; below is an example.

interface class:

public interface UserInterface{

   public String make_nickname();

}

implementing class:

public class UserInfo implements UserInterface{

   private String name;
   private String nickname;

   public String make_nickName(){
   this.nickname = this.name + "ish";
   return this.nickname;
   }


}




That given, I'm calling any instances that implements UserInterface like such:

some arbitrary function in another remote class:

public void showNickName(<T extends UserInterface>T user){
   system.out.println(user.make_nickname());
}

T is used to denote a (generic) implementing class of UserInterface.
But here I called it as <T exnteds UserInterface> which as far as I know implies T is an subclass(not implementing class) of UserInterface.
Is this legitimate grammar of calling instances of UserInfo?
Somehow above code seems to work for me.

Any feedback would be welcome.


Solution

  • As mentioned in the Java tutorial on bounded type parameters:

    In generics context, extends is used in a general sense to mean either "extends" (as in classes) or "implements" (as in interfaces).