Super in Generics by Malatesh Karabisti
OCPJP 6.0 93%
OCPJWCD 5.0 98%
OCPJP 6.0 93%
OCPJWCD 5.0 98%
Cheers,
Divyesh.
Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
mohitkumar gupta wrote:CAN ANYONE ANSWER MY QUERY ??
PLEASE MODERATORS HELP ME
KB book PG-619 GENERIC METHODS
Regarding your first problem, ? super Horse1 allows you to add objects to the list but only the subtypes of Horse1(and of course, Horse1). You cannot add anything which is above Horse1 in the inheritance hierarchy. That is the reason for compilation error.
If you use List<? super Horse1> as a method argument, that would mean you can pass any List declared with Horse1(and any other class from the hierarchy between Object and Horse1) as the generic type. e.g. List<Object>, List<Animal>, List<Horse1>, etc
Hope this clears your first doubt.
OCPJP 6.0 93%
OCPJWCD 5.0 98%
mohitkumar gupta wrote:
in the above code,animals of type is passed to addAnimal method that is supertype of Dog.then why not in the First Problem i posted
mohitkumar gupta wrote:
2.why do line 22 ,21 give error ?
list is of Horse Type,then why such error
mohitkumar gupta wrote:
3.why the casting is required in the line 42 but not in 28?
Cheers,
Divyesh.
OCPJP 6.0 93%
OCPJWCD 5.0 98%
i compiles and ran the TestGeneric1 program and got the folllowing errors:
why the casting is required in the line 42 but not in 28?
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |