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 problem in a question about generic? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "problem in a question about generic?" Watch "problem in a question about generic?" New topic
Author

problem in a question about generic?

Sumit Khurana
Ranch Hand

Joined: Sep 19, 2010
Posts: 68

Question from ExamLab

abstract class A<K extends Number>{
//Insert here
}
which are the valid method declarations for above class??

These are the two answers among the all..

a) public abstract <K> A<? extends Number> useMe(A<? super K> k);
b) public abstract <K> A<? super Number> useMe(A<? extends K> k);

i am not able to understand these two..

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

These methods declare their K which is different from the K defined in the class. You can write these methods as
If you are confused over something else, let us know...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Leon Omk
Ranch Hand

Joined: Aug 17, 2010
Posts: 75



I don't understand why there is no compile error on A<? super Number>?

Class A only accepts types which extends Number, how could this method return an A instance which might contain a super type of Number?


OK, so that other guy knows Java better than I do, but I bet he can't speak Wuhanese(a Chinese Dialect) like me.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

how could this method return an A instance which might contain a super type of Number?

No it can't. Since the method's return type is A<? super Number>, this method can return only an instance of type A<Number> as anything down that hierarchy (eg. A<Integer>) won't fit the return type and anything up the hierarchy can't be declared/instantiated (eg. new A<Object>() would result in a compile time error)...
Leon Omk
Ranch Hand

Joined: Aug 17, 2010
Posts: 75

Excellent explanation! Thanks Ankit Garg!
Sumit Khurana
Ranch Hand

Joined: Sep 19, 2010
Posts: 68



i was facing with the same problem which was asked by Leon. But now i am cleared with it your(ankit) explanation.

My next problem is about the generic type declaration of method <M>

i am writing what i think about the method tell me whether i am right or wrong..



<M> means a new generic for this method whose type is determined through the value we pass into this method.A<? extends Number> says that this method can return any object which is below the heirarchy of Number, same is said by the class A.so,there is no problem.
i am not able to grasp the concept which is written between the paranthesis.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

Since the class A can be typed Number or its sub-classes, so restrictions will apply to M based on that. M can be a sub-class of Number like Integer or Number itself. So you can for example pass the method A<? super Integer> in which case M will resolve to Integer. The actual implications of what M resolves to will be easier to understand if you take a collection class into consideration instead of A...
Sumit Khurana
Ranch Hand

Joined: Sep 19, 2010
Posts: 68

Thanks ankit for your post...now i understood...
Tom Mark
Greenhorn

Joined: Jun 15, 2011
Posts: 27
Hello, I've been searching for an answer in JAVA forum for many hours, but I didn't find it.
Can anyone explain me how does the code you wrote above works?

abstract class A<K extends Number>{
//Insert here
}
1. a) public abstract <K> A<? extends Number> useMe(A<? super K> k);
2. b) public abstract <K> A<? super Number> useMe(A<? extends K> k);

As I understand A<? extends Number> in the method declaration is a type of a return method, am I wrong?
But what is <K> (again I'm talking about the method), why do we need that, especially in brackets? Where is it comes from?
Can you give an example that might help me ?

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem in a question about generic?