/** * @authorRollen-holt using generics*/ classHello<t, v>{hello () {} PublicT GetName () {returnname; } Public voidsetName (T name) { This. Name =name; } PublicV getage () {returnAge ; } Public voidsetage (V age) { This. Age =Age ; } PrivateT name; PrivateV age; Public Static voidMain (string[] args) {Hello<string, integer> he =NewHello<string, integer>(); He.setage (10); He.setname ("Rollen Holt"); System.out.println (He.getname ()+ " " +he.getage ()); }} /** * @authorRollen-holt Generic class construction method definition*/ classHello<t, v>{Hello (T name, V age) { This. Age =Age ; This. Name =name; } PublicT GetName () {returnname; } PublicV getage () {returnAge ; } PrivateT name; PrivateV age; Public Static voidMain (string[] args) {Hello<String,Integer> he=NewHello<string,integer> ("Rollen", 12); System.out.println (He.getname ()+" "+he.getage ()); }} /** * @authorRollen-holt using wildcard characters*/ classInfo<t>{info (T name) { This. Name =name; } PrivateT name;} classhello{ Public Static voidfunction (info<?>temp) {System.out.println ("Content:" +temp); } Public Static voidMain (string[] args) {info<String> demo=NewInfo<string> ("Rollen"); function (demo); }} /** * @authorrollen-holt Generic Limit*/ classInfo<t>{info (T age) { This. age=Age ; } PrivateT age;} classhello{ Public Static voidfunction (INFO<?extendsNumber>temp) {System.out.println ("Content" +temp); } Public Static voidMain (string[] args) {info<Integer> demo=NewInfo<integer> (1); function (demo); }} /** * @authorrollen-holt Generic Lower limit*/ classInfo<t>{info (T age) { This. age=Age ; } PrivateT age;} classhello{ Public Static voidfunction (INFO<?SuperString>temp) {System.out.println ("Content" +temp); } Public Static voidMain (string[] args) {//only string or object can be used hereInfo<string> demo=NewInfo<string> ("Rollen"); function (demo); }}/** * @authorRollen-holt limitations of generics and subclass inheritance*/ classInfo<t>{} classhello{ Public Static voidMain (string[] args) {info<String> demo1=NewInfo<string>(); Info<Object> demo2=NewInfo<object>(); //Demo2=demo1; Error here }} /*** The above example shows that a subclass of a class can be instantiated by its parent class by polymorphism * But in a generic operation, a generic type of a subclass cannot be instantiated by its parent class's generic type. */if the above bar statement is allowed, it will appear: Exception in thread"Main"java.lang.Error:Unresolved compilation Problem:type Mismatch:cannot convert from Info<String> to Info<object>At Hello.main (Hello.java:12) Two implementations of generic interfaces:/** * @authorRollen-holt implementation of generic interfaces 1*/ InterfaceInfo<t> { Public voidsay ();} //declaring generics directly after a subclassclassHello<t>ImplementsInfo<t>{ Public Static voidMain (string[] args) {info<String> demo =NewHello<string>(); Demo.say (); } Public voidsay () {System.out.println ("Hello"); }} /** * @authorRollen-holt implementation of generic interfaces 2*/ InterfaceInfo<t> { Public voidsay ();} //explicitly given a generic type in the interface implemented by the subclassclassHelloImplementsInfo<string>{ Public Static voidMain (string[] args) {info<String> demo =Newhello (); Demo.say (); } Public voidsay () {System.out.println ("Hello"); }}/** * @authorRollen-holt The type of an incoming parameter using a generic pass-through*/ classInfo<t> { PrivateT var; PublicT GetVar () {returnvar; } Public voidSetVar (T var) { This. var =var; } PublicString toString () {return This. var.tostring (); } }classhello{ Public Static voidMain (string[] args) {info<String> demo1=NewInfo<string>(); Info<String> demo2=NewInfo<string>(); Demo1.setvar ("Rollen"); Demo2.setvar ("Holt"); Printadd (Demo1, Demo2); } //all of the same string types are passed here . Public Static<T>voidPrintadd (info<t> demo1, info<t>Demo2) {System.out.println (Demo1.getvar ()+" "+Demo2.getvar ()); Otherwise, it looks like this: An error has occurred:/** * @authorRollen-holt The type of an incoming parameter using a generic pass-through*/ classInfo<t> { PrivateT var; PublicT GetVar () {returnvar; } Public voidSetVar (T var) { This. var =var; } PublicString toString () {return This. var.tostring (); } }classhello{ Public Static voidMain (string[] args) {info<String> demo1=NewInfo<string>(); Info<Integer> demo2=NewInfo<integer>(); Demo1.setvar ("Rollen"); Demo2.setvar (30); //Error called herePrintadd (Demo1, Demo2); } //all of the same string types are passed here . Public Static<T>voidPrintadd (info<t> demo1, info<t>Demo2) {System.out.println (Demo1.getvar ()+" "+Demo2.getvar ()); }} Exception in thread"Main"java.lang.Error:Unresolved Compilation Problem:the method Printadd (Info<t>, info<t>) in the type Hello was not applicable forThe arguments (INFO<STRING>, info<integer>) at Hello.main (Hello.java:26)
Java Generic Instances