I have couple of questions regarding Generics.
Lets take the code below.
Output :
washington
fuji
washington is red apple
fuji is red apple
apple is washington
apple is not washington
apple is not washington
apple is not washington
apple is not fuji
The questions are
1) In the above code declaration of class 'Washington' is
Washington<W extends Apple>
What does it mean ?? Is it mean class 'Washington' can hold only objects of type Apple (and not its subclasses example RedApple).
2)What if I replace it with
Washington<W extends Apple<W>>
then why the statement in main method
Washington<Apple<
String>> wash = new Washington<Apple<String>>();
Dont work ??
3)Why I have to take 'Apple' as raw type ?? Is it mandatory to always use raw types in such cases when class type is used as bounded type ?
4) In the main method twice RedApple object 'redapple' has been passed to Washington class method CheckWashington() as in statements
wash.CheckWashington(redapple); and
Washington<RedApple<String>> wash2 = new Washington<RedApple<String>>();
wash2.CheckWashington(redapple);
Both the times the output is "apple is not washington". Why here subclass object 'redapple' has not been upcasted to superclass 'apple' ??? In non-generic classes in a method where superclass object is needed as an argument and subclass object has been sent then it will be upcasted to superclass.
4) How many classes can be used as bounded types ?? As shown in code above the declaration of class fuji is
class Fuji<Apple, F extends Apple>
and the statement in the main method
Fuji<Apple<String>,RedApple<String>> fuji = new Fuji<Apple<String>,RedApple<String>>();
fuji.CheckFuji(app2, redapple);
as two objects 'app2' and 'redapple' belongs to Apple and RedApple classes respectively it was not possible to access their type <A> in case of Apple and <R> in case of RedApple. As shown in code below.
In the Washington class we have 'if' statement as
if(w1.a == "washington")
Same in the class Fuji we have if statement as
if(app1 == "fuji" || f1 == "fuji")
Why in the above if statement i cant access 'app1.a'. ??
5) Also the declaration of fuji class
class Fuji<Apple, F extends Apple> Whats the second type parameter actually means here ?? Does it mean that F could be objects of both Apple and its subclass ?
Thanks in advance for reading a long post and the reply.