Forums Register Login

Problem applying Generics

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
Create symphonies in seed and soil. For this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1139 times.
Similar Threads
Generics
generics
generics...
Generics
Generics
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 02:10:42.