This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why is not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why is not working" Watch "Why is not working" New topic
Author

Why is not working

temesgen kahsay
Greenhorn

Joined: Jun 08, 2008
Posts: 4
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
Sheriff

Joined: Sep 28, 2004
Posts: 18138
    
  39



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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6657
    
    5

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


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Byju Joy
Ranch Hand

Joined: Sep 06, 2005
Posts: 84
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
Sheriff

Joined: Sep 28, 2004
Posts: 18138
    
  39

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

Joined: Sep 06, 2005
Posts: 84
Thanks Henry.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why is not working
 
Similar Threads
Generics.
Generics Question
Generics and ? symbol
Generics problem- why not taking Animal, Object and super of Dog
Doubt in generics