Understanding Inheritance in Java: A Complete Guide

By • min read
<h2 id="introduction">Introduction to Java Inheritance</h2> <p>Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a class to inherit properties and behaviors from another class. In Java, this mechanism promotes <strong>code reuse</strong>, <strong>method overriding</strong>, and a natural hierarchy among classes. By building relationships between classes, developers can create <em>extensible</em> and <em>maintainable</em> applications.</p><figure style="margin:20px 0"><img src="https://media2.dev.to/dynamic/image/width=1200,height=627,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F98dp3s1w883oldsrf6tg.png" alt="Understanding Inheritance in Java: A Complete Guide" style="width:100%;height:auto;border-radius:8px" loading="lazy"><figcaption style="font-size:12px;color:#666;margin-top:5px">Source: dev.to</figcaption></figure> <p>This article covers the core aspects of inheritance in Java, including the <code>extends</code> keyword, types of inheritance, and practical examples to help you master this essential OOP feature.</p> <h2 id="basics">Superclass and Subclass: The Parent-Child Relationship</h2> <p>In Java inheritance, you'll encounter two key terms:</p> <ul> <li><strong>Superclass (Parent class)</strong> – the class whose attributes and methods are inherited.</li> <li><strong>Subclass (Child class)</strong> – the class that inherits from the superclass.</li> </ul> <p>The subclass can use all accessible members of the superclass (except those marked <code>private</code>) and can also define its own unique members. To establish this relationship, use the <code>extends</code> keyword.</p> <h2 id="extends">Using the <code>extends</code> Keyword</h2> <p>The syntax for inheritance is simple: <code>class Subclass extends Superclass { }</code>. Here's a concrete example:</p> <pre><code>// Superclass class Vehicle { String brand = "Ford"; void honk() { System.out.println("Beep beep!"); } } // Subclass inherits from Vehicle class Car extends Vehicle { String modelName = "Mustang"; } public class Main { public static void main(String[] args) { Car myCar = new Car(); myCar.honk(); // inherited method System.out.println(myCar.brand + " " + myCar.modelName); } }</code></pre> <p>In this example, <code>Car</code> inherits the <code>honk()</code> method and the <code>brand</code> field from <code>Vehicle</code>. The subclass can also access superclass methods directly, as shown.</p> <h2 id="types">Types of Inheritance in Java</h2> <p>Java supports several inheritance patterns, but it does <em>not</em> support multiple inheritance (a class inheriting from more than one class) to avoid ambiguity. The allowed types are:</p> <h3 id="single">1. Single Inheritance</h3> <p>In single inheritance, one subclass inherits from exactly one superclass. This is the most common form.</p> <pre><code>class Animal { void eat() { System.out.println("Eating..."); } } class Dog extends Animal { void bark() { System.out.println("Barking..."); } }</code></pre> <p>Here, <code>Dog</code> inherits <code>eat()</code> from <code>Animal</code>.</p> <h3 id="multilevel">2. Multilevel Inheritance</h3> <p>Multilevel inheritance forms a chain: class A extends class B, and class B extends class C. Each subclass inherits from its immediate parent.</p> <pre><code>class A { void methodA() { System.out.println("Class A method"); } } class B extends A { void methodB() { System.out.println("Class B method"); } } class C extends B { void methodC() { System.out.println("Class C method"); } }</code></pre> <p>Class C can invoke methods from A and B. This hierarchy models real-world relationships like <em>Vehicle → Car → SportsCar</em>.</p> <h3 id="hierarchical">3. Hierarchical Inheritance</h3> <p>In hierarchical inheritance, multiple subclasses inherit from the same superclass. This is useful when several classes share common features defined in one parent.</p><figure style="margin:20px 0"><img src="https://media2.dev.to/dynamic/image/width=50,height=50,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3632178%2Fe62cc11b-0d35-42ab-9c04-d2bccefb6c5c.png" alt="Understanding Inheritance in Java: A Complete Guide" style="width:100%;height:auto;border-radius:8px" loading="lazy"><figcaption style="font-size:12px;color:#666;margin-top:5px">Source: dev.to</figcaption></figure> <pre><code>class Shape { void draw() { System.out.println("Drawing shape"); } } class Circle extends Shape { void area() { System.out.println("Area of circle"); } } class Rectangle extends Shape { void area() { System.out.println("Area of rectangle"); } }</code></pre> <p>Both <code>Circle</code> and <code>Rectangle</code> reuse the <code>draw()</code> method from <code>Shape</code>.</p> <h2 id="important-notes">Important Notes on Inheritance</h2> <ul> <li><strong>Constructor chaining:</strong> When a subclass object is created, the superclass constructor is called first (implicitly or explicitly via <code>super()</code>).</li> <li><strong>Access modifiers:</strong> Only <code>public</code> and <code>protected</code> members are inherited. <code>private</code> members are not accessible directly, but they can be accessed via <code>getter</code>/<code>setter</code> methods if provided.</li> <li><strong>Method overriding:</strong> A subclass can provide a specific implementation of a superclass method (same signature). Use <code>@Override</code> annotation for clarity.</li> <li><strong>Final classes:</strong> A class declared with <code>final</code> cannot be inherited.</li> <li><strong>Multiple inheritance:</strong> Java does not allow a class to extend multiple classes. Instead, use <strong>interfaces</strong> to achieve multiple inheritance of type.</li> </ul> <h2 id="benefits">Benefits of Using Inheritance</h2> <ol> <li><strong>Code reusability:</strong> Write once in the superclass, reuse in all subclasses.</li> <li><strong>Method overriding:</strong> Enables runtime polymorphism – a subclass can provide a specific behavior while keeping the same interface.</li> <li><strong>Logical hierarchy:</strong> Models real-world relationships clearly (e.g., <em>Vehicle</em> → <em>Car</em>).</li> <li><strong>Easier maintenance:</strong> Changes in a superclass propagate to subclasses automatically.</li> </ol> <h2 id="conclusion">Conclusion</h2> <p>Java inheritance is a powerful tool for building modular and scalable applications. By understanding the <code>extends</code> keyword and the different inheritance types (<a href="#single">single</a>, <a href="#multilevel">multilevel</a>, <a href="#hierarchical">hierarchical</a>), you can create clean, reusable class hierarchies. Remember to use access modifiers wisely and consider interfaces when you need the flexibility of multiple inheritance. Practice with the examples above to solidify your skills, and soon you'll harness the full potential of Java's OOP paradigm.</p>