The mechanism of deriving a new class from an old one is called inheritance. The old class is known as the base class or super class or parent class and the new one is called the subclass or derived class or child class.
The inheritance allows subclasses to inherit all the variables and methods of their parent classes. Inheritance may take different forms:

  • Single inheritance (only one super class)
  • Hierarchical inheritance (one super class, many subclasses)
  • Multiple inheritance(many super class, one subclass)
  • Multilevel inheritance(Derived from a derived class)

Single Inheritance     
            When a subclass is derived simply from its parent class then this mechanism is known as simple inheritance. In case of simple inheritance there is only a sub class and its parent class. It is also called single inheritance or one level inheritance.

class A {
public int i,j;
void setij(int x, int y) {
i = x;
j = y;
}
}

class B extends A {
int total;
void sum() {
total = i + j;
}
class Access {
public static void main(String args[]) {
B subOb = new B();
subOb.setij(10, 12);
subOb.sum();
System.out.println("Total is " + subOb.total);
}
}
Hierarchical inheritance
Many programming problems can be cast into a hierarchy where certain features of one level are shared by many others below the level.
/* Program to Implement Hierarchical Inheritance in java Programming Language */
class Info
{
int pid;
char branch;
char year;

Info(int p,char ch,char y)
{
pid = p;
branch = ch;
year = y;
}

void display()
{
System.out.println("\nPID\t: "+pid);

System.out.print("Branch\t: ");
if(branch == 'i')
System.out.println("Information Technology");
if(branch =='e')
System.out.println("Electronics and Telecommunication");
if(branch =='c')
System.out.println("Computer Science");

System.out.print("Year\t: ");
if(year == 'f')
System.out.println("FE");
if(year == 's')
System.out.println("SE");
if(year == 't')
System.out.println("TE");
}
}

class Fe extends Info
{
int c;
int cpp;

Fe(int p,char ch,char y,int m1,int m2)
{
super(p,ch,y);
c = m1;
cpp = m2;
}

void fdisplay()
{
display();
System.out.println("Performance:");
System.out.println("\tC\t"+c);
System.out.println("\tC++\t"+cpp);
}
}

class Se extends Info
{
int vb;
int html;

Se(int p,char ch,char y,int m1,int m2)
{
super(p,ch,y);
vb = m1;
html= m2;
}

void sdisplay()
{
display();
System.out.println("Performance:");
System.out.println("\tVB\t"+vb);
System.out.println("\tHTML\t"+html);
}
}

class Te extends Info
{
int matlab;
int java;

Te(int p,char ch,char y,int m1,int m2)
{
super(p,ch,y);
matlab = m1;
java = m2;
}
void tdisplay()
{
display();
System.out.println("Performance:");
System.out.println("\tMATLAB\t"+matlab);
System.out.println("\tSJava\t"+java);
}
}

class Language
{
public static void main(String args[])
{
Fe F = new Fe(1074,'i','f',9,8);
Se S = new Se(1064,'e','s',6,8);
Te T = new Te(1054,'c','t',9,9);
F.fdisplay();
S.sdisplay();
T.tdisplay();
}
}
Multiple Inheritance
The mechanism of inheriting the features of more than one base class into a single class is known as multiple inheritance. Java does not support multiple inheritance but the multiple inheritance can be achieved by using the interface.
In Java Multiple Inheritance can be achieved through use of Interfaces by implementing more than one interfaces in a class.

 

Class Other
{              public Other(int value) { ... }
               
               public void whatever()
               {...
               }
}
Interface OtherInterface
{              void whatever();
}
 
class OtherChild extends Other implements OtherInterface
{              public OtherChild (int value){ super(value); }
}

Multilevel Inheritance
            It is the enhancement of the concept of inheritance. When a subclass is derived from a derived class then this mechanism is known as the multilevel inheritance. The derived class is called the subclass or child class for its parent class and this parent class works as the child class for it's just above (parent) class.  Multilevel inheritance can go up to any number of level.
class A {
  int x;
  int y;
  int get(int p, int q){
    x=p; y=q; return(0);
    }
    void Show(){
      System.out.println(x);
      }
}
class extends A{
  void Showb(){
    System.out.println("B");
    }
}

class extends B{
  void display(){
    System.out.println("C");
  }
  public static void main(String args[]){
    A a = new A();
    a.get(5,6);
    a.Show();
    }
}