---
# System prepended metadata

title: Inheritance Demo

---

```java
// Publication -> Research Paper, Book, Magazine

import java.util.ArrayList;

public class Main {
  public static void main(String[] main) {
    Book howToThink =
        new Book(
            "How to think like a computer scientist",
            new String[] {"Peter Wentworth", "Jeffrey Elkner"},
            "computer",
            "12345");

    System.out.println(howToThink.getTitle());
    System.out.println(howToThink instanceof Publication);
    System.out.println(howToThink instanceof Book);

    Student juan = new Student("Juan", "BSCS", "25-0001");

    juan.borrow(howToThink);

    for (Publication pub : juan.getItems()) {
      System.out.println(pub.getTitle());
    }
    System.out.println(howToThink.getTitle());
  }
}

class Publication {
  private String title;
  private String[] authors;
  private String genre;

  Publication(String title, String[] authors, String genre) {
    this.title = title;
    this.authors = authors;
    this.genre = genre;
  }

  String getTitle() {
    return title;
  }

  void setTitle(String newTitle) {
    this.title = newTitle;
  }

  String[] getAuthors() {
    return authors;
  }

  String getGenre() {
    return genre;
  }
}

class Book extends Publication {
  private String ISBN;

  Book(String title, String[] authors, String genre, String ISBN) {
    super(title, authors, genre);
    this.ISBN = ISBN;
  }

  public String getISBN() {
    return ISBN;
  }
}

class Borrower {
  String name;
  ArrayList<Publication> items = new ArrayList<>();

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

  void borrow(Publication pub) {
    pub.setTitle("Computer scientist");
    items.add(pub);
  }

  ArrayList<Publication> getItems() {
    return items;
  }
}

class Student extends Borrower {
  String course;
  String studentId;

  Student(String name, String course, String studentId) {
    super(name);

    this.course = course;
    this.studentId = studentId;
  }
}
```