What is Single Inheritance? Explain it with example.

- The mechanism of deriving a new class from an old one is called Inheritance.

- One class derived into another class is called Single Inheritance.

- A derive class with only one base class is called Single Inheritance.


Example : 

                                                

single-inheritance-diagram
Figure of Single Inheritance


import java.util.Scanner;

class A

{

    int i;

    public void sq(int n)

    {

        i=n*n;

    }

}

class B extends A

{

    public void set()

    {

        System.out.print("Square is : "+i);

    }

}

class Square

{

    public static void main(String arg[])

    {

        B obj=new B();

        Scanner s=new Scanner(System.in); 

   

        System.out.print("Enter Any No : ");

        int no=s.nextInt();

        obj.sq(no);

        obj.set();

    }

}


OUTPUT : 

          Enter Any No : 10

          Square is : 100

Comments