• 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

Generics Basic doubt

 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi i didnt get this point from java beat mock exam.

class GenTest<T super Number> {

T num;

public T checkNumber(T n) {

return n;

}

}

compilation fails as <T super Number> is invalid syntax
please throw some light on this concept.
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

The problem is when you say that your type has to be a super type of Number.
If you type the class as extends from the Number...everything goes fine.

What can be a super type of Number that you want as a class type?

There lies the problem.
Thanks.
 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java does not allow lower bound constraints on type parameters, so "<T super Foo>" is always invalid syntax. Lower bounds are only allowed for wildcards, e.g. "<? super Foo>". This limitation was imposed because of the way Java's type inference system works.
 
Sandip Sarkar
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kelvin,

Can you explain a bit more with code.
I got your point but no idea why java's type inference system doesnt allow this.

Thanks.
 
Kelvin Chenhao Lim
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The details are highly technical, and honestly I don't really understand it all either--it's been too many years since my last programming language theory course in college!

But if you're interested, check out this page in Sun's bug database, which contains this explanation from Sun:

See
http://lampwww.epfl.ch/~odersky/ftp/local-ti.ps

particularly section 3 and the last paragraph on page 9. Admitting
type variables on both sides of subtype constraints can result in a
set of type equations with no single best solution; consequently,
type inference cannot be done using any of the existing standard
algorithms. That is why type variables have only "extends" bounds.

Wildcards, on the other hand, do not have to be inferred, so there
is no need for this constraint.



The provided link is for "Inferred Type Instantiation Without Prototypes for GJ" (Postscript format), a paper on Generic Java (the predecessor of Java 1.5 generics). Warning: if you don't have a Computer Science background, much of the notation in that paper will probably look like gibberish to you.

I haven't had time to really make sense of the above information yet, so if someone else can translate it into a more layman-friendly description, that'd be most appreciated.
[ December 10, 2007: Message edited by: Kelvin Lim ]
 
Sandip Sarkar
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI KELVIN,

THANKS A LOT.

AM NOT A COMPUTER SCIENCE STUDENT BUT STILL WILL GIVE IT A TRY.



 
Time flies like an arrow. Fruit flies like a banana. Steve flies like a tiny ad:
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