Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Generic doubt

 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Code is from javabeat

In the above code, if i uncomment //1 then it is compilier error. My doubt is, we can add anything into s (set of Objects), then why can't hs? (HashSet is subclass of Object)
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Generics conventions are different.
Object o = new String(); // Polymorphic assignment is Ok.
List<Object> lo = new ArrayList<String>() // Polymorphic generic assignments are not considered .

You can use,
List<? extends Object> lo = new ArrayList<String>();

For more information of on generics usage,
1) refer to K&B book
2) Sun Core java book, Generics chapter
 
Ranch Hand
Posts: 893
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because the generic types don't match. Polymorphism doesn't work for generic types.

If you created a new HashMap<Object> refererence then the assignment should work. See the code example below

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


I thought //1 does not work because HashSet "s" is designed to take only Object, but you are trying to point it to a HashSet which only takes String?


[HENRY: Formatted Code]
[ March 18, 2007: Message edited by: Henry Wong ]
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.util.*;class jb1
{ public static void main(String... args) {
HashSet<String> hs = new HashSet<String>();
hs.add("scjp");
hs.add("exam");
HashSet<Object> s = new HashSet<Object>();
s.add("check");
s.add(new Integer(5));
// s=hs; //1

java.util.HashSet<java.lang.String> and java.util.HashSet<java.lang.Object> are incompatible types.Both are completely different.So we cannot assign s=hs;

regards
SADASIVAKUMAR UTTI
SCJP1.4
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic