• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem in a question about generic?

 
Sumit Khurana
Ranch Hand
Posts: 68
Chrome Java MyEclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9495
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Leon Omk
Ranch Hand
Posts: 75
Chrome Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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?
 
Ankit Garg
Sheriff
Posts: 9495
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
Chrome Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent explanation! Thanks Ankit Garg!
 
Sumit Khurana
Ranch Hand
Posts: 68
Chrome Java MyEclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 9495
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 68
Chrome Java MyEclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks ankit for your post...now i understood...
 
Tom Mark
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic