Home › Learn › Java › Objects

Encapsulation

Lesson 28 of 40. Private fields, getters, and a toString.

Fields marked private can only be touched by the class's own methods, so the class controls its data: a getter like String getTitle() reads it, and a setter like void setAvailable(boolean available) changes it, with any checking the class wants in between. That is encapsulation, and it is the default style for Java classes: fields private, methods public.

toString() is the method Java calls when an object is turned into text ("" + book, System.out.println(book)); every class inherits a useless one that prints the class name and a hash, and defining your own with @Override public String toString() makes objects readable. @Override asks the compiler to check that a method really replaces an inherited one; a typo in the name becomes a compile error instead of a silent second method.

This lesson builds the library project's Book: title, author, and ISBN that never change, and an availability flag that flips as it is lent out and returned.

class Account {
    private int balance;

    public int getBalance() {
        return balance;
    }

    public void deposit(int amount) {
        if (amount > 0) {
            balance += amount;
        }
    }

    @Override
    public String toString() {
        return "Account(" + balance + ")";
    }
}

Your task

Write class Book with private fields title, author, and isbn (strings) and available (a boolean that starts true), a constructor Book(String title, String author, String isbn), getters getTitle(), getAuthor(), getIsbn(), isAvailable(), a setter setAvailable(boolean available), and a toString() returning <title> by <author>.

Part of the final project. Book is a book in the catalog: title, author, ISBN, and whether it is on the shelf. It is saved in this browser and loaded into Project: the library when you get there.

Hint 1

private String title; ... private boolean available = true; then the constructor assigns this.title = title and so on.

Hint 2

public boolean isAvailable() { return available; } public void setAvailable(boolean available) { this.available = available; } @Override public String toString() { return title + " by " + author; }

Show the solution
public class Main {
    public static void main(String[] args) {
        Book b = new Book("Dune", "Frank Herbert", "0441172717");
        System.out.println(b);
    }
}

class Book {
    private String title;
    private String author;
    private String isbn;
    private boolean available = true;

    Book(String title, String author, String isbn) {
        this.title = title;
        this.author = author;
        this.isbn = isbn;
    }

    public String getTitle() {
        return title;
    }

    public String getAuthor() {
        return author;
    }

    public String getIsbn() {
        return isbn;
    }

    public boolean isAvailable() {
        return available;
    }

    public void setAvailable(boolean available) {
        this.available = available;
    }

    @Override
    public String toString() {
        return title + " by " + author;
    }
}

    Compiles with OpenJDK javac and TeaVM, both compiled to WebAssembly (7 MB download the first time, then cached), and runs in this tab; nothing is sent anywhere.