Java Tutorial : Multilevel Inheritance
In multilevel inheritance, a subclass inherits the properties of another subclass. For example, Class A is a superclass for the Class B; and Class B is a superclass for the subclass, Class C. You can include any number of levels in multilevel inheritance. The following syntax shows how to implement multilevel inheritance:
class A
{
//Body of class A
}
class B extends A
{
//Body of class B
}
class C extends B
{
//Body of class C
}
In the preceding syntax, class A is the superclass and class C is the subclass. The class B acts as a subclass for the class A and superclass for the class C.
The following figure shows the structure of multilevel inheritance:
You can implement multilevel inheritance in Java by using the Book superclass that displays the details of books. The SoftwareBook subclass extends from the Book superclass and displays the details of software computer books. The Cplus subclass extends from the SoftwareBook subclass and displays the details of software book, C++. You can use the following code to create the Book superclass:
class Book
{
//Declaring the data members.
int price;
int pages;
//Defining the get() method.
public void get(int mprice, int mpages)
{
price = mprice;
pages = mpages;
}
public void show()
{
System.out.println(” “);
System.out.println(“\t Books Information”);
System.out.println(“\t Book Price: ” + price);
System.out.println(“\t Number of pages: ” + pages);
System.out.println(” “);
}
}
In the preceding code, the Book superclass defines the data members, such as price and pages. The Book class also defines methods, get() and show(). The get() method initializes the values of the data members. The show() method displays the
values stored in these data members.
To extend the functionality of the Book class to the SoftwareBook class, you can use the following code:
class SoftwareBook extends Book//Defining the subclass.
{
//Declaring the data members.
String softwareName;
String softwareVersion;
//Defining the methods.
public void getDetails(String msoftwareName, String msoftwareVersion)
{
softwareName = msoftwareName;
softwareVersion = msoftwareVersion;
}
public void showDetails()
{
System.out.println(” “);
System.out.println(“\t Software Books Information”);
System.out.println(“\t Software Name: ” + softwareName);
System.out.println(“\t Software Version: ” +
softwareVersion);
System.out.println(” “);
}
}
In the preceding code, the SoftwareBook subclass extends from the Book superclass. The SoftwareBook class defines the data members, softwareName and softwareVersion. The SoftwareBook class defines the methods, getDetails() and showDetails(). The getDetails() method initializes the values of these data members. The showDetails() method displays the values stored in these data members.
To extend the functionality of the SoftwareBook class to the Cplus class, you can use the following code:
class Cplus extends SoftwareBook //Declaration of the Subclass
{
//Declaring the data members.
String author;
String title;
//Defining the methods.
public void getData(String mauthor, String mtitle)
{
author = mauthor;
title = mtitle;
}
public void showData()
{
show();
showDetails();
System.out.println(” “);
System.out.println(“\t C++ Books Information”);
System.out.println(“\t Author Name: ” + author);
System.out.println(“\t Book Title: ” + title);
System.out.println(” “);
}
public static void main(String args[])
{
Cplus c = new Cplus();
c.get(45, 450); // Calling the get() method of the Book
class.
// Calling the getDetails() method of the Software class.
c.getDetails(“Borland C++”, “5.0″);
// Calling the getData() method of the Cplus class.
c.getData(“Lee Mitchell”, “Programming using C++”);
c.showData();
}
}
In the preceding code, the Cplus class extends from the SoftwareBook class. The SoftwareBook class is a subclass as well as a superclass. The Cplus class defines data members, author and title and methods, getData() and showData(). The getData() method initializes the values of these data members. The showData() method displays the values stored in these data members. The values for the data members are passed through the main() method created in the Cplus class.
The following figure shows the output of the preceding code:




Pingback: Java Tutorial: Implementing Overriding Methods - Rocking Team
Pingback: Inheritence : How the inherited methods behave