Java Tutorial : Inheritance in Java – Part 2, Example
In the last article we saw the inheritance in java.
The following syntax shows how to implement single level inheritance:
class A
{
//body of Class A
}
class B extends A
{
//body of Class B
}
class C extends A
{
//body of Class C
}
In the preceding syntax, the extends keyword is used to derive a subclass from a superclass.
Books Treasure Inc. is an organization that has a library containing various books. The books can be categorized as Hardware Books and Software Books.
Don Allen, the librarian at Books Treasure Inc. wants to automate the library management system. To do this, Susan Ward, the programmer needs to create a superclass, Book that stores the book details, such as author, title, and number of
pages.
A subclass, HardwareBook needs to be created to store details about the hardware books. Another subclass, SoftwareBook needs to be created to store details about the software books. Susan creates a class, Library that contains the main() method.
You can use the following code to create the Book class:
class Book //Declaration of the Superclass
{
//Declaring the data members.
String author= “Steve”;
String title = “Handbook”;
int price = 50;
int pages = 350;
int stock = 13;
//Defining the methods.
public void show()
{
System.out.println(” “);
System.out.println(“\t Books Information”);
System.out.println(“\t Book Author: ” + author);
System.out.println(“\t Book Title: ” + title);
System.out.println(“\t Book Price: ” + price);
System.out.println(“\t Number of pages: ” + pages);
System.out.println(“\t Book Stock: ” + stock);
System.out.println(” “);
}
}
In the preceding code, the Book class contains the show() method and the data members, such as author, title, price, pages and stock. When the show() method is called, the details of the book are displayed. To extend the functionality of the Book class to the HardwareBook class, you can use the following code:
class HardwareBook extends Book//Declaration of the Subclass
{
//Declaring the data members.
String hardwareTitle = “Printers”;
String publisher = “Tom Wilkins”;
//Defining the method, showData()
public void showData()
{
System.out.println(” “);
System.out.println(“\t Hardware Books Information”);
System.out.println(“\t Hardware Title: ” + hardwareTitle);
System.out.println(“\t Publisher Name: ” + publisher);
System.out.println(” “);
}
}
In the preceding code, the HardwareBook class is inherited from the Book superclass. The HardwareBook class defines the showData() method and the data members, such as hardwareTitle and publisher. When the showData() method is called, the details of the hardware books are displayed.
To extend the functionality of the Book class to the SoftwareBook class, you can use the following code:
class SoftwareBook extends Book//Declaration of the Subclass
{
//Declaring the data members.
String softwareName = “Windows”;
String softwareVersion = “Mary Peterson”;
//Definition of method
public void showDetails()
{
show();//Calling the method of Book class.
System.out.println(“\t Software Books Information”);
System.out.println(“\t Software Name: ” + softwareName);
System.out.println(“\t Software Version: ” +
softwareVersion);
System.out.println(” “);
}
public static void main(String args[])
{
HardwareBook h = new HardwareBook();
SoftwareBook s = new SoftwareBook();
s.showDetails();
h.showData();
}
}
In the preceding code, the SoftwareBook class is inherited from the Book superclass. The SoftwareBook class defines the data members, softwareName and softwareVersion and the showDetails() method. When the showDetails() method
is called, the details of classes, Book and SoftwareBook are displayed. The SoftwareBook class creates objects of subclasses, SoftwareBook and HardwareBook inside the main() method. The SoftwareBook object, s calls the showDetails()
method; and the HardwareBook object, h calls the showData() method of the HardwareBook class.
The following figure shows the output of the preceding code:


