• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why is not working

 
temesgen kahsay
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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);
}
}
 
Henry Wong
author
Marshal
Pie
Posts: 21226
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • 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
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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...
 
Byju Joy
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • 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
Marshal
Pie
Posts: 21226
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks Henry.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic