• Post Reply Bookmark Topic Watch Topic
  • New Topic

Generics: Polymorphism with generics  RSS feed

 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I was reading below link and was not clear on below commented portion of questions in bold

So what does it mean ?

The statement List<? extends Vehicle> means that you can pass any type of list that subtype of List and which is typed as Vehicle or some subtype of Vehicle. But you cannot add anything into the collection at all.

Now what is that? Why can’t we add anything into the collection?

(//which collection vehicle or bike or car???)


Lets consider this.



public void addVehicle(List<? extends Vehicle> vehicles){
vehicles.add(new Car());
}
The compiler stops you at the very moment. It doesn’t allow you to add anything while using ? extends. Why is that?

? extends allows us to pass any collection that is subytype of the method parameter which is typed as generic type of subtype of the generic type. i.e. we can pass an ArrayList<Vehicle> , ArrayList<Bike> or ArrayList<Car> to it.
But consider a scenario in which we are passing ArrayList<Bike> to addVehicle(List<? extends Vehicle>). In that case, if compiler didn’t stop us

(//i do not think compiler stopping right ??it is allowing vehicle, car, bike all 3 in case of extends???)


from adding into the collection, we would have added a Car into ArrayList<Bike>.
This is the scenario so ? extends doesn’t allow you to add into collection.
(//i thought we chose extends approach to allow vehicle and its subclasses right..i think compile time...but at run time type erasure kicks in which is bit unclear???)

But there is also a workaround for this. We can also pass a subtype collection and still be able to add into collection. We have to use the super keyword along with wildcard operator
//does it means we should always use super since extends has issues. Super is only for bike and its super classes right...it wont deal with subclasses at all??


i am bit confused here. Please advise
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

To start, Wildcards and Polymorphism are not the same here.

List<? extends Vehicle> means a list that can be List<Car>, List<Train>, List<Truck>, etc. etc. etc., but the compiler has no idea what the generic type is. It is *not* a list that can take anything that extends Vehicle !!

Henry
 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is *not* a list that can take anything that extends Vehicle !!

does not it takes bike, vehicle and also car here. I got super concept clear but not this extends concept w.r.t generics. Please advise

We will change the method to


1
2
3

After doing this,


1
2
3
mechanic.addVehicle(vehicles); // compiles fine
mechanic.addVehicle(bikes); // compiles fine
mechanic.addVehicle(cars); // compiles fine
So what does it mean ?

The statement List<? extends Vehicle> means that you can pass any type of list that subtype of List and which is typed as Vehicle or some subtype of Vehicle. But you cannot add anything into the collection at all.

i did not understand last sentence above like
But you cannot add anything into the collection at all.



everything is compiling fine though right as below which means it should also add right

mechanic.addVehicle(vehicles); // compiles fine
mechanic.addVehicle(bikes); // compiles fine
mechanic.addVehicle(cars); // compiles fine


Any good links, examples around these topics. Please advise
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does 1 2 3 mean? Don't try to add line numbers, because the code tags do so automatically.

You can have an add method and there is no way the compiler can know that you want to add anything to the List. It is only when you try to add something to the List with bikes.add(new Bike()); that there could be any confusion.
 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1 2 3 is accident in copy paste.



Please advise
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please show us the error messages.
 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When my code is as below











I get below error at line 29 of Mechanic class

The method addVehicle(List<Vehicle>) in the type Mechanic is not applicable for the arguments (List<Bike>)
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sai rama krishna wrote:
The method addVehicle(List<Vehicle>) in the type Mechanic is not applicable for the arguments (List<Bike>)


Your method expects a List<Vehicle>. You passed it a List<Bike>, which of course, is not what it wants.

Henry
 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
after changing to extends approach as below


i am getting below error at line 33 of Mechanic class which does not make sense to me.

Syntax error, insert ";" to complete Statement


i also see some weird issues when i copy paste the code and format it it is giving red lines. when i remove the space at the front of line that error is going away from eclipse. I never saw this kind of space or format error before.
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried your code at the command line and got no such compiler error.
 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when i tried to compile from command line i see below 100 errors

errors.png
[Thumbnail for errors.png]
err
 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when i removed every single space in front eclipse i happy.
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why did you remove all those spaces? Did you not try to find out what \160 means? 160 in octal is p and 160 in hex is Š, but 160 in decimal is \u00a0 which appears to be non‑breaking space. You can usually only get such characters into your code if you use a word processor. I don't know how you got á printed out.
Don't use word processors for programming because the can add such strange characters.
 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that website above code there are different options to copy code. Simple copy option is messing the code as above. when i selected 'new window java' then code is good without special spaces etc. But only thing is numbers ccoming at top which i can delete easily
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What website above? I didn't think we had such problems on this website.

Don't use online compilers.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!