aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics doubt" Watch "Generics doubt" New topic
Author

Generics doubt

megha joshi
Ranch Hand

Joined: Feb 20, 2007
Posts: 206
Hi all,

The code below gives me a compiler error

Compiling 1 source file to Main.java:36: cannot find symbol
symbol : method add(Animal)
location: interface java.util.List<capture of ? super Dog>
l.add(new Animal());

-----------------------------------------
I cant understand the reason for it.Please help me.
-------------------------------------------------------------

import java.util.*;
class Animal{
Animal(){}
}
class Dog extends Animal//implements Comparable<Dog>

{
private String name;
Dog(String d)
{ this.name = d;}
public String toString()
{return this.name;
}


public boolean equals(Object o)
{
if( (o instanceof Dog) && ( ((Dog)o).name == this.name ) )
return true;
return false;
}
}
public class Main extends Thread
{


public static void main(String argv[])
{
List<? super Dog> l = new ArrayList<Animal>();

l.add(new Animal());
}

}
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Originally posted by megha joshi:
Hi all,

The code below gives me a compiler error

Compiling 1 source file to Main.java:36: cannot find symbol
symbol : method add(Animal)
location: interface java.util.List<capture of ? super Dog>
l.add(new Animal());

-----------------------------------------
I cant understand the reason for it.Please help me.
-------------------------------------------------------------

import java.util.*;
class Animal{
Animal(){}
}
class Dog extends Animal//implements Comparable<Dog>

{
private String name;
Dog(String d)
{ this.name = d;}
public String toString()
{return this.name;
}


public boolean equals(Object o)
{
if( (o instanceof Dog) && ( ((Dog)o).name == this.name ) )
return true;
return false;
}
}
public class Main extends Thread
{


public static void main(String argv[])
{
List<? super Dog> l = new ArrayList<Animal>();

l.add(new Animal());
}

}


The lowerbound is Dog which means that l can refer to a List of Dog, a List of Animal, or a List of Object.

The only object that can safely be added to the list is a Dog.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Megha,

You can look at the following code too!


package generics;

import java.util.*;

/**
*
* @author cmbhatt
*/
class Animal {

}

class Dog extends Animal {

}

class Beagal extends Dog {

}


public class DemoAnimal {

public static void main(String... args) {
List<? super Dog> lst = new ArrayList<Animal>();
lst.add(new Dog());
lst.add(new Beagal());

//See this also
//lst = new ArrayList<Dog>();//OK
//lst = new ArrayList<Beagal>(); //NO
/* So far add() method is concerned only Dog or subclass
*of the Dog can be added (take care the lower bound is Dog)
*lst reference can refer to Dog and anybody who is superclass
*of dog but add() method wont do like this.
*/

}

}


Regards,
cmbhatt


cmbhatt
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics doubt