0

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.

  • I suspect the keyword `extends` was used for this to avoid adding another keyword that meant "extends or implements" to the language grammar. Adding keywords is bad for backwards compatibility, because if you use a word that someone has used as a name for a variable/method/class/etc. then their code will stop working in the new version. Backwards compatibility is an explicit goal for Java language changes. – kaya3 Feb 06 '20 at 06:37

1 Answers1

2

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).

kaya3
  • 47,440
  • 4
  • 68
  • 97
Kartik
  • 7,677
  • 4
  • 28
  • 50