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: Inheritance in java

// Multiple inheritance in java
interface M
{
   public void helloWorld();
}
interface N
{
   public void helloWorld();
}
// implementing interfaces
public class MultipleInheritanceExample implements M, N
{
   public void helloWorld()
   {
      System.out.println("helloWorld() method");
   }
   public static void main(String[] args) 
   {
      MultipleInheritanceExample obj = new MultipleInheritanceExample();
      obj.helloWorld();
   }
}

Example 3: Inheritance in java

// Multilevel inheritance in java
class Animal
{
   void eating()
   {
      System.out.println("animal eating");
   }
}
class Lion extends Animal
{
   void roar()
   {
      System.out.println("lion roaring");
   }
}
public class Cub extends Lion
{
   void born()
   {
      System.out.println("cub drinking milk");
   }
   public static void main(String[] args)
   {
      Animal obj1 = new Animal();
      obj1.eating();
      Lion obj2 = new Lion();
      obj2.eating();
      obj2.roar();
      Cub obj3 = new Cub();
      obj3.eating();
      obj3.roar();
      obj3.born();
   }
}

Example 4: Inheritance in java

// Hierarchical inheritance in java
class Animal
{
   void eating()
   {
      System.out.println("animal eating");
   }
}
class Lion extends Animal
{
   void roar()
   {
      System.out.println("lion roaring");
   }
}
public class Dog extends Animal
{
   void bark()
   {
      System.out.println("dog barking");
   }
   public static void main(String[] args)
   {
      Animal obj1 = new Animal();
      obj1.eating();
      Lion obj2 = new Lion();
      obj2.eating();
      obj2.roar();
      Dog obj3 = new Dog();
      obj3.eating();
      obj3.bark();
   }
}

Example 5: 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();
   }
}

Example 6: Inheritance in java

// single inheritance example
import java.util.*;
class Animal
{
   void eat()
   {
      System.out.println("Animal is eating.");
   }
}
class Lion extends Animal
{
   void roar()
   {
      System.out.println("lion is roaring.");
   }
   public static void main(String[] args)
   {
      Lion obj = new Lion();
      obj.eat();
      obj.roar();
   }
}

Example 7: Inheritance in Java

Java Inheritance (Subclass and Superclass)
In Java, it is possible to inherit attributes and methods from one class to another. We group the "inheritance concept" into two categories:

subclass (child) - the class that inherits from another class
superclass (parent) - the class being inherited from
To inherit from a class, use the extends keyword.

Example 8: inheritance in java

it is used to define relationship between two class, 
which a child class occurs all the properties and
  behaviours of a parent class. 
Provides code reusability.
Ex: in my framework I have a TestBase 
class which I store 
all my reusable code and methods.
  My test execution classes and 
elements classes will extend the 
TestBase in order to reuse the code.

Example 9: inheritance in java

Inheritance in Java is a mechanism in which one object acquires 
all the properties and behaviors of a parent object.
It is an important part of OOPs (Object Oriented programming system).

Tags:

Misc Example