THE GENERAL BLOG

Understanding the Concept of Inheritance in Java with application

Posted on February 13, 2025

Inheritance is a fundamental concept in Java (and in object-oriented programming in general) that allows classes to inherit properties and behaviors from other classes. Let's dive into concept of inheritance more deeply with a lots of examples.

What is Inheritance ?

In Java, inheritance refers to the mechanism by which a class can inherit properties and methods from another class. The class from which properties and methods are inherited is called the superclass or parent class or base class, and the class that inherits those properties and method is called the subclass or child class.

Example:

// parent class or base class
public class Animal {
    public void eat(){
        System.out.println("eating....");
    }
    }
    // child class or subclass
    public class Dogs extends Animal {
    public void bark(){
        System.out.println("Dog is barking");
    }
}
// Main.java
public class Main {
    public static void main(String[] args) {
    Dogs dog =new Dogs();
       dog.eat();
       dog.bark();
    }
}
Output
eating....
Dog is barking

Note: Extend keywords are used to inherit the properties of the class.

Types of Inheritance:

  1. Single Inheritance

  2. Multilevel inheritance

  3. Hierarchical Inheritance

  4. Hybrid Inheritance

Single Inheritance

A class can inherit attributes and methods from only one parent class. In other words, a subclass can extend only one superclass.

single inheritence

Example:

// parent class or base class
public class Animal {
    public void eat(){
        System.out.println("eating....");
    }
    }
    // child class or subclass
    public class Dogs extends Animal {
    public void bark(){
        System.out.println("Dog is barking");
    }
}
// Main.java
public class Main {
    public static void main(String[] args) {
    Dogs dog =new Dogs();
       dog.eat();
       dog.bark();
    }
}
Output
eating....
Dog is barking

Multilevel Inheritance

The Inheritance in which class inherits methods and properties from a class that is already deriving from another class is called multilevel inheritance.

Multilevel inheritance

Example:

// Parent class
public class Animal {
    public void eat(){
        System.out.println("eating....");
    }
    }
//    subclass of Animal  
    public class Dogs extends Animal {
    public void bark(){
        System.out.println("Dog is barking");
    }
    public void greeting(){
        System.out.println("welcome to animal family");
    }
}

// subclass of Dogs
public class cat extends Dogs {
    public void sound(){
        System.out.println("Meow...");
    }
    }

// Main.java
    public class Main {
    public static void main(String[] args) {

       cat Cat=new cat();
       Cat.eat();
       Cat.sound();
       Cat.greeting();
    }
}
Output
eating....
Meow...
welcome to animal family

Hierarchical Inheritance

In Hierarchical inheritance, more than one sub-class inherits the property of a single base class. There is one base class and multiple derived classes.

 Hierarchical Inheritance

// Parent class or base class
public class Vehicle {
    void speedUp() {
        System.out.println("Speeding up...");
    }

    void brake() {
        System.out.println("Braking...");
    }
}

// Subclass of Vehicle
public class Car extends Vehicle{
    public void Greeting(){
        System.out.println("Hello I am Car");
    }
}

// Subclass of Vehicle
public class Bike extends Vehicle {

    public void Greeting(){
        System.out.println("Hello I am Bike");
    }
}

// Subclass of Vehicle
public class Truck extends Vehicle {

    public void Greeting(){
        System.out.println("Hello I am Truck");
    }
}
output
Braking...
Hello I am Car
Speeding up...
Hello I am Bike
Speeding up...
Braking...
Hello I am Truck

Hybrid Inheritance

It is a mix of two or more types of inheritance.Since java doesn't support multiple inheritance with classes. It can be achieved through a combination of Multilevel Inheritance and Hierarchical Inheritance with classes, Hierarchical and Single Inheritance with classes.

Hybrid Inheritance

// Parent class or Base class

public class Vehicle {
    void speedUp() {
        System.out.println("Speeding up...");
    }

    void brake() {
        System.out.println("Braking...");
    }
}

// subclass  of  Truck

public class Car extends Truck{
    public void Greeting(){
        System.out.println("Hello I am Car");
    }
}

// subclass of Vehicle

public class Truck extends Vehicle {

    public void Greeting(){
        System.out.println("Hello I am Truck");
    }
    public void Brand(){
        System.out.println("TATA");
    }
}

// Main.java

public class Main {
    public static void main(String[] args) {

       cat Cat=new cat();
       Cat.eat();
       Cat.sound();
       Cat.greeting();
    }
}
Output

Braking...
Hello I am Car
TATA
Speeding up...
TATA


The Most Popular Blog

The best tips and tricks on managing digital documents

How to convert docx file to pdf online free ?

Presentations created in DOCX format might need to be shared with clients or partners. Converting them to

Read More >

How to convert Docx file to Epub online free using converteasly ?

Teachers, educators, and instructional designers can convert lesson plans, textbooks, educational guides, and study materials from DOCX to EPUB

Read More >

Unlocking the Power of Java Multithreading

Multithreading is a programming technique that enables a program to execute multiple threads or flows of execution concurrently

Read More >

How to Remove Background of Image using converteasly

In today digital world, having images with transparent backgrounds is often a necessity for designers, content creators, and anyone looking to create polished visuals.

Read More >

How to Create and Download Your Digital Signature Securely Online

Draw your signature safely online with Converteasly — fast, free, and privacy-first.

Read More >

How to convert png to jpg or jpeg online using converteasly ?

Converting PNG images to JPG format can significantly reduce the file size, making it more suitable for web publishing, email attachments, or

Read More >

Free tool to convert Excel (.xls/.xlsx) file to PDF online.

When you want to share your spreadsheet data with others who may not have Excel or who need a format that cannot be easily edited, converting to PDF

Read More >

Free tool to convert text to pdf online with no restriction

A free tool to convert one or multiple text files to PDF online, at no-cost, no-registration, and no-installation needed.

Read More >

Simple steps to delete pages from your PDF file online using converteasly

Merge PDF functionality is helpful for compiling e-books or digital publications. Authors or publishers can combine individual chapters

Read More >

How to convert Rich Text Format (.rtf) file to PDF online using converteasly ?

Legal professionals often convert legal documents, agreements, and contracts from RTF to PDF to maintain document integrity and

Read More >

How to convert image to pdf online using converteasly?

A free tool to convert one or multiple images to PDF online, at no-cost, no-registration, and no-installation needed.

Read More >

How to convert jpg or jpeg to png online using converteasly ?

Converting JPG to PNG can be useful when you want to compress an image while maintaining its quality.

Read More >

Unlocking Creativity: Understanding Generative AI and Its Everyday Applications.

Welcome to the world of Generative AI, where algorithms transform data into something entirely new, giving machines the ability to create, imagine, and innovate.

Read More >

How to Merge one or multiple PDFs into single PDf online ?

Merge PDF functionality is helpful for compiling e-books or digital publications. Authors or publishers can combine individual chapters

Read More >

Understanding the concept of Encapsulation and Polymorphism in java

Encapsulation is a fundamental principle in object-oriented programming (OOP) where the internal state of an object is hidden from outside access.

Read More >

How HashSet Works Internally in Java — Step-by-Step Explanation with Example

Understanding HashSet in Java: Internal Working, HashMap Relation, and Efficiency Explained

Read More >

How to compress or reduce image size online using converteasly ?

Compressing images is crucial for optimizing website performance. Large image file sizes can significantly impact page load times, leading to slower website speeds

Read More >

How to rotate image left and right online using converteasly ?

Images captured with digital cameras or smartphones may sometimes have incorrect orientations due to device positioning.

Read More >

How to convert webp to jpg or jpeg or png online using converteasly ?

By converting WebP images to JPEG or PNG, you ensure compatibility with a broader range of devices, applications,

Read More >

Understanding Git and GitHub: A Beginner's Guide with Simple Examples

If you're diving into the world of coding, you've probably heard about Git and GitHub. They're like the dynamic duo of version control, helping developers

Read More >

How to convert Pdf To Docx file online free using converteasly ?

If you have received a document in PDF format but need to continue working on it converting it to DOCX allows you to seamlessly resume

Read More >

Simple steps to split single PDF into multiple PDF online using converteasly

you can extract a single chapter from a large book or isolate specific sections for reference or distribution.

Read More >

Simple and Free tool to merge multiple Docx file into single Docx online.

When preparing presentations, different team members might be responsible for various sections. A merge DOCX tool lets you bring together

Read More >

How to convert PDF to Image online using converteasly?

Simple steps to convert PDF to images online, at no-cost, no-registration, and no-installation needed.

Read More >

Exploring JDK 17 Features: A Comprehensive Guide

Java Development Kit (JDK) 17, released in September 2021, is the Long-Term Support (LTS) version of the Java programming language.

Read More >

How to Escape JSON Using Converteasly

Dealing with JSON data often requires ensuring that the text is properly escaped to avoid errors during processing.

Read More >

How to Unescape JSON Using Converteasly

Are you dealing with escaped JSON that needs to be converted back to its original form?

Read More >

Understanding equals() and hashCode() in Java with Examples

Learn the difference between equals() and hashCode() in Java, their relationship, and why they matter when working with collections like HashMap and HashSet.

Read More >

How to Convert JPG/PNG to WebP Using Converteasly.com

Optimizing images for the web is essential for faster loading times and better user experience. Converting JPG and PNG images to WebP format is an effective way to achieve this.

Read More >

How to convert Rich Text Format (.rtf) file to Epub online free using converteasly ?

Educational institutions and educators can convert RTF-based textbooks, study guides, and educational resources into EPUB format

Read More >

© 2025 converteasly.com - Made with 💕 for the people of the internet.

Consent PreferencesPrivacy PolicyDisclaimerT&C