• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem applying Generics  RSS feed

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!