0

Well I have a Collection of Books that I need to use to write methods from an Interface. But I don`t know how to access the attributes or methods from the object inside this collection.

I tried to implement like this but its says "type mismatch: cannot convert from element type Object to Book".

public class MyCozyLibrary implements SmallLibrary {

private Collection allBooks;

@Override
public Book getBook(BookTag tag) {
    for (Book book : allBooks) {
        if (tag == book.getTag()) {
            return book;
        }
    }

    return null;
}

1 Answers1

0

Use private Collection<Book> allBooks; instead.

Have a look at What are Generics in Java?

devgianlu
  • 1,547
  • 13
  • 25
  • I don`t know if I can change this part of the code, because I have the class with the sectors that I need to write the code. If there is any other way I`ll be glad. – Gabriel Vendramini Apr 19 '19 at 11:06