TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: Java Inheritance Types
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
Java OOPJava Tutorials

Java Inheritance Types

Last updated: Sep 29, 2023 4:36 pm
By Meenakshi Agarwal
Share
6 Min Read
types of inheritance in java
SHARE

From this tutorial, you will learn about different inheritance types available in Java. It will also teach you how to use them in Java programs with the help of code and examples.

Contents
Single InheritanceMultiple InheritanceMultilevel InheritanceHierarchical InheritanceHybrid InheritanceRelated Posts

Types of Inheritance in Java – A Beginner’s Guide

Before delving into different types of Inheritance in Java, first, let’s briefly understand what inheritance means.

The first thing to remember is that Inheritance is an important OOP concept in Object-Oriented programming. It is the process through which an already existing class extends its features to a new class. Java supports various kinds of inheritance.

Single Inheritance

As the name would suggest, single inheritance is simply one subclass extending one superclass. It is the simplest of all. You can have a look at the general syntax to implement single inheritance below. Here class Parent is the superclass and the Child is the subclass.

class Parent {

    // methods
    // fields
    // ……
}

class Child extends Parent {

    // already supports the methods and fields in Parent class
    // additional features
}

The below diagram helps you understand the logical flow of this OOP concept.

Fig – 1

Single Inheritance in Java

Now, go through the following code. It implements a Calculator class and its subclass AdvancedCalculator.

class Calculator{

    int add(int a , int b)
    {
        return a+b;
    }
    
    int sub(int a , int b)
    {
        return a-b;
    }
}

public class AdvancedCalculator extends Calculator {

    int mult(int a , int b)
    {
        return a*b;
    }
    
    int div(int a , int b)
    {
        return a/b;
    }
    
    public static void main(String args[]) {
    
        AdvancedCalculator cal= new AdvancedCalculator();
        
        System.out.println(cal.add(1,2));
        System.out.println(cal.sub(1,2));
        System.out.println(cal.mult(1,2));
        System.out.println(cal.div(1,2));
    }
}

The superclass AdvancedCalculator uses already defined add and sub-methods and adds more functionality (product and division). Therefore, the output is:

3
-1
2
0

Multiple Inheritance

Multiple Inheritance is one class extending multiple classes. As discussed before, Java does not support multiple Inheritance. However, with the help of interfaces, we can visualize multiple Inheritance at work.

Conceptually, a visualization for multiple Inheritance would be something like this:

Fig – 2

Multiple Inheritance

The example below shows multiple Inheritance through interfaces:

interface A
{
    public void dis(int num);
}

interface B
{
    public void disp(int num);
}

class Mult implements A, B
{
    public void dis(int num)
    {
        System.out.println("From Interface A number is " + num);
    }
    
    public void disp(int num)
    {
        System.out.println("From Interface B number is " + num);
    }
}

public class Test extends Mult
{
    public static void main(String args[])
    {
        Mult ml = new Mult();
        ml.dis(10);
        ml.disp(20);
    }
}

The output is:

From Interface A number is 10
From Interface B number is 20

Multilevel Inheritance

We call a relationship of Java classes multilevel inheritance in which a superclass extends its features to a subclass. And, the subclass itself acts as a superclass to another class. This means, that once a subclass, will be a future superclass.

Evidently, you can see it from the diagram below. In the picture, a multilevel inheritance relationship is forming like a hierarchy of classes. See the picture below.

Fig – 3

Multilevel Inheritance in Java

The example below is an example of multilevel inheritance:

class A
{
    public void disA()
    {
        System.out.println("ClassA");
    }
}

class B extends A
{
    public void disB()
    {
        System.out.println("ClassB");
    }
}

class C extends B
{
    public void disC()
    {
        System.out.println("ClassC");
    }
}

public class Test extends C
{
    public static void main(String args[])
    {
        C c = new C();
        c.disA();
        c.disB();
        c.disC();
    }
}

The output is:

ClassA
ClassB
ClassC

Hierarchical Inheritance

In hierarchical inheritance, one class acts as a parent class for multiple subclasses. The visual diagram for hierarchical inheritance is given below:

Fig – 4

Hierarchical Inheritance Types
class A
{
    public void disA()
    {
        System.out.println("ClassA");
    }
}

class B extends A
{
    public void disB()
    {
        System.out.println("ClassB");
    }
}

class C extends A
{
    public void disC()
    {
        System.out.println("ClassC");
    }
}

public class Test extends C
{
    public static void main(String args[])
    {
        B b = new B();
        b.disB();
        b.disA();
        
        C c = new C();
        c.disC();
        c.disA();
    }
}

In the example above class A acts as the parent class for all the sub-classes.

The output is :

ClassB
ClassA
ClassC
ClassA

Hybrid Inheritance

Hybrid inheritance is formed as a result of the combination of both single and multiple Inheritance. As multiple Inheritance is not viable with classes, we achieve hybrid inheritance through interfaces.

The visualization for hybrid inheritance is given below:

Fig – 5

Hybrid Inheritance
interface B
{
    public void print();
}

interface C
{
    public void print();
}

class A
{
    public void disA()
    {
        System.out.println("ClassA");
    }
}

class D extends A implements B, C
{
    public void print()
    {
        System.out.println("method print()");
    }

    public void disD()
    {
        System.out.println("ClassD");
    }
}

public class Car extends D{
    public static void main(String args[]) {
        
        D d = new D();
        d.disD();
        d.print();
    }
}

In the example above Class D singly inherits the features of Class A (single inheritance) and at the same time extends both interfaces B and C (Multiple Inheritance)

The output is:

ClassD
method print()

Related Posts

Class and Object in Java
Constructor in Java
Interfaces in Java
Inheritance in Java

You Might Also Like

A Simple Guide to Exception Handling in Java

Difference Between Spring and Spring Boot

How to Use Java String Format with Examples

Java IRC Bot with Sample Code

Generate Random Number in Java – 10 Ways

Meenakshi Agarwal Avatar
By Meenakshi Agarwal
Follow:
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large MNCs, I gained extensive experience in programming, coding, software development, testing, and automation. Now, I share my knowledge through tutorials, quizzes, and interview questions on Python, Java, Selenium, SQL, and C# on my blog, TechBeamers.com.
Previous Article Java interfaces with examples Java Interfaces Explained
Next Article Format or percentage for string formatting in Python Python String Formatting Methods

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x