File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes I need explanation on this java code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "I need explanation on this java code" Watch "I need explanation on this java code" New topic
Author

I need explanation on this java code

henry joe
Ranch Hand

Joined: Jul 24, 2012
Posts: 32
Given:



Konstantinos Alvertis
Greenhorn

Joined: May 05, 2013
Posts: 11

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.


OCPJP6
henry joe
Ranch Hand

Joined: Jul 24, 2012
Posts: 32
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

Joined: May 05, 2013
Posts: 11

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

Joined: Jul 24, 2012
Posts: 32
Thanks alot.
 
wood burning stoves
 
subject: I need explanation on this java code