• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

Generics

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi guys,could you please help to solve Error in the Following Programme..


import java.util.*;


class Vehicle
{
public String toString()
{
return "Vehicle";
}
}

class Car extends Vehicle
{
public String toString()
{
return "Car";
}
}
class Van extends Vehicle
{
public String toString()
{
return "Van";
}
}
class Subaru extends Van
{
public String toString()
{
return "Subaru";
}
}

class A
{
void m(List<? extends Vehicle > Vehicles)
{
for(Vehicle v:Vehicles)
{
System.out.println(v);
}
Vehicles.add(new Car());


}
public static void main(String args[])
{
List<Car> list=new ArrayList<Car>();

list.add(new Car());
list.add(new Car());
list.add(new Car());
System.out.println(list.size());
new A().m(list);
System.out.println(list.size());
}
}

 
Ranch Hand
Posts: 59
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Sirojan,

Please use CodeTags to make code more readable.

The error is due to the addition of a Car you are doing in the method void m(List<? extends Vehicle > Vehicles).
Please have a look at this particular discussion to know more. Matthew Brown's posts in particular.

More About the same topic
 
Ranch Hand
Posts: 40
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Object cannot be added in the method to the List which is declared as List<? extends Vehicle> in method declaration.
The reason,
1) If the method parameter is declared as List<? extends Vehicles> this means that any object type which is subclass of Vehicles can be added to List.
so here the method m() can be called using List<Car>, List<Van>, List<Subaru>

2) Now consider a scenario
method m() declared same as in your code m(List<? extends Vehicles> Vehicle)
and inside method to the list Vehicle object of type Car is added

and while calling method m, list passed as parameter is m(List<Van>) as I exlpained in point 1) which is perfectly valid

Here the object type added in the list and the object type used to call the method contradicts (adding Car and called using Van)

hence the java compiler does not allows to add objects to the collection in the method whose parameter is declared as <? extends ClassType>
 
Ranch Hand
Posts: 384
MyEclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi buddy,

the problem is ---- Vehicles.add(new Car()); in A class.

you cannot add a car object on the List<? extends Vehicle > reference. you can ONLY get the things out of it.
So remove that line . and code works fine.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic