Inheritance is one such mechanism.In which the properties and methods of one class can be used in another class. That is, the child class inherits the properties an methods of its parent class.
Parent class is called base class, super class and child class is also called sub class ,derived class.
Inheritance represents is a relationship which is also called parent-child relationship.
There are some types of Inheritance,
1. Single Level Inheritance
2.Multi-Level Inheritance
3. Hierarchical Inheritance
4. Multiple Inheritance
5. Hybrid Inheritance
1. Single Level Inheritance : In single level inheritance a class inherits the properties of one single class.
class Shape{
public void area(){
System.out.println("displays area");
}
}
class Traingle extends Shape{
public void area(int l,int h){
System.out.println("1/2*l*h");
}
}
public class main{
public static void main(String[]args){
}
}
2. Multi-Level Inheritance : In Multi-Level Inheritance there is one base class which has one derived class and that derived class can have another derived class hence making it a base class. This can go to several levels.
class Shape{
public void area(){
System.out.println("displays area");
}
}
class Traingle extends Shape{
public void area(int l,int h){
System.out.println("1/2*l*h");
}
}
class Equilateral Traingle extends Traingle{
public void area(int l,int h){
System.out.println("1/2*l*h");
}
}
public class main{
public static void main(String[]args){
}
}
3. Hierarchical Inheritance : In Hierarchical Inheritance there is a single base class and multiple derived classes.
class Shape{
public void area(){
System.out.println("displays area");
}
}
class Traingle extends Shape{
public void area(int l,int h){
System.out.println("1/2*l*h");
}
}
class Circle extends Shape{
public void area(int r){
System.out.println(Math.PI*r*r);
}
}
public class main{
public static void main(String[]args){
}
}
4.Multiple Inheritance : In Multiple Inheritance there are multiple base classes and a single child or derived class.
5. Hybrid Inheritance : In Hybrid Inheritance there can be two or more types of inheritance between classes.