• Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubt in Generics...  RSS feed

 
Thangaraj Selvamani
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

1)
import java.util.*;
public class gen{
public static void main(String[] str){
List<? extends Number> list1 = new ArrayList<Integer>();
}
}

Number Is Super Class For Integer..Why This Code Shows Compile Error...


2)
import java.util.*;
class animal{
}
class dog extends animal{
}

class test{
public static void main(String[] str){
List<? extends animal> lst = new ArrayList<dog>();
}

}

But This Second Code Is Not Showing Compile Error
 
Remko Strating
Ranch Hand
Posts: 893
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With your first and second code I don't get a compile error. What is your fault message?
 
Santiago Bravo
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nothing wrong with 1) or 2)
 
Christina Pong
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I compiled both codes with eclipse; they were fine.
 
pradeepta chopra
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No compiler errors
 
Ankit Garg
Sheriff
Posts: 9610
37
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thangraj please tell the error that you are receiving as there is no compilation error as everyone is saying...You may have posted incomplete code... If you try to add any element to this list, then it will give you an error...
 
Thangaraj Selvamani
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry to late Reply... The Error Which Im getting is

C:\SCJP>javac gen.java
gen.java:4: incompatible types
found : java.util.ArrayList<Integer>
required: java.util.List<? extends java.lang.Number>
List<? extends Number> list1 = new ArrayList<Integer>();
^
1 error
C:\SCJP>java -version
java version "1.5.0-beta2"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta2-b51)
Java HotSpot(TM) Client VM (build 1.5.0-beta2-b51, mixed mode, sharing)

Please Tell Me Why This Error Comes For First Program.... But Second Program Is Working Correctly...Does Any Thing Problem With Version...

[ November 04, 2008: Message edited by: Thangaraj Raj ]
[ November 04, 2008: Message edited by: Thangaraj Raj ]
 
Remko Strating
Ranch Hand
Posts: 893
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try a different java version. I use 1.6.0.7.
 
Thangaraj Selvamani
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im Preparing For Java5(Scjp)..... But According To That It Must Not Give Compilation error??...
 
Ankit Garg
Sheriff
Posts: 9610
37
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thangraj did you create any class of your own with the name Integer??? Also the version of your JDK is Beta. So try a newer version...
 
Thangaraj Selvamani
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg...I have Not Created Integer Class..Ya I Tried In Other Version...its working...Correctly...What Will Be The Reason Not To Work In JDK1.5(Beta)...
 
Ankit Garg
Sheriff
Posts: 9610
37
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well Beta is a testing stage of a software's life cycle. There are bugs in software at Beta stage (there are bugs in final softwares too ). So it must be a bug...
 
Thangaraj Selvamani
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ankit Garg.. To Cleare My Doubt...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!