Types of Inheritance

INHERITANCE……

Inheritance describe the relationship between two classes one is base class and other is derived class. Base class is also known as Super class or Parent class and derived class is also known as sub class or child class .Inheritance concept is same a parent and child relationship in real world entity. In this concept child class inherit the parent class property and methods. There are many types of inheritance

Types of Inheritance.....

1)SINGLE INHERITANCE(SIMPLE INHERITANCE)………One base class and one derived class

2)MULTILEVEL INHERITANCE……Also known as indirect inheritance because in this concept grand parents property is inherited by parents and parents property is inherited by child class so the indirect way we can say that the property of grand parents is inherited by child class.

3)HIERARCHIAL INHERITANCE……Also known as tree concept .Hierarchy means branches of a root.

4)HYBRID INHERITANCE……There is one base class that is grand parents class and two derived class that means two child and again one derived class which inherits the properties of two child classes.

SINGLE INHERITANCE……

In simple inheritance there is one base class(parents class, super class) and one derived class(sub class,child class).Child class inherits the properties and method of parents class means child class can use the variable and methods of parents class .We can create the object of child class because the parents class inherited by child class by using  “extends “keyword.

The programof this inheritance is…..

class parent       //parent class(base class)                     single inheritance

{

public void f1()   //f1 function is created by parent class

{

System.out.println(“parents class”); //message print

int first,second,total;   //variable declaration for arithmetic calculation

first=4;                         //value passing

second=50;

total=a+b;             //arithmetic calculation

System.out.println(‘total=”+total);  //output display

}                               //end of function f1

}                           //end of class parent(base class)

class child extends parent     //child class inherit the parent class by using extends keyword

{

public void f2()   //f2 is the function which is created by child class

{

System.out.print(“child class”);   //message print

}}

class mainn   //main class

{

public static void main(String axa[])   //main method

{

child ob=new child();     //ob is the object of class child

Ob.f1();                 //f1 function called by the object of child class

Ob.f2();               //f2 function called by the object of child class

}                         //end of mainn method(public static void main method)

}                       //end of mainn class

                                                               Save as…….mainn.java

                                                               Run……..javac mainn.java

                                                                           Java mainn

Output….

Parents class

Child class

HIERARCHICAL INHERITANCE………

Hierarchical inheritanceàHierarchical means tree concept .In real world entities there is one grand father class which have two sub class that is father1 and father2 which inherits the grand father class property.Son1,daughter and son2 is also the derived or sub class of father1 and father2.Here son1 and daughter class are the derived of father1 class but son2 is derived class of father2 class.

                         Hierarchical inheritance                                         

Program….

class grandparents //grandparents class or base class

{

public void f1() //function created by base class

{

System.out.println(“grand parents”); //message print (we can use any program with message print)

}   //f1 function close

}   //base class(grand parents )close

class father1 extends grandparents   //derived class(father1)inherits base class

{

public void f2()//f2 is the function of class father1

{

System.out.println(“father1”);//message print

} //end of function f2

} //end of class father1

class father2 extends grandparents //father2 inherits baseclass(grand parents)

{

public void f3() //f3 is the function of class father2

{

System.out.println(“father2”);

}   //end of function f3

}   //end of class father2

class son1 extends father1   //son class inherits father1 class

{

public void f4()   //f4 is the function of class son1

{     //f4 method body start

System.out.println(“son1”); //message print

} //end of function f4

} //end of class son1

class daughter extends father1   //daughter class inherits father1 class

{

public void f5()   //f5 function created by daughter

{

System.out.println(“daughter”);

}}

class son2 extends father2   //son2 inherits class father2(means father2 is the base class for class son2)

{

public void f6()

{

System.out.prinln(“son2”);

}

}

class main

{

public static void main(String aa[])

{

father1 ob1=new father1();

father2 ob2=new father2();

son1 ob3=new son1();

daughter ob4=new daughter();

son2 ob5=new son2();

ob1.f1();   ob1.f2();

ob2.f1(); ob2.f3();

ob3.f1();ob3.f2();ob3.f4();

ob4.f1();ob4.f2();ob3.f5();

ob5.f1();ob4.3();ob4.f6();

}}

We can want to use any function with program(calculation) .Here we use message print .But if we want to do the program in the function then we use like this…

public void f1()

{

System.out.print(“father class”);

int a,b;

a=4;

b=3;

if(a>b)

{

System.out.println(“a is greater”+a);

}

else

{

System.out.println(“b is greater”+b);

}}

MULTILEVEL INHERITANCE…..

In multilevel inheritance there is one grand parent class means one base class and one derived called parents class and one child class which inherits the properties and methods of parents class.Multilevel level inheritance is also known as INDIRECT inheritance because in multilevel inheritance the property of grandparents is inherit by parents and the property of parents class is inherit by child class so,the property and method of grand parents class is inherit by child class.

                                                                         Multilevel inheritance

PROGRAM…..

class grandparents

{

public void f1()

{

System.out.println(“grandparents”);

}

}

class parents extends grandparents

{

public void f2()

{

System.out.println(“parents class”);

}}

class child extends parents

{

public void f3()

{

System.out.println(“child class”);

}}

class main

{

public static void main(String aa[])

{

child ob1=new child();

ob1.f1();

ob1.f2();

ob2.f3();

}

}