+ 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 .
3 Respuestas
+ 2
Thank you Mike for really good answer.☺️
+ 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 😂
+ 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>