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 Explained
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 Explained

Last updated: Sep 29, 2023 5:19 pm
By Meenakshi Agarwal
Share
6 Min Read
Java Inheritance
Java Inheritance
SHARE

This tutorial will guide you on what is Inheritance in Java. You will know how to use Inheritance for a class and reuse its properties.

Contents
Inheritance concept in JavaDescription:Syntax:Inheritance example:Super keywordDescription:Super keyword example:Related Posts

Basics of Inheritance in Java

You can go through the following sections to learn about Java Inheritance.

Inheritance concept in Java

Inheritance is one of the most-used features of Object-Oriented Programming. It denotes a methodology through which a newly built class extracts features (methods and variables/fields) from an already existing class.

Description:

The Inheritance represents a special relationship between classes. And, every relationship requires a minimum of two parties.

1. One is the primary class, a.k.a., base class or superclass which has a set of properties and methods.
2. Another is the child class which links up to the base class and inherits almost every feature the parent class offers.

With the implementation of inheritance, information is available in a hierarchical order.

Inheritance tree

In Java, you can use the extend keyword to implement the concept of inheritance. The use of inheritance comes into play when the programmer does not wish to define the same set of methods or fields for the new class if the parent already has them.

Syntax:

Here, we have captured the general syntax that you can use to define inheritance in a Java program. Please note that you will need a minimum of two classes to use the extend keyword:

class Parent {

   // methods
   // fields

   // ……
}

class Child extends Parent {

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

Inheritance example:

The example below will help you visualize the implementation of inheritance.

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). When the object cal comes to life, it gets a copy of the contents of the Calculator class by default.

Java facilitates all this with the help of constructors. It is important to note constructors are not inherited by subclass as they are neither methods nor fields. However, the child class can invoke the constructor of its parent.

Therefore, the output is:

3
-1
2
0

Super keyword

Description:

The super keyword provides a mechanism by which a subclass can invoke the constructor of the superclass. In case members and fields of both, classes have identical names, the super keyword helps differentiate between the superclass members from the subclass members.

The super keyword is very much like this keyword but only used for the superclass, within the subclass.

If the superclass has a predefined parametrized constructor, the subclass needs to invoke the superclass constructor using the super keyword.

Super keyword example:

The following example shows the use of the super keyword:

class Parent {

    int num = 1;
    int a;
    
    Parent(int a)
    {
        this.a = a;
    }
    
    void disp( ) {
        System.out.println("Number from Parent class " + num);
    }
    
    void getA( ) {
        System.out.println("Value of a " + a);
    }
}

public class Child extends Parent {

    int num = 2;
    
    Child(int a) {
        super( a );
    }
    
    void disp( ) {
        System.out.println("Number from Child class " + num);
    }
    
    void methodToCallSuperKeyword( ) {
        System.out.println(super.num);
        super.disp();
    }

    public static void main(String args[]) {
    
        Child child = new Child(100);
        
        child.methodToCallSuperKeyword();
        System.out.println(child.num);
        
        child.disp();
        child.getA();
    }
}

The output for the above snippet is:

1
Number from Parent class 1
2
Number from Child class 2
Value of a 100

The Java instanceof operator can be used to check if a particular instance belongs to a class. For an inherited class, the instance of the subclass is an instance of both subclass and superclass.

Thus in the example above:

System.out.println(child instanceof Child);
System.out.println(child instanceof Parent);

Outputs:

true
true

Based on the example above, the Parent is the superclass of the Child class. In terms of the IS-A relationship, we can justify the Child’s IS-A Parent. By using the extend keyword, a subclass claims all the properties of its superclass, except for the private members.

HAS-A relation defines a way to determine if a class has a certain thing. For example in the above example the Child class HAS-A methodToCallSuperKeyword().

It is important to realize that Java does not support Multiple Inheritance. This means one class cannot extend multiple classes. However, with the help of interfaces, it is possible to achieve multiple inheritance in Java.

Related Posts

1. Class in Java
2. Constructor 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 Constructor in Java Java Constructor
Next Article Java interfaces with examples Java Interfaces Explained

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