Java: Inheriting a class with members

This entry is part 36 of 54 in the series Learn Java

Let’s look at an inheritance example with members.

The class LibraryBook is a child class of a super class Book. Here, we say LibraryBook inherits states and behaviors of objects of Book class such as isbn, author, title and many others.

Apart from deriving from Base class, LibraryBook can have its own data members such as number of copies and member functions representing states and behaviors of objects.

package javaapplication31;

public class BookClass {

    static class Book {

        int isbn;
        String author;
        String title;

        public void init(int isbn, String author, String title) {
            this.isbn = isbn; // this.isbn refers to instance variable,
            // isbn refers to local variable
   = author;
            this.title = title;

    static public class LibraryBook extends Book {

        int nCopies = 10; // number of copies of the book

        public void show() {
            System.out.println("isbn = " + isbn + "\nauthor = " + author + "\ntitle = " + title + "\ncopies = " + nCopies);

    public static void main(String[] args) {

        LibraryBook lb = new LibraryBook();
        lb.init(4321, "Jeniffer", "Data Structures");;




Series Navigation<< Java: Static variables
Java: Objects as arguments to methods >>

Leave a comment

Leave a Reply