Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I need explanation on this java code

 
henry joe
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given:



 
Konstantinos Alvertis
Greenhorn
Posts: 11
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Henry,

Line 4 : The generic class Carpet accepts as bounded type , type B or subclass type of B (for example C). So V generic type can be B type or subclass of B type.

Line 5 : You have a generic method with generic type <X extends V>(which then applies to the argument) and return type Carpet<? extends V> :

I) So you declare the generic type of the method <X extends V>, which means X is of type B or subclass of B ( according to V parameter type i described above)

Then the <X extends V> applies to the argument of the method :
II) Carpet< ? super X> means that accepts everything that is of type X or super type of X . ( X can be B type or sublcass of B according to I) )

So merge I) and II) the argument can be only of type B. ( the argument is Carbet<B> e)

Line 5: For the return type Carpet<? extends V> means that accepts as return type every subclass of V or V , so B or subtype of B.
 
henry joe
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Could you give me a link to where I can fully study and understand these generic types? Also, since they are of subtype B or type B doesn't that makes them of type A as well since B extends class A?
Also, you said this "Line 5 : You have a generic method with generic type <X extends V>(which then applies to the argument) and return type Carpet<? extends V> : "
I don't quite understand it. What I know basically is you have a method and a return type. what I think you mean now is there is a method(generic method) and a return type(Carpet<? extends V>) but this generic method has a generic type( <X extends V>)? Am I correct?
 
Konstantinos Alvertis
Greenhorn
Posts: 11
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a link : http://docs.oracle.com/javase/tutorial/java/generics/ .

In line 5 , first you find what types X can hold with <X extends V>.

Then you use X in the argument Carpet < ? super X>.
 
henry joe
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks alot.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic