+ 2

If class is generic class Phone<TYPE> and has TYPE variable; and method setVariable(TYPE var){variable = var} and another class

Has method justNameItSome(Phone<?> P){ P.setVariable("string"); } I know I will get error but how to solve that. What is the meaning of using ? To make method generic .

18th Oct 2019, 1:30 PM
Smit Kalkani
Smit Kalkani - avatar
3 ответов
+ 2
Thank you Mike for really good answer.☺️
18th Oct 2019, 4:54 PM
Smit Kalkani
Smit Kalkani - avatar
+ 1
Mikka your answer is perfectly right... Can you explain why to use extends and super for set and get data... So if my college professor ask me this in my Viva exam I can explain him I hate him no offense 😂
18th Oct 2019, 5:03 PM
Smit Kalkani
Smit Kalkani - avatar
+ 1
you know that you will set String type, so just require String class Phone<TYPE> { TYPE variable; void setVariable(TYPE var) {variable = var;} } class Another { void some(Phone<String> p) { p.setVariable("string"); } } class Program { public static void main(String[] args) { var ps = new Phone<String>(); new Another().some(ps); System.out.println(ps.variable); } } -------- but if you want with void some(Phone<? super String> p) { you get possibility to use Phone<Object>
19th Oct 2019, 12:52 AM
zemiak