1
6.1kviews
Explain different types of inheritance with example.

Mumbai University > Information Technology > Sem 3 > Object Oriented Programming Methodology

Marks: 10M

Year: May 2014

1 Answer
1
45views
  • The mechanism of deriving a new class from existing one is called as Inheritance. The old class or existing class is known as super class and new class is known as subclass.
  • The subclass derives all of the instance variables and methods defined by super class and add its own unique elements. So the elements defined in super class are reused in the subclass.
  • There are four different types of the inheritance which are allowed by Java.
  • Single Inheritance (only one super and subclass)
  • Multiple Inheritance (several super classes for a subclass)
  • Hierarchical Inheritance (one super class with many subclasses)
  • Multilevel Inheritance (subclass is super class of another class)
  • Hybrid Inheritance (combination of above three types)
  • Java does not directly implement the multiple inheritance. This is implemented using the concept of interface.
  • The class can derived from another class by following the syntax:

    class subclassname extends superclassname
    {
    //Body of the class;
    }
    
  • The keyword ‘extends’ specifies that the properties of superclassname are extended to subclassname. After this the subclass will contain all the methods of super class and it will add the members of its own.

  • Single Inheritance: In this type of Inheritance, only one class is derived from a single class (Base class). In an example below, Class B is derived from Class A. That means Class B will be having access to data members and member functions of Class A. Another example can be oe student can have only one id in college to access its information.

enter image description here

  1. Multiple Inheritance: In this type of Inheritance, a class is derived from more than one super class. There can be more than one Super class and only one derived class. One derived class will implement properties of many Super classes. In this example, Class A extends properties of 3 Super Class, Class B, Class C and Class D. Another example can be many students belong to only one college.

enter image description here

  1. Hierarchical Inheritance: In this type of Inheritance, one super class can have multiple deriving classes. There can be many classes deriving only one super Class. In this example, Class A is having 3 derived classes Class B, Class C and Class D. 3 derived classes will extend prosperities of single base class, Class A. Another example can be One Universities can have multiple colleges affiliated.

enter image description here

  1. Multilevel Inheritance: In this type of Inheritance, Classes are inherited in the form of levels. For example, figure shows that Class C is derived from Class B and Class B is derived from Class A. Therefore , Inheritance can be carried out in multiple levels.

enter image description here

The derived class with multilevel inheritance can be declared as follows:

class A{……..};

class B extends A{….};

class C extends B{….};

The process can be extended to any number of levels.

  1. Hybrid Inheritance: Sometimes, there is a need to implement more than one type of inheritances. In such situations, we combine two or more types of inheritances and design a Hybrid Inheritance. Given figure shows, Class B and Class D have single Inheritance designed, whereas Class A has two derived classes class B and Class C. In this Hybrid Inheritance has been carried out combining Single and Multiple Inheritance.

enter image description here

Code showing implementation of Single Inheritance:

class Person
{   
    String name;
    static int age;
    Person()
    {
        name="Nikita";
        age=18;
    }
    void display()
    {
        System.out.println("Employee Name: "+name);
        System.out.println("Employee Age: "+age);
    }
}
class Employee extends Person
{
    String emp_des;
    static float emp_sal;
    Employee()
    {
         emp_des="Manager"; 
        emp_sal=25000f; 
    }
    void display1()
    {
        super.display();    
        System.out.println("Employee Designation:"+emp_des);
        System.out.println("Employee Salary:"+emp_sal);
    } 
}
class Main
{
    public static void  main(String args[])
    {
        Employee E1=new Employee();
        E1.display1();
    }}
Please log in to add an answer.