aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why is not working Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply 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: 16692
    
  19



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: 6588
    
    1

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 - SCJP Mocks - SCJP 5 Mock exam (Word document ) - SCJP 5 Mock exam in Java.Inquisition format
Ankit Garg
Saloon Keeper

Joined: Aug 03, 2008
Posts: 9189
    
    2

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: 16692
    
  19

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://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Why is not working
 
Similar Threads
Doubt in generics
Generics.
Generics problem- why not taking Animal, Object and super of Dog
Generics and ? symbol
Generics Question