How to use single inheritance in java?

In this article we learn single inhetitance by using “has a relationship” ang “is a relationship”.

Now, first of all we write the program by using “has a relationship ” in Single Inheritance.

Step 1: Lets open the notepad and write the following code.

//Code

 

class Test

{

                void show()

                {

                                System.out.println(“welcome in java method”);

                }

}

class demo

{

                public static void main(String arg[])

                {

                                Test t=new Test();

                                t.show();

                }

}

 

Step 2:  Name it as “hasrel.java” and save the file in any location. i saved at “C:/kiran/program”.

step 3: Open command prompt(press window +R and write cmd and hit ok).

1

step 4: Go to  “command prompt.

2

step 5: Go to “C:/kiran/program” by using cmd.

3

Step 6: Now, write the following code for cheking my java file is compile or not.

javac hasrel.java

5

My java file is compiled succesfully.

 

Step 7: Write the following code in command prompt. press enter and see the output.

java demo

//Demo is a class name which is written in my “hasrel.java” file.

 

Output

6

Now,  we write the program by using  “is a relationship ” in Single Inheritance.

Step 8: Write the code in the notepad.

//Code

 

class Test

{

                void show()

                {

                                System.out.println(“welcome in java method!!!”);

                }

}

class demo extends Test

{

                public static void main(String arg[])

                {

                                demo d=new demo();

                                d.show();                           

                }

}

 

Step 10: Now, write the following code for cheking my java file is compile or not.

javac isrel.java

7

 

My java file is compiled succesfully.

 

Step 11: Follow the step 7.

Output:

8

 

I hope you like this java file.

Happy codding.

Tags: ,