How To Use Single Inheritance in Java

Introduction 

 
Now, we will first write a program using a "has a relationship " in Single Inheritance. 
 
Step 1
Let's open Notepad and enter the following code.
  
Code
  1. class Test   
  2. {  
  3.     void show()  
  4.     {  
  5.         System.out.println("welcome in java method");  
  6.     }  
  7. }  
  8. class demo   
  9. {  
  10.     public static void main(String arg[])  
  11.     {  
  12.         Test t = new Test();  
  13.         t.show();  
  14.     }  
  15. }
Step 2
Name it "hasrel.java" and save the file in any location. I saved at "C:/kiran/program".
 
Step 3
Open a command prompt (press window+R and enter cmd and hit OK).
 
 
 
 
Step 4
Go to the "command prompt".
  
 
 
Step 5
Go to "C:/kiran/program" by cmd. 
 
 
 
Step 6
Now, enter the following code for cheking my Java file is compiled or not.
 
javac hasrel.java
 
 
 
My Java file is compiled successfully.
 
Step 7
Enter the following code into the command prompt. Press Enter and see the output.
  
Java demo
 
//demo is a class name that is written in my "hasrel.java" file 
 
Output
 
  
We will now enter the program using a "is a relationship " in Single Inheritance.
  
Step 8
Enter the following code in Notepad.
  
Code
  1. class Test   
  2. {  
  3.     void show()   
  4.     {  
  5.         System.out.println("welcome in java method!!!");  
  6.     }  
  7. }  
  8. class demo extends Test  
  9. {  
  10.     public static void main(String arg[])  
  11.     {  
  12.         demo d = new demo();  
  13.         d.show();  
  14.     }  
  15. }  
Step 9
Now, enter the following code for cheking my Java file is compiled or not.
 
javac hasrel.java
 
  
My Java file is compiled succesfully.
  
Step 10
Repeat Step 7.
  
Output
 
 
I hope you like this Java file.
  
Happy Codding.


Similar Articles