• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Generis (super and extends)

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why this not works



if this works



explain kindly ?
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I suggest you to go thru the k&B 1.5 book, its explain this very nicely.

I can just give you some hints that

<T super Number> means T is a super class of Number, actually which is not correct, so it can not work.

The abstract class Number is the superclass of classes Byte, Double, Float, Integer, Long, and Short.

<T extends Number> means Number is a super class and T is extending that class, so it is valid and we can pass T as Interger, Long etc.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sarwar,



class GenTest<T super Number> {
T num;
public T checkNumber(T n) {
return n;
}
}



There is compiler error, because it is senseless, we are not
allowed to write "class GenTest<T super Number>{}" or so.
Let us see why?
You know compiler does type erasure so the type erasure of your
class definition would have been become(if compiler provided that),



You can see that the ultimate type erasure would be Object,
you couldn'y have called the instance methods of the Number
where as <T extends Number> you can call the instance
methods of the Number because the type erasure is <Number>
(the left most bound).

In the "super" case when after the type erasure the type becomes
Object, the same trouble of casting to Number, when you require to
call Number specific method although you could have called the
Object instance method simply, but what could have come in your
hand. Nothing!!!

Other case, you have not been able to instantiate the class like:

GenTest<Double> d1 = new GenTest<Double>(); or
GenTest<Integer> d2 = new GenTest<Double>();

That is because it would be class GenTest<T super Number> {}
What is its need if only you are allowed to do :

GenTest<Number> d1 = new GenTest<Number>(); and
GenTest<Object> d2 = new GenTest<Object>();

That is why we are not provided with <T super Number> like senseless things.

Hope you got the above verbose details,

Thanks and Regards,
cmbhatt
[ April 06, 2007: Message edited by: Chandra Bhatt ]
 
saqib sarwar
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Chandra Bhatt

you are a Generics Genius
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can also read one of Angelika Langer's FAQs (#107)
 
He does not suffer fools gladly. But this tiny ad does:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic