Learn inheritance in java

Hey guys?!! Welcome to flower brackets blog. Today we are going to learn inheritance in java.

Inheritance is a procedure of accessing all the properties and behavior from one class (super class) to another class (subclass). Inheritance represent “IS-A” relationship between super class and subclass.

Also read – learn abstraction in java

By inheriting we are reusing all the methods, properties and behavior of super class.

Syntax:

class SubClass extends SuperClass
{
   // code
}

A class in java can be extended by creating new classes which can retain the characteristics of the base class.

Base class is the class from which we are inheriting. Let’s see an example,

// super class
public class Polygon
{ 
   public int height;
   public int width;
 
   public void setValues(int a,int b)
   {
      height = a;
      width = b;
   }
}

// Rectangle class is subclass
public class Rectangle extends Polygon
{ 
   public double area()
   {
      return (height * width);
   }
}

// Triangle class is subclass
public class Triangle extends Polygon
{ 
   public double area()
   {
      return (height * width) / 2;
   }
}
public class InheritanceExample
{
   public static void main(String[] args)
   {
      Rectangle re = new Rectangle();
      Triangle tri = new Triangle();
      re.setValues(10,40);
      tri.setValues(20,30);
      System.out.println("Area of rectangle: " + re.area());
      System.out.println("Area of triangle: " + tri.area());
   }
}

Output:

Learn Inheritance In Java


Types of inheritance

There are five types of inheritance out of which three are supported they are single, multilevel and hierarchical inheritance.

Remaining two multiple and hybrid inheritance can be implemented through interfaces.

Single inheritance:

In single inheritance subclass inherits functionality of one superclass.

Learn Inheritance In Java

class Parent
{
   public void displayHello()
   {
      System.out.println("Hello");
   }
}

class Child extends Parent
{
   public void displayWorld()
   {
      System.out.println("World");
   }
}

public class SingleInheritanceDemo 
{
   public static void main(String[] args) 
   {
      Child obj = new Child();
      obj.displayHello();
      obj.displayWorld();
   }
}

Output:

Hello
World


Multilevel inheritance :

Multilevel inheritance represent hierarchical order where subclass inherits a superclass and also subclass behave as superclass to another class.

Learn Inheritance In Java

class GrandParent 
{ 
   public void displayHello() 
   { 
      System.out.println("Hello"); 
   } 
} 

class Parent extends GrandParent 
{ 
   public void displayWorld() 
   { 
      System.out.println("World"); 
   } 
} 

class Child extends Parent 
{ 
   public void displayJava() 
   { 
      System.out.println("Java"); 
   } 
}

public class MultilevelInheritanceDemo 
{
   public static void main(String[] args) 
   {
      Child obj = new Child();
      obj.displayHello();
      obj.displayWorld();
      obj.displayJava();
   }
}

Output:

Hello
World
Java


Hierarchical inheritance :

In hierarchical inheritance one superclass serves as subclass for more than one subclass.

Learn Inheritance In Java

class GrandParent 
{ 
   public void displayHello() 
   { 
      System.out.println("Hello"); 
   } 
} 

class Parent extends GrandParent 
{ 
   public void displayWorld() 
   { 
      System.out.println("World"); 
   } 
} 

class Child extends Parent 
{ 
   public void displayJava() 
   { 
      System.out.println("Java"); 
   } 
}

public class HierarchicalInheritanceDemo 
{
   public static void main(String[] args) 
   {
      Child obj1 = new Child();
      obj1.displayHello();
      Parent obj2 = new Parent();
      obj2.displayWorld();
      obj1.displayJava();
   }
}

Output:

Hello
World
Java


Multiple inheritance :

In multiple inheritance one subclass have more than one super class. This will result in a bug so java does not support multiple inheritance.

Also read – interface in java

Multiple inheritance can be achieved only through interfaces.

Learn Inheritance In Java

interface A 
{ 
   public void displayHello(); 
} 

interface B 
{ 
   public void displayWorld(); 
} 

interface C extends A, B 
{ 
   public void displayHello(); 
}

class Demo implements C 
{ 
   @Override
   public void displayHello() 
   { 
      System.out.println("Hello"); 
   } 

   public void displayWorld() 
   { 
      System.out.println("World"); 
   } 
}

public class MultipleInheritanceDemo 
{
   public static void main(String[] args) 
   {
      Demo obj = new Demo();
      obj.displayHello();
      obj.displayWorld();
      obj.displayHello();
   }
}

Output:

Hello
World
Hello

Hybrid inheritance : is a combination of hierarchical and multiple inheritance. Like multiple inheritance, hybrid inheritance can be achieved only through interfaces.

Learn Inheritance In Java

Related Posts