• Inheritance In Java | Java Tutorial

    Inheritance is a process where one class inherits the properties of another class.

    Inheritance In Java

    Let’s say we have two classes namely Parent Class and Child Class. The child class is also known as Derived Class. As per the above definition, the Child class inherits the properties of the Parent Class. The main purpose of Inheritance is to obtain Code Reusability. We can achieve run time polymorphism with inheritance. We will see what is run-time polymorphism in a few minutes.

    Check this link to learn what is a Class and Object

    Assume we have a Class named Laptop, Apple MacBook Pro, Lenovo Yoga. Apple MacBook Pro and Lenovo Yoga classes extend the Laptop Class to inherit the properties of the Laptop Class.

    We will see a detailed explanation with some example programs about Inheritance in the post related to Inheritance.

    In inheritance, we use two keywords namely extends and implements

    Extends:

    We use the extends keyword in Java to allow the child class to inherit all the properties (data members and methods) of the parent class and in addition to these, we can also create new data members and methods. If the properties are private then the child class cannot inherit those properties from the parent class.

    Technically, we say that child class has an IS-A relationship with the parent class

    As per the IS-A relationship, we can say QA IS-A Employee

    Employee Class:

    QA Class:

    In the above example, the QA object can access the properties of its own as well as the Employee class.

    Implements:

    We use implements keyword in Java to inherit the properties from an interface. Interfaces cannot be extended by the classes.

    We will see this in the Interface section.

    Types of Inheritance:

    1. Single Inheritance
    2. Multilevel Inheritance
    3. Hierarchical Inheritance

    There are two more inheritances as Multiple and Hybrid Inheritance which are supported through interface only. We will see those in the Interface section.

    Single Inheritance:

    Single Inheritance in Java refers to a child and parent class relationship. In this one class extends another class say Class B extends Class A

    Single Inheritance

    Output:

    Multilevel Inheritance:

    Multilevel Inheritance in Java refers to a child and parent class relationship. In this a class extends a child class say Class C extends Class B and Class B extends Class A

    Multilevel Inheritance

    Output:

    Hierarchical Inheritance:

    Hierarchical Inheritance in Java refers to a child and parent class relationship. In this more than one class extends the same class say Class B, Class C extends Class A

    Hierarchical Inheritance

    Output:

    Multiple Inheritance:

    Multiple Inheritance is not possible in Java. In this a child extends two parents classes say Class C extends Class A and Class B.

    Multiple Inheritance

    Hybrid Inheritance:

    Combination of more than one type of inheritance.

    Hybrid Inheritance

    Must Read: Java Tutorial

    Sharing is caring.

    Share on facebook
    Facebook
    Share on twitter
    Twitter
    Share on linkedin
    LinkedIn

    Like This Post?

    We have a lot more where that came from?

    We only send really good stuff occasionally, promise.

    Rajkumar SM

    Leave a Comment

    Your email address will not be published. Required fields are marked *

    Share on facebook
    Share on google
    Share on twitter
    Share on linkedin
    Share on whatsapp

    Recent Posts:

    Scroll to Top
    API Testing eBook

    DOWNLOAD FOR FREE