Inheritance:
When you were at your school you must have come up with the term inheritance while reading biology. What was the meaning of inheritance in biology. Heredity is the passing of traits to offspring (from its parent or ancestors). This is the process by which an offspring cell or organism acquires or becomes predisposed to the characteristics of its parent cell or organism. So it’s the same concept that is being employed in java programming.
Inheritance is the process of creating a new class from the existing class rather than creating a new class from the scratch. Also inheritance can be defined as extending already defined class.The new class being made is called a derived class or child class or subclass.
And the existing class from which the new class is made is called base class or super class or parents class.
After inheriting the base class the derived class can be able to use all the functionality available in the base class just using one line of code. The derived class can use the instance variables and the methods in the base class. Also the derived class can add the functionality of its own. Imagine what it be without inheritance ,the child class would have to write every method available in the base class again.
Inheritance is like making a new model of cell phone adding some extra functionality on the existing one .See every cell phone available in the market they all have a common functionality only what we find is some extra features and graphics being added.This is the good example of inheritance the company wont make a new model of cell phone bringing change in every hardware and software that previous model had.They will reuse at least something from previous model. Inheritance is also about the re-usability of code.Why to write a code again and again if verified and tested code is available.So inheritance saves the time and money.
Syntax to make inherit the class
class childclassname extends baseclassname
eg:class Dog extends Animal
{
—-
——
}
lets see one example code:
class Father
{
String color="red";
public void myCar()
{
System.out.println("i have a "+color+"car");
}
}
class Son extends Father
{
public static void main(String[] args)
{
Son obj=new Son();
obj.myCar();
}
}
So can you understand the code .Here the son needs the method to display car. It can find that the method is already available in its
Father class so it extends the Father class rather than writing the code of its own.
So from now onward whenever you find some functionality that some class can fulfill than extend that class.
how to know if a class can be inherited
To know if the class you are going to make can be inherited from the existing one we can do one test called IS A .
Which means suppose I have a class called Mobile to describe what a mobile should be like. Bow I am going to make a class called Nokia than we will do the test like this. Nokia is a Mobile .It sounds so clear yes Nokia is a mobile phone means Nokia class can be made to inherit Mobile class.
Types of Inheritance in java
There are three type of inheritance in java.
1-Single inheritance
2-Multilevel inheritance
3-Hierarchial inheritance
Multiple inheritance is not supported in java.
Single inheritance
It is the simplest type of inheritance available in java where a class will inherit the properties from a single base class. It means we will have a single base class and a single derived class.
Following figure gives the schematic representation of single inheritance in java.

Single Inheritance
example code of single inheritance in java:
class MobilePhone
{
public void call()
{
System.out.println("basic feature of mobile");
}
}
class NokiaLumia extends MobilePhone
{
public void lumia()
{
System.out.println("features of Lumia");
}
public static void main(String[] args)
{
NokiaLumia cell=new NokiaLumia();
cell.call();
cell.lumia();
}
}

sample output of single inheritance
Multilevel inheritance
The multilevel inheritance can be well defined only from a simple diagram so please see the figure below which shows the schematic representation of single inheritance in java.

Multi-Level inheritance
These type of inheritance follows the one to one ladder as shown in figure.The father will inherit the property of the Grandfather so
the grandfather is the base class of the father but the son will inherit the properties of Father but will also have the properties from the
grandfather class.The multilevel inheritance is the indirect way of implementing multiple inheritance like we can see in this example
the son class has a multiple super class Grandfather and Father.
example code of multiLevel inheritance in java
class Grandfather
{
public void one()
{
System.out.println("grandfather")
}
}
class Father extends Grandfather
{
public void two()
{
System.out.println("Father");
}
}
class Son extends Father
{
public void three()
{
System.out.println("son");
}
public static void main(String args[])
{
Son obj=new Son();
obj.one();
obj.two();
obj.three();
}
}

sample output of above examle code
3.Hierarchical inheritance
It is the type of inheritance in which multiple classes are derived from the same single base class. Or we can say it as one class is being inherited by more than one class such type of inheritance is called hierarchical inheritance.Figure below depicts the hierarchical inheritance.

example code of hierarchical inheritance in java
class HumanBeing
{
public void parts()
{
System.out.println("I am human ");
}
}
class Male extends HumanBeing
{
public void boys()
{
System.out.println("Male");
}
}
class Female extends HumanBeing
{
public void girls()
{
System.out.println("Female");
}
}
class MainStart
{
public static void main(String[] args)
{
Male m=new Male();
m.parts();
m.boys();
Female f=new Female();
f.parts();
f.girls();
}
}

sample output of above examle code