Skip to content

Commit b240a7c

Browse files
Update README.md
1 parent 9950e89 commit b240a7c

1 file changed

Lines changed: 75 additions & 1 deletion

File tree

README.md

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5264,7 +5264,81 @@ public class Example <T extends List<Number>>{
52645264

52655265
<h3> <i> Here "Example eg = new Example();" is in raw type i.e. doesnot mention "Type" in " < > " operator. </i></h3>
52665266

5267-
<h3><i><ins>Sub Types:</ins></i> </h3>
5267+
<h3><i><ins>Sub Types:</ins></i> We can subtype a generic class or interface by extending or implementing it. The relationship between the type parameters of one class or interface and the type parameters of another are determined by the extends and implements clauses.</h3>
5268+
5269+
<h3 align="Left">
5270+
5271+
```
5272+
5273+
import java.util.ArrayList;
5274+
import java.util.List;
5275+
5276+
interface b2<T> extends List<T> { }
5277+
5278+
public class Example3<T extends b2<? extends Number>> {
5279+
5280+
T get(T t) {
5281+
t.forEach(System.out::println);
5282+
return t;
5283+
}
5284+
5285+
public static void main(String[] args) {
5286+
Example3<b2<Number>> e = new Example3<>();
5287+
Example3<b2<Integer>> e1 = new Example3<>();
5288+
List<Number> al = new ArrayList<>();
5289+
al.add(1);
5290+
al.add(2);
5291+
al.add(3);
5292+
System.out.println(e.get((b2<Number>) al));
5293+
5294+
List<Integer> al1 = new ArrayList<>();
5295+
al1.add(1);
5296+
al1.add(2);
5297+
al1.add(3);
5298+
5299+
5300+
System.out.println(e1.get((b2<Integer>) al1));
5301+
5302+
}
5303+
}
5304+
5305+
```
5306+
5307+
</h3>
5308+
5309+
<h3> <i> Hence: </i></h3>
5310+
5311+
<h3 align="Left">
5312+
5313+
```
5314+
interface b2<T> extends List<T> { }
5315+
5316+
Then:
5317+
5318+
class Example<T extends b2<? extends Number>>{
5319+
5320+
public static void main(String[] args) {
5321+
5322+
Example3<b2<Number>> e = new Example3<>();
5323+
Example3<b2<Integer>> e1 = new Example3<>();
5324+
Example3<b2<FLoat>> e1 = new Example3<>();
5325+
Example3<b2<Double>> e1 = new Example3<>();
5326+
5327+
}
5328+
5329+
}
5330+
5331+
i.e.,
5332+
5333+
List<Number> --> b2<Number>
5334+
List<Number> --> b2<Integer>
5335+
List<Number> --> b2<FLoat>
5336+
List<Number> --> b2<Double>
5337+
5338+
```
5339+
5340+
5341+
</h3>
52685342

52695343
</ul>
52705344

0 commit comments

Comments
 (0)