• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt on generics once again

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.util.*;
public class RentalGeneric<T> { // "T" is for the type
// parameter
private List<T> rentalPool; // Use the class type for the
// List type
private int maxNum;
public RentalGeneric(
int maxNum, List<T> rentalPool) { // constructor takes a
// List of the class type
this.maxNum = maxNum;
this.rentalPool = rentalPool;
}
public T getRental() { // we rent out a T
// blocks until there's something available
return rentalPool.get(0);
}
public void returnRental(T returnedThing) { // and the renter
// returns a T
rentalPool.add(returnedThing);
}
}

class TestRental {
public static void main (String[] args) {
//make some Cars for the pool
Car c1 = new Car();
Car c2 = new Car();
List<Car> carList = new ArrayList<Car>();
carList.add(c1);
carList.add(c2);
RentalGeneric<Car> carRental = new
RentalGeneric<Car>(2, carList);
// now get a car out, and it won't need a cast
Car carToRent = carRental.getRental();
carRental.returnRental(carToRent);
// can we stick something else in the original carList?
carList.add(new Cat("Fluffy"));
}
}

--Getting error---
javac1.5 RentalGeneric.java
RentalGeneric.java:38: cannot find symbol
symbol : method add(Cat)
location: interface java.util.List<Car>
carList.add(new Cat("Fluffy"));

Can you please explain?
I am really facing tuff time in understanding generics
 
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

could you please use the "code" tag and apply some formatting, it's not a lot of fun to read like that...

Thanks,
Alex
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you have declared your "carList" with a generic type <Car>,so it restricts the calls of "add" on carList such that you can add elements belongning to that type(Car) only! Because of the generic type <Car>, The list carList can have elements belonging to Car only (i.e its subtypes as well!).
 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Following topic might help you to understand the very basics of Generics..!!Understanding Generics.....

I Hope after reading this you will be able to answer your question..!
 
reply
    Bookmark Topic Watch Topic
  • New Topic