• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Generics

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am having hard time understanding generics.
Could someone explain why line 2 compilation fails.??


}
Assuming Dog extends Animal
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Minu Sam wrote:I am having hard time understanding generics.
Could someone explain why line 2 compilation fails.??


}
Assuming Dog extends Animal




Simply, lower bounds are not supported with generic methods. That's how it is defined.

Henry
 
Minu Sam
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Henry,

Appreciate your response, however can you please explain what you mean by lower bounds?I still dont understand
 
Henry Wong
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Minu Sam wrote:
Appreciate your response, however can you please explain what you mean by lower bounds?I still dont understand



Java generics allow you to bound the type. You can specify an upper bound via <T extends UpperBound>, or you can specify a lower bound via <T super LowerBound>. With generic methods, only upper bounds are supported.

Henry
 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This wont' compile because p can be Animal, Animal's super type or Object.
What happens if a programmer does this inside addAgainWithSuper method :
List<Animal> l = new ArrayList<Animal>();
l.add(p);

?
What happens if p is an object? The list l cannot add p to it.

Or what happens if a program writes this:
Animal a = p; //where p is an object.
It will have type mismatch compilation error.

Since the programmer can write code in the method that may violation compilation rules or not type safe, the compiler won't let the addAgainWithSuper to be compiled as p may be Animal, Animal's super type or even object. During compilation time, the compiler does not know what type of p the programmer will pass as the argument.

Correct me if I am wrong.
reply
    Bookmark Topic Watch Topic
  • New Topic