Hierarchical inheritance in java language

Hierarchical inheritance in java language

In this tutorial, we will discuss the subject of Hierarchical inheritance in java language
Hierarchical inheritance is when multiple classes inherit from a single class. There are one parent (super) class and many children(sub) classes. Hierarchical inheritance is supported in Java. In the example below, class B, class C and class D inherit the properties of class A. Class A acts as the parent class of class B, class C and class D.
In the diagram above, a class can have more than one child class (subclasses) or two or more children classes can only have one (same) parent class. Here, Class A acts as the parent for subclasses class B, class C and class D. This is known as hierarchical inheritance.
Let’s try to understand using the following syntax

Syntax
class A
{
public void method_A
    {
//Block of code
    }
}
class_B extends class_A
{
public void method_A
    {
//Block of code
    }
}
class_C extends class_A
{
public void method_A
    {
//Block of code
    }
}
class MainClass{
public static void main(String args[]){
B obj1=new B(); // object created class B
obj1.method_A(); //call method_A() method of class B
obj1.method_B(); //call method_B() method of class B
C obj2=new C(); // object created class C
obj2.method_C(); //call method_C() method of class C
obj1.method_A(); //call method_A() method of class C
}
}
Example
Program 1
Program 2

 

Related Links
Inheritance in Java                     Inheritance in C++
Interface in Java                         Multiple Inheritance in C++
Karmehavannan

Recent Posts

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

11 months ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

11 months ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

11 months ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

11 months ago

Function/method to convert Celsius into Fahrenheit -Entered by user

Function/method to convert Celsius into Fahrenheit -Entered by user In this article, we will discuss…

11 months ago

Temperature conversion: Celsius into Fahrenheit using function or method

Temperature conversion: Celsius into Fahrenheit using a function or method In this article, we will…

11 months ago

This website uses cookies.