different types of inheritance in java code example

Example 1: inheritance in java

// super class
class Animal
{
   void eat()
   {
      System.out.println("Animal is eating.");
   }
}
// subclass
class Lion extends Animal
{
   public static void main(String[] args)
   {
      Lion obj = new Lion();
      obj.eat();
   }
}

Example 2: multilevel inheritance in java

class Car{
   public Car()
   {
	System.out.println("Class Car");
   }
   public void vehicleType()
   {
	System.out.println("Vehicle Type: Car");
   }
}
class Maruti extends Car{
   public Maruti()
   {
	System.out.println("Class Maruti");
   }
   public void brand()
   {
	System.out.println("Brand: Maruti");
   }
   public void speed()
   {
	System.out.println("Max: 90Kmph");
   }
}
public class Maruti800 extends Maruti{

   public Maruti800()
   {
	System.out.println("Maruti Model: 800");
   }
   public void speed()
   {
	System.out.println("Max: 80Kmph");
   }
   public static void main(String args[])
   {
	 Maruti800 obj=new Maruti800();
	 obj.vehicleType();
	 obj.brand();
	 obj.speed();
   }
}

Example 3: Inheritance in java

// Hybrid inheritance in java
class A 
{
   public void display()
   {
      System.out.println("class A display method");
   }
}
interface B 
{
   public void print();
}
interface C
{
   public void print();
}
public class HybridInheritanceDemo extends A implements B, C
{
   public void print()
   {
      System.out.println("implementing print method");
   }
   public void show()
   {
      System.out.println("show method of class HybridInheritanceDemo");
   }
   public static void main(String[] args) 
   {
      HybridInheritanceDemo obj = new HybridInheritanceDemo();
      obj.show();
      obj.print();
   }
}

Tags:

Java Example