• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generic types on methods and classes

 
O. Ziggy
Ranch Hand
Posts: 430
Android Debian VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 185
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 430
Android Debian VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Surely you cannot have:



Compiler will moan that T is not defined?
 
O. Ziggy
Ranch Hand
Posts: 430
Android Debian VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
C++ Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
C++ Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 430
Android Debian VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic