Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics doubt

 
megha joshi
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • 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());
}

}
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • 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.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic