Skip to main content
Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Resources
    • Cheatsheets
    • Tech Comparisons
  • Languages
    • Angular Angular js ASP.NET Asp.net Core ASP.NET Core, C# ASP.NET MVC ASP.NET Web Forms C C# C#, ASP.NET Core, Dapper
      C#, ASP.NET Core, Dapper, Entity Framework DotNet General Web Development HTML, CSS HTML/CSS Java JavaScript JavaScript, HTML, CSS JavaScript, Node.js Node.js
      Python Python 3.11, Pandas, SQL Python 3.11, SQL Python 3.11, SQLAlchemy Python 3.11, SQLAlchemy, SQL Python 3.11, SQLite React Security SQL Server TypeScript
  • Post Blog
  • Tools
    • Beautifiers
      JSON Beautifier HTML Beautifier XML Beautifier CSS Beautifier JS Beautifier SQL Formatter
      Dev Utilities
      JWT Decoder Regex Tester Diff Checker Cron Explainer String Escape Hash Generator Password Generator
      Converters
      Base64 Encode/Decode URL Encoder/Decoder JSON to CSV CSV to JSON JSON to TypeScript Markdown to HTML Number Base Converter Timestamp Converter Case Converter
      Generators
      UUID / GUID Generator Lorem Ipsum QR Code Generator Meta Tag Generator
      Image Tools
      Image Converter Image Resizer Image Compressor Image to Base64 PNG to ICO Background Remover Color Picker
      Text & Content
      Word Counter PDF Editor
      SEO & Web
      SEO Analyzer URL Checker World Clock
  1. Home
  2. Blog
  3. Java
  4. Understanding Inheritance in Java: A Complete Guide with Examples

Understanding Inheritance in Java: A Complete Guide with Examples

Date- Dec 09,2023 Updated Mar 2026 3274
csharp inheritance

Understanding Inheritance

Inheritance is a core principle of object-oriented programming (OOP) that allows a class (called the derived or child class) to inherit attributes and methods from another class (called the base or parent class). This mechanism not only promotes code reusability but also allows for the extension and customization of existing functionalities. By using inheritance, developers can create a hierarchy of classes that model real-world relationships, making the code easier to understand and maintain.

In Java, inheritance is implemented using the extends keyword. This allows a child class to inherit all public and protected members (fields and methods) of its parent class, which can be particularly useful in scenarios where multiple classes share common behaviors.

Types of Inheritance

Single Level Inheritance

In single-level inheritance, a single derived class inherits from a single base class. This is the simplest form of inheritance and is often used in straightforward applications.

class School {
    void displaySchool() {
        System.out.println("My School Name Is XYZ");
    }
}

class Class1 extends School {
    void displayClass1() {
        System.out.println("This is Class1");
    }
}

public class Main {
    public static void main(String[] args) {
        Class1 c1 = new Class1();
        c1.displayClass1();
        c1.displaySchool();
    }
}

Multi-Level Inheritance

In multi-level inheritance, a class is derived from another derived class, creating a chain of inheritance. This allows for more complex relationships and is useful in scenarios requiring multiple layers of abstraction.

class School {
    School() {
        System.out.println("Constructor called at run-time");
    }
    void displaySchoolName() {
        System.out.println("This is School XYZ");
    }
}

class Class1 extends School {
    void displayClass1() {
        System.out.println("This is Class1");
    }
}

class SectionA extends Class1 {
    void displaySectionA() {
        System.out.println("This is Section A of Class1");
    }
}

public class Main {
    public static void main(String[] args) {
        SectionA sa = new SectionA();
        sa.displaySectionA();
        sa.displayClass1();
        sa.displaySchoolName();
    }
}

Hierarchical Inheritance

Hierarchical inheritance occurs when multiple derived classes inherit from a single base class. This allows for the reuse of common functionality while still providing specific implementations in the derived classes.

class School {
    void displaySchoolName() {
        System.out.println("This is School XYZ");
    }
}

class Class1 extends School {
    void displayClass1() {
        System.out.println("This is Class1");
    }
}

class Class2 extends School {
    void displayClass2() {
        System.out.println("This is Class2");
    }
}

public class Main {
    public static void main(String[] args) {
        Class1 c1 = new Class1();
        Class2 c2 = new Class2();
        c1.displayClass1();
        c1.displaySchoolName();
        c2.displayClass2();
        c2.displaySchoolName();
    }
}

Additional Types of Inheritance

Multiple Inheritance

Java does not support multiple inheritance directly through classes to avoid ambiguity. However, it can be achieved using interfaces, where a class can implement multiple interfaces, allowing for a form of multiple inheritance.

interface Interface1 {
    void method1();
}

interface Interface2 {
    void method2();
}

class Class1 implements Interface1, Interface2 {
    public void method1() {
        System.out.println("Method from Interface1");
    }
    public void method2() {
        System.out.println("Method from Interface2");
    }
}

public class Main {
    public static void main(String[] args) {
        Class1 obj = new Class1();
        obj.method1();
        obj.method2();
    }
}

Multilevel and Hierarchical Inheritance Combined

It is possible to combine multilevel and hierarchical inheritance to create a complex class structure. This allows for a more robust design where classes can inherit from multiple layers while also being part of a broader hierarchy.

class Animal {
    void eat() {
        System.out.println("Animal is eating");
    }
}

class Mammal extends Animal {
    void walk() {
        System.out.println("Mammal is walking");
    }
}

class Dog extends Mammal {
    void bark() {
        System.out.println("Dog is barking");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.eat();
        dog.walk();
        dog.bark();
    }
}

Edge Cases & Gotchas

While inheritance is powerful, it is essential to be aware of potential pitfalls. One common issue is the diamond problem, which occurs when a class inherits from two classes that have a common ancestor, potentially leading to ambiguity in method resolution.

Another edge case is when overriding methods. If a method in the parent class is marked as final, it cannot be overridden in the child class, which can lead to unexpected behaviors if not properly accounted for.

Performance & Best Practices

When using inheritance, it is crucial to follow best practices to maintain readability and performance. Here are some best practices:

  • Favor Composition Over Inheritance: In many cases, using composition (where a class contains references to other classes) can lead to more flexible and maintainable code.
  • Use Abstract Classes and Interfaces: They allow you to define a contract for subclasses, promoting a clear structure and reducing coupling.
  • Limit the Depth of Inheritance: Deep inheritance hierarchies can lead to complex code that is hard to understand. Aim for a shallow hierarchy whenever possible.
  • Document Inherited Methods: Ensure that inherited methods are well-documented to prevent confusion about their behavior in derived classes.

Conclusion

Inheritance is a fundamental concept in Java that provides significant benefits when used correctly. By understanding the various types of inheritance and following best practices, developers can create more robust and maintainable code.

  • Inheritance promotes code reusability and flexibility.
  • Java supports single, multi-level, hierarchical, and multiple inheritance through interfaces.
  • Be aware of edge cases like the diamond problem and method overriding issues.
  • Follow best practices to maintain code quality and performance.

S
Shubham Saini
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

Understanding Polymorphism in Java: A Complete Guide with Examples
Dec 09, 2023
Mastering Inheritance in C++: A Complete Guide with Examples
Dec 09, 2023
Mastering Method Overloading in Java: A Complete Guide with Examples
Dec 09, 2023
Method Overriding in Java
Sep 13, 2023
Previous in Java
Understanding Abstraction in Java: A Complete Guide with Examples
Next in Java
Understanding Constructors in Java: A Complete Guide with Example…
Buy me a pizza

Comments

🔥 Trending This Month

  • 1
    HTTP Error 500.32 Failed to load ASP NET Core runtime 6,939 views
  • 2
    Error-An error occurred while processing your request in .… 11,281 views
  • 3
    Comprehensive Guide to Error Handling in Express.js 236 views
  • 4
    ConfigurationBuilder does not contain a definition for Set… 19,464 views
  • 5
    Complete Guide to Creating a Registration Form in HTML/CSS 4,218 views
  • 6
    Mastering Unconditional Statements in C: A Complete Guide … 21,507 views
  • 7
    Mastering JavaScript Error Handling with Try, Catch, and F… 162 views

On this page

🎯

Interview Prep

Ace your Java interview with curated Q&As for all levels.

View Java Interview Q&As

More in Java

  • User-defined data types in java 6287 views
  • Master Java Type Casting: A Complete Guide with Examples 6256 views
  • How to add (import) java.util.List; in eclipse 5851 views
  • org.openqa.selenium.SessionNotCreatedException: session not … 5791 views
  • java.lang.IllegalStateException: The driver executable does … 5122 views
View all Java posts →

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1770
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • SQL Formatter
  • Diff Checker
  • Regex Tester
  • Markdown to HTML
  • Word Counter
More Tools
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Base64 Encoder
  • JWT Decoder
  • UUID Generator
  • Image Converter
  • PNG to ICO
  • SEO Analyzer
By Language
  • Angular
  • Angular js
  • ASP.NET
  • Asp.net Core
  • ASP.NET Core, C#
  • ASP.NET MVC
  • ASP.NET Web Forms
  • C
  • C#
  • C#, ASP.NET Core, Dapper
  • C#, ASP.NET Core, Dapper, Entity Framework
  • DotNet
  • General Web Development
  • HTML, CSS
  • HTML/CSS
  • Java
  • JavaScript
  • JavaScript, HTML, CSS
  • JavaScript, Node.js
  • Node.js
  • Python
  • Python 3.11, Pandas, SQL
  • Python 3.11, SQL
  • Python 3.11, SQLAlchemy
  • Python 3.11, SQLAlchemy, SQL
  • Python 3.11, SQLite
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy
Accessibility
Text size
High contrast
Grayscale
Dyslexia font
Highlight links
Pause animations
Large cursor