• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Super in Generics PROBLEM

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

Super in Generics by Malatesh Karabisti




i know that this question has been answered but still i am not convinced


why does this line give compiler error ?
Animal1 is superclass of Horse1,so why cannot Animal1 be added.


similarly,why line 16,17 give complier error ?


i saw the final code by Malatesh Karabisti


why is casting required in the below code ?


since list is of <Horse1> generic type then why such casting

TAKE FOR eg:

here no casting is required,then WHY IN THE ABOVE CASE
 
Mohit G Gupta
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
CAN ANYONE ANSWER MY QUERY ??

PLEASE MODERATORS HELP ME
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Regarding the second doubt, you need to cast because though the actual list is of type List<Horse1>, it has been declared as List<? super Horse1> which is not same as the type of animalHorse1111 (which is List<Horse1>).
 
Bartender
Posts: 543
4
Netbeans IDE Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You assigned it to a List<? super Horse1>. The compiler has to be sure about generics at compile time. So when you assign something to a List<? super Horse1>, the compiler knows that your List could be any supertype of Horse1 (Meaning Horse1, Animal1 or Object). Now, you can add Horse1 or any of its subtypes (because they can all fit in a List<Horse1> or a List<Animal1> or a List<Object>, but you CANNOT add a supertype of Horse1, because the compiler isn't sure that the collection you passed can take a supertype of Horse1.

Imagine the following:


and casting was required because he was using an Object reference before. You can cast that List referred to by an Object reference to a List of any type, which is dangerous at runtime, but part of the downsides of making sure non-generic code doesn't break.
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mohitkumar gupta wrote:CAN ANYONE ANSWER MY QUERY ??

PLEASE MODERATORS HELP ME



Please read this http://faq.javaranch.com/java/KeepItDown

Also where did you get that question from? http://faq.javaranch.com/java/QuoteYourSources
 
Mohit G Gupta
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.





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


----------------------------


2.why do line 22 ,21 give error ?


list is of Horse Type,then why such error

-------------------------

3.why the casting is required in the line 42 but not in 28?
 
Divyeshh Patel
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


Passing the list as an argument and adding something to the list are two different things, aren't they?

mohitkumar gupta wrote:
2.why do line 22 ,21 give error ?


list is of Horse Type,then why such error


No doubt, list is of type Horse, but has been declared as <? super Horse> so the only way to access it is (Object o:list)

mohitkumar gupta wrote:
3.why the casting is required in the line 42 but not in 28?


Since Object is the supertype of all objects, you can assign anything to Object without a cast which is not the case with line 42
e.g.
 
Mohit G Gupta
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i compiles and ran the TestGeneric1 program and got the folllowing errors:
-------------------------------------------------------------------------------------------

C:\Documents\Jcreator FIles\TestGeneric1.java:55: inconvertible types
found : java.util.List<capture#955 of ? super Horse1>
required: java.util.List<Horse2>
List<Horse2> animalHorse2222=(List<Horse2>) list;
^
C:\Documents\Jcreator FIles\TestGeneric1.java:57: inconvertible types
found : java.util.List<capture#442 of ? super Horse1>
required: java.util.List<Dog1>
List<Dog1> animalDog1111=(List<Dog1>) list;

^
C:\Documents\Jcreator FIles\TestGeneric1.java:59: inconvertible types
found : java.util.List<capture#822 of ? super Horse1>
required: java.util.List<java.lang.String>
List<String> animalString=(List<String>) list;


-------------------------------------------------------------------------------------------


i have just inserted import java.util.*;
 
Ranch Hand
Posts: 153
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
mohitkumar,

Thanks for digging the question more.

i compiles and ran the TestGeneric1 program and got the folllowing errors:


The code compiles in eclipse IDE but not with command line compiler. just comment the code after 55
and check, for me it worked. since there is Run time error in line 52 the code won't reaches to line number
55 and hence eclipse could not spot the error in Runtime. I don't know why it failed to notice while typing(compile time)
and if you have eclipse IDE just check how it behaves.


Let me come to your question

why the casting is required in the line 42 but not in 28?


Line 28 Object Objtye=list;
Line 42 List<Horse1> animalHorse1111=(List<Horse1>) list;



This time I compiled with command line compiler it should work

// All Java Gurus there please correct me If I am wrong any where

 
look! it's a bird! it's a plane! It's .... a teeny tiny ad
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic