You are here:

Java/Query regarding parameter passing in a list which is generically declared as

Advertisement


Question
QUESTION: Please read the code below.

class Basic {
  String name;
   List< ? extends Basic> list;
   public Basic(){}
  public Basic(String name) {
     this.name = name;
  }

  public String toString() {
     return name;
  }
   
  public void add(Basic x)
  {
  list.add(x);
  }
  
  public String getName() {
     return name;
  }

  public void setName(String name) {
     this.name = name;
  }

  public List<? extends Basic> getList() {
     return list;
  }
  
  public void setList(List<? extends Basic> list) {
     this.list = list;
  }

}
class Country extends Basic
{
public Country(String name)
{
  super(name);
  list = new ArrayList<State>();
}
}
class State extends Basic
{
public State(String name)
{
  super(name);
  list = new ArrayList<State>();
}
}

//********************************************************


In the 'Basic' class,for the method ,

public void add(Basic x)
  {
  list.add(x);
  }

x of type basic is being reported as error. How should the parameter be properly passed such that all the subclasses of type basic be inserted to the list in the superclass' function definition itself.
Is there any way to allow such additions.

ANSWER: Do you mean:
List<? super Basic> list;

Which means it can be any super class of Basic including List<Object>.

If you do mean List<? extends Basic>, then you cannot add non-null elements because you could be trying to add an invalid member to the list. E.g. if you have Advanced as a subclass of Basic, then you could not add Basic elements to a List<Basic>.

---------- FOLLOW-UP ----------

QUESTION: Ok. Thank you.

But,if I do this.

class SomeClass{}
class Basic extends SomeClass{
//Class definition same as above
}

then shouldn't this work ?

public void add(Basic x)
{
list.add(x);
}

Please tell what is wrong here.

Answer
The reason it doesn't work is that it could be any subclass of Basic, not necessarily the one you put in. Simply put if you have "? extends Basic" then you cannot add any element to the set except for null, since null is assignable to any class.

What you can do with ? extends Basic is:

List<SomeClass> list = new ArrayList<SomeClass>();
List<? extends Basic> list2 = list;

which would basically let you do:

Basic basic = new Basic();
basic.setList(list);

This was the Basic class can handle any list that has Basic or some subclass as its element type.

Generally classes that use "? extends Something" will perform operations on elements in the list that are Something.

For example, you could have List<? extends Number> as a parameter. Then, you could have:

public static double sum(List<? extends Number> list) {
  double sum = 0.0d;
  for(Number number : list)
     sum += number.doubleValue();
  return sum;
}

This way you could pass in a List<Double>, List<Integer>, List<Float>, ....

Java

All Answers


Answers by Expert:


Ask Experts

Volunteer


Artemus Harper

Expertise

I have a Masters in computer science. I can answer questions on core J2SE, swing and graphics. Please no questions about JSP or J2ME.

Experience

I have experience in Core Java, good background in Java swing/gui, some experience with JNI, Java reflection. Some experience in bio-informatics. Basics in c++ and c#

Organizations
Washington State University

Education/Credentials
MS in Computer Science from Washington State University and a BS in Mathematics and Computer Science from Central Washington University.

©2016 About.com. All rights reserved.