This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes Problem applying Generics Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Problem applying Generics" Watch "Problem applying Generics" New topic
Author

Problem applying Generics

adam smith ii
Greenhorn

Joined: Feb 04, 2010
Posts: 20
In Java tutorial, exercise is:
Design a class that acts as a library for the following kinds of media: book, video, and newspaper. Provide one version of the class that uses generics and one that does not. Feel free to use any additional APIs for storing and retrieving the media.

Current error is on line 13 the "storeMedia(b);" and other two similar lines. Error message, "method storeMedia in class Library<T> cannot be applied to given types, required T, found Library<T>.Book.

I have made many attempts trying to get clean compile. I think I am missing a basic concept so an explanation along with the code remedy would be appreciated. Thanks in advance.
steve claflin
Ranch Hand

Joined: Dec 04, 2008
Posts: 53
I think the problem is that you have defined Media and the implementing classes as inner classes of Library. Even though the class is parameterized with T, you are locking in within the class what T must be. One solution would be to move Media and the implementing classes outside of Library. Another approach would be to have Library not be parameterized, and to make the ArrayList field be ArrayList<Media>, which is what I think the intent of the exercise is.

Also note that your Newspaper does not implement Media.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Problem applying Generics
 
Similar Threads
generics...
generics
Generics
Generics
Generics