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

Why is not working

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers, i am new to post but i almost read the forum daily. and to come to my question,
why is the line commented as "This line" not working

import java.util.*;
class Animal {}
class Dog extends Animal {}
class Cat extends Animal {}
class generics4 {
public void addAnimal(List<? super Dog> animals) {
animals.add(new Dog());
animals.add(new Animal());//This line
}
public static void main(String[] args) {
List<Animal> animals = new ArrayList<Animal>();
animals.add(new Dog());
animals.add(new Dog());
animals.add(new Cat());
generics4 doc = new generics4();
doc.addAnimal(animals);
}
}
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The "animals" parameter, is a list of some unknown type that could either be a Dog, Animal, or Object. This doesn't mean that the list can hold Dog, Animal, or Object instance -- it just means that Java doesn't know what it is... But is still require to type check it, and not allow invalid types for the list.

So, since Java doesn't know what it is, and must type check it... it has two options. One, it can allow only objects that IS-A Dog, IS-A Animal, and IS-A Object (all the possible types at the same time). Or Two, it can reject the operation. At "this line", it took the second option, at the line before "this line", it took the first option.

Henry
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to javaranch temesgen kahsay. If you post your code using the code tags and provide a more meaningful subject line, your question will attract more responses. Have a nice time here
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
temesgen please Use Code Tags when you post a source code. You can edit your message using button and then add code tags to it. Also please Use A Meaningful Subject Line when you start a topic...
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:
So, since Java doesn't know what it is, and must type check it... it has two options. One, it can allow only objects that IS-A Dog, IS-A Animal, and IS-A Object (all the possible types at the same time). Or Two, it can reject the operation. At "this line", it took the second option, at the line before "this line", it took the first option.

Henry



... what's the criteria for selecting any of these two options?

Thanks
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Byju Joy wrote:
... what's the criteria for selecting any of these two options?

Thanks



What "criteria for selecting"? Either the instance passed is *all* of the class types that can satisfy the generic, or it is a compile error.

Henry
 
Byju Joy
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic