• 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:

Problem in using keyword "super" with generics

 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Animal{}
class Dog extends Animal{}
class Puppy extends Dog{}

public class Generic2 {
public static void main(String[] args) {
List <? super Dog>dogList = new ArrayList<Dog>(); //1
dogList.add(new Dog());
dogList.add(new Puppy()); //2
dogList.add(new Animal()); //3
}
}

<? super Dog> (at line //1 ) this says that List reference dogList can accept anything that is either a Dog or a superclass of Dog.
Then why does'nt it give an error when i am trying to add an object of class Puppy to it ? line marked //2
Also why does it give me an error when i am trying to add an object of class Animal to it ? line marked //3

Pl explain
 
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 Prasad,

List <? super Dog> dogList = new ArrayList<Dog>();

Means dogList can be added "Any Object which IS-A Dog".

1) Puppy IS-A Dog // Correct
2) Animal IS-A Dog // Need Not be.

Hope you got it.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all, first comment here in javaranch =).

Well, i have a dude in generics and wilcards.

The next is not allowed=
List<Animal> list = new ArrayList<Dog>()

Of course, polymorphism with generics get us to a nice compiler error.

But with wilcards, isnt it the same?

List<? extends Animal> list = new ArrayList<Dog>();
is ok, i think...

But with return types, is this ok?

public List<? extends Animal> maybeGetDogs(List<Animal>
{
...
}

List<Dog> dog = maybeGetDogs(animals);

And about the code wich wrote our friend in the first post, what does it throw? I think compiler error, because in runtime there arent generics...

-A pleasure write here.Thanks for all, and sorry for my english -spanish, lot of sun and beach and bulls, but bad english.
 
Prasad Shindikar
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Srinivasan,

i got it mate. thanks a lot
 
Srinivasan thoyyeti
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 Antonio,

you are correct.
Please find the reply relevant to your query.
https://coderanch.com/t/262080/java-programmer-SCJP/certification/Generic

Lets Program Better.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic