• 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Generics doubt

 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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());
}

}
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic