How to Use Multilevel Inheritance in Java

Introduction

 
Please go through my first article on Single Level Inheritance in Java:
 
 
In this article, we learn about multilevel inheritance using Notepad. A class can be derived from a derived class and that is known as multilevel inheritance.

Step 1
 
Let's open Notepad and write the following code:
  1. class A  
  2. {  
  3.     void disp()  
  4.     {  
  5.         System.out.println("disp method");  
  6.     }  
  7. }  
  8. class Test extends A  
  9. {  
  10.     void show()  
  11.     {  
  12.         System.out.println("show method");  
  13.     }  
  14. }  
  15. class demo extends Test  
  16. {  
  17.     public static void main(String arg[])  
  18.     {  
  19.         demo d=new demo();  
  20.         d.show();  
  21.         d.disp();  
  22.     }  
  23. }  
Step 2
 
In this program we use "extends" to inherit all the properties from the base class or the parent's class.
 
Step 3
  
Name it "multy.java" and save the file in any location. I saved mine at "c:/kiran/program".
 
Step 4
 
Open a command prompt (press Windows + R and write cmd and hit OK).
 
 
Step 5
 
Go to "c:/app" using the command prompt.
 
 
Step 6
 
Set the path (path set means where you save your Java file).
 
 
Step 7
 
Now write the following code for checking whether or not the Java file has compiled.
  1. javac swap.java  
 
My Java program compiled successfully.
 
Step 8
 
Write the following code in the command prompt. Press Enter and see the output.
  1. java demo  
  2. // demo is a class name which is written in my "swap.java" file.  
Output
 
Happy coding.


Similar Articles