Inheritance in java with example

It is a mechanism in which one object acquires  all the properties and behaviors of parent object. 


It is a building block. It is the property by which an object acquires properties and behaviors of another object. 

Example: Parent-child relationship here child is inherit of parent properties and behaviors.

Types of Inheritance: There are three types of inheritance shown in figure below.

1. Single Inheritance : In this we can have single base class and n number if drive class.

Example:-

class a

 {

    int a;

  void display()

   {

      System.out.println(“Display method”);

    }

 class b extends a

  {

  }

}

2. Multi Level inheritance 

It is just concept of grand parent class child class inheritance of parent class and parent-class is inheritance of grand-parent-class so in this case grand parent-class is implicitly inheriting the properties and the method of grand-parent along with parent-class that’s is called multi level inheritance.

## For example 1.

class A

  {

     System.out.println(“Grand-parent-class”);

   }

class B extends A

 {

      System.out.println(“Parent-class inheritance of grand-parent”);

 }

class C extends B

 {

      System.out.println(“child-class inheritance of parent-class”);

 }

## Example 2.

package com;

/**

 *

 * @author pradeep

 */

class bike{

public bike()

{

System.out.println(“Class bike”);

}

public void vehicleType()

{

System.out.println(“Vehicle Type: bike”);

}

}

class hero extends bike{

public hero()

{

System.out.println(“Class hero”);

}

public void brand()

{

System.out.println(“Brand: hero”);

}

public void speed()

{

System.out.println(“Max: 70Kmph”);

}

}

public class honda extends hero{

 public honda()

 {

 System.out.println(“honda : 80kmph”);

 }

 public void speed()

{

System.out.println(“Max: 120Kmph”);

}

 public static void main(String args[])

 {

 honda obj=new honda();

 obj.vehicleType();

 obj.brand();

 obj.speed();

 }

}

Output : Output of this program is below.

run:

Class bike

Class hero

honda : 80kmph

Vehicle Type: bike

Brand: hero

Max: 120Kmph

BUILD SUCCESSFUL (total time: 1 second)

3. Multiple Inheritance :

In this we can have n numbers of parents and a single class which drives from it.

## For Example 1.

 class A

   {

       void display()

         {

         }

       class B

            {

                   void display()

                          {

                           }

                   class C extends A,B

                       {

                             C C1=new C1();

                             C1.disp();

                          }

            }

   }

## Example 2.

/**

 *

 * @author pradeep

 */

public class MultipleInheritance 

{

}

interface A

{

   public void MultipleInheritance();

}

interface B

{

   public void MultipleInheritance();

}

class MeltiDemo implements A, B

{

   public void MultipleInheritance()

   {

       System.out.println(” Multiple inheritance”);

   }

}

Post a Comment

Previous Post Next Post