• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generic doubt like K& B book

 
Ram Reddy
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1)
package Test.Collections;

import java.util.List;

class GenericTest
{
public static void main(String...strings)
{
List<Integer> input = null;
List<Number> output = process(input);
//the method process(List<? super E> in the type GenericTest is
//not applicable for the arguments (List<Integer>
}
static <E extends Number> List<E> process(List<? super E> nums)
{
return null;
}
}

In the above program , i am getting the compile time error.Then i changed
List<Number> output = process(input); to List<Integer> output =
process(input);(Number Type to Integer). This compiles fine. I am not able to understand how it is treating. can somebody help me please?

2)
package Test.Collections;

import java.util.List;

class GenericTest
{
public static void main(String...strings)
{
List<Number> input = null;
List<Integer> output = process(input);
//Chnaged from List<Number> to List<Integer>. Compiles fine
}
static <E extends Number> List<E> process(List<? super E> nums)
{
return null;
}
}



//These programs comiles fine. please give me explanation for these.

3)
package Test.Collections;

import java.util.List;

class GenericTest
{
public static void main(String...strings)
{
List<Number> input = null;
List<Number> output = process(input);

}
static <E extends Number> List<E> process(List<? super E> nums)
{
return null;
}
}

4)

package Test.Collections;

import java.util.List;

class GenericTest
{
public static void main(String...strings)
{
List<Integer> input = null;
List<Integer> output = process(input);

}
static <E extends Number> List<E> process(List<? super E> nums)
{
return null;
}
}

thanks
Rami.
 
Hasnain Khan
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Rami,
Please refer to the following thread
http://www.coderanch.com/t/266819/java-programmer-SCJP/certification/Collections-Generics-Help

and

http://www.coderanch.com/t/266826/java-programmer-SCJP/certification/Generics-Quite

for calrification

Kind regards.
Hasnain Javed Khan.
 
Ram Reddy
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your reply hasnain.

My doubt is somewhat different. I slightely modified it.

I defined the method definition as
static <E extends Number> List<E> process(List<? super E> nums)
{
//E is defined as E extends Number and accepting argument
// as ? super E. Here i am not understanding
return null;
}

and i sent argument as

List<Integer> input = null;
List<Number> output = process(input);
here i am getting error as
the method process(List<? super E> in the type GenericTest is
not applicable for the arguments (List<Integer>

but same i changed to

List<Number> input = null;
List<Integer> output = process(input);//changed to Integer and sed Number as argument. Now i am not getting error.
Sorry guys if i am confusing you. But i am not able to understand this difference properly.
 
Hasnain Khan
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Rami,
Guys correct me if Im wrong

Ok Rami here is how it goes

First you look at the method argument which is defined as

List<? super E> nums

which means that any type that is a super class of E including the super class itself (Types above the hierarchy i.e Number and anything above it like Object).

Now you have defined E before the return type which is

<E extends Number>

which means that E is any subclass/implementation that extends/implements Number (Types below the hierarchy i.e Number and anything below it like Integer)

From your code you're sending a List<Integer>, but Number is the super class/interface of Integer, not Integer class itself. Thats why you are getting a compiler error.

Hope that is correct,clear and helped.
Kind Regards.
Hasnain Javed Khan.
 
Hasnain Khan
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and by hierarchy I mean Inheritance Tree
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic