Writing a class

Report a typo

Three classes are given:

class Book {
}

class Brochure extends Book {

    private String name;

    Brochure(String name) {
        this.name = name;
    }

    String getName() {
        return name;
    }

    void setName(String name) {
        this.name = name;
    }
}

public class Main {

    public static void main(String[] args) {
        Brochure brochure = new Brochure("NewBrochure");
        Shelf<Brochure> shelf = new Shelf<>();
        shelf.setElement(brochure);
        System.out.println(shelf.getElement().getName());
    }
}

Write a generic Shelf class which will have a field, containing a type of object, and methods getElement() and setElement(). The first method has to return an object which is contained in Shelf (which is assigned to the field), the second one is to assign a passed object to the field. The class Shelf must accept only different types of books (subtypes of Book class)!

Write a program in Java 17
//write code of the Shelf class here
___

Create a free account to access the full topic