aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generic types on methods and classes 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 "Generic types on methods and classes" Watch "Generic types on methods and classes" New topic
Author

Generic types on methods and classes

O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430



The above syntax has confused me slightly as i never saw that syntax before

- What is the difference between <V extends B> and <? extends B>
- Is it legal to have the type defined at both the class level and method level. I thought you can only do one of the following but not both.

John Stark
Ranch Hand

Joined: Jul 19, 2011
Posts: 185
This is a question from the self assessment 2 of the K & B Practice exam book. I could not figure this one out even after looking at the answer. Would be nice if somebody could explain it. I found the self assessments quite frustrating. I did quite well in the first one and then in the book it says 'Now for the bad news ... We picked 14 of the easiest questions'. In the second self assessment I did pretty badly the book saying 'You need a LOT more studying' (after reading the K & B study guide twice ).

John
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

John Stark wrote:This is a question from the self assessment 2 of the K & B Practice exam book. I could not figure this one out even after looking at the answer. Would be nice if somebody could explain it. I found the self assessments quite frustrating. I did quite well in the first one and then in the book it says 'Now for the bad news ... We picked 14 of the easiest questions'. In the second self assessment I did pretty badly the book saying 'You need a LOT more studying' (after reading the K & B study guide twice ).

John


This is exactly how i felt! i got 11 out of 14 for the first assessment but then i got 4 out of 14 on the second assessment.

I think the questions are unecessarily difficult for the required level to pass. It is probably a good thing as it makes you put a lot of thought on the question but if you are only limited to one or two hours a day for the revision it is not very helpful.

I wonder if it is better to do few difficult questions or lots of intermediate level questions..
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1030
    
    5

Surely you cannot have:



Compiler will moan that T is not defined?
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

James Boswell wrote:Surely you cannot have:



Compiler will moan that T is not defined?


I was referring to declaring a generic method on a class that is not generic. Like this one.

Sebanti Sanyal
Ranch Hand

Joined: Nov 07, 2011
Posts: 58

James Boswell wrote:Surely you cannot have:



Compiler will moan that T is not defined?


Compiler here complains because return type of the method is missing. Just make it '<T> void method(){}' and it will be a generic method.
Sebanti Sanyal
Ranch Hand

Joined: Nov 07, 2011
Posts: 58

It is perfectly legal to have something like this.


What is the difference between <V extends B> and <? extends B>


Syntax like <? extends B> is used for reference variable declaration, method arguments and return type.
While defining generic class and methods, you can't use '?', have to go for something like <V extends B>.
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

Sebanti Sanyal wrote:It is perfectly legal to have something like this.


What is the difference between <V extends B> and <? extends B>


Syntax like <? extends B> is used for reference variable declaration, method arguments and return type.
While defining generic class and methods, you can't use '?', have to go for something like <V extends B>.


Thanks Sebanti. I wasnt aware of that..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generic types on methods and classes