Java Inheritance Keyword Guide

Benjamin Jul 11, 2026

In the realm of object-oriented programming, inheritance is a powerful feature that enables the creation of new classes based on existing ones. In Java, this concept is widely used to establish a 'is-a' relationship between classes, promoting code reusability and maintaining a hierarchical structure. When discussing inheritance in Java, several key terms and concepts arise, each playing a crucial role in understanding and implementing this fundamental principle.

three different types of inferiances are shown on the diagram, and one is labeled
three different types of inferiances are shown on the diagram, and one is labeled

Before delving into the specifics, let's briefly explore the primary benefits of inheritance in Java. Inheritance allows new classes to inherit attributes and methods from existing ones, reducing code duplication and promoting a consistent design. It also enables polymorphism, a feature that allows methods to act differently based on the object that they are acting upon.

Inheritance in Java Explained | OOP Concepts for Beginners
Inheritance in Java Explained | OOP Concepts for Beginners

Key Inheritance Concepts in Java

To grasp the intricacies of inheritance in Java, we must first understand several key concepts that form its foundation.

10 Example of this keyword in Java
10 Example of this keyword in Java

One such concept is the 'superclass' and 'subclass' relationship. A superclass, also known as a base class or parent class, is the class from which other classes are derived. A subclass, on the other hand, is a class that is derived from another class, inheriting its properties and behaviors.

Superclass and Subclass Relationship

Java Programming
Java Programming

The relationship between a superclass and its subclasses is crucial in understanding inheritance. The superclass provides the basic structure and functionality, while subclasses can extend or override these features to suit their specific needs.

For instance, consider a simple example with an 'Animal' superclass and 'Dog' and 'Cat' subclasses:

public class Animal {
    public void makeSound() {
        System.out.println("The animal makes a sound");
    }
}

public class Dog extends Animal {
    @Override
    public void makeSound() {
        System.out.println("The dog barks");
    }
}

public class Cat extends Animal {
    @Override
    public void makeSound() {
        System.out.println("The cat meows");
    }
}

In this example, 'Animal' is the superclass, and 'Dog' and 'Cat' are subclasses that inherit the 'makeSound' method but provide their own implementations.

30 Days of Java – Day 14 | Java Inheritance Explained for Beginners
30 Days of Java – Day 14 | Java Inheritance Explained for Beginners

Inheritance Keywords

Java provides several keywords to facilitate inheritance, with the most fundamental being the 'extends' keyword. The 'extends' keyword is used to indicate that a class is a subclass of another class, inheriting its properties and methods.

Other important keywords include 'super' and 'this'. The 'super' keyword refers to the immediate parent class instance, while 'this' refers to the current instance of the class. These keywords are essential for accessing parent class methods and constructors.

What is Inheritance in Java – 5 Types, Use and Advantages
What is Inheritance in Java – 5 Types, Use and Advantages

Inheritance and Access Modifiers

Inheritance in Java is also closely tied to access modifiers, which control the accessibility of class members. Understanding how access modifiers interact with inheritance is crucial for writing secure and maintainable code.

the key words in this language are used to describe something
the key words in this language are used to describe something
Types of Inheritance in Java: A Comprehensive Guide
Types of Inheritance in Java: A Comprehensive Guide
types of inheritance 💪
types of inheritance 💪
Java List of Keywords (All Reserved Words) | Complete Java Reference Guide
Java List of Keywords (All Reserved Words) | Complete Java Reference Guide
the different types of children's names and their meanings are shown in this diagram
the different types of children's names and their meanings are shown in this diagram
Types of Inheritance in Java | OOP Concepts Explained
Types of Inheritance in Java | OOP Concepts Explained
Inheritance in Java
Inheritance in Java
Famous Keywords in Java
Famous Keywords in Java
Types of Inheritance in Programming
Types of Inheritance in Programming
Mastering Inheritance in Java with the `super` Keyword
Mastering Inheritance in Java with the `super` Keyword
Java Final Keyword Cheat Sheet – Explained Simply
Java Final Keyword Cheat Sheet – Explained Simply
Super Keyword in Java Explained Simply | Java OOP Concepts | BCA Notes
Super Keyword in Java Explained Simply | Java OOP Concepts | BCA Notes
java keywords  you must know
java keywords you must know
What is Inheritance in Java with example - Object Oriented Programming Tutorial
What is Inheritance in Java with example - Object Oriented Programming Tutorial
OOP Inheritance Types Explained – Single, Multiple, Hybrid & More
OOP Inheritance Types Explained – Single, Multiple, Hybrid & More
✅ Java Inheritance Example Explained Simply
✅ Java Inheritance Example Explained Simply
Keywords in Java - Java Reserved Words - TechVidvan
Keywords in Java - Java Reserved Words - TechVidvan
Type of Inheritance in Python
Type of Inheritance in Python
Java Inheritance - Learn its Importance and Types with Coding Examples.
Java Inheritance - Learn its Importance and Types with Coding Examples.
Inheritance
Inheritance

Java provides four access modifiers: public, private, protected, and default (or package-private). Each modifier has specific rules regarding inheritance:

  • Public: Public methods and fields can be accessed from any class, including subclasses.
  • Private: Private methods and fields are only accessible within the same class, not even subclasses can access them directly.
  • Protected: Protected methods and fields can be accessed within the same package and by subclasses, even if they are in a different package.
  • Default (or package-private): These methods and fields can only be accessed within the same package.

Protected Access Modifier and Inheritance

The protected access modifier is particularly relevant to inheritance. It allows subclasses to access protected methods and fields, even if they are in a different package. This promotes code reuse while maintaining some level of encapsulation.

For example, consider a 'Vehicle' superclass with a protected method 'startEngine()' and a 'Car' subclass in a different package:

// Vehicle class in package com.example.vehicles
public class Vehicle {
    protected void startEngine() {
        System.out.println("Engine started");
    }
}

// Car class in package com.example.cars
public class Car extends Vehicle {
    @Override
    protected void startEngine() {
        System.out.println("Car engine started");
    }
}

In this case, the 'Car' subclass can access and override the protected 'startEngine()' method from the 'Vehicle' superclass.

Method Overriding and Inheritance

Method overriding is a key feature of inheritance in Java, allowing subclasses to provide their own implementation of a method that is already defined in their superclass. This enables polymorphism and allows objects of different classes to respond to the same method call in different ways.

To override a method in a subclass, you must use the '@Override' annotation to indicate that you intend to override a method from the superclass. This helps catch errors at compile time if the method signature does not match exactly.

Method Overriding Rules

To override a method correctly in Java, you must adhere to specific rules:

  1. The method in the subclass must have the same name and return type as the method in the superclass.
  2. The method in the subclass must have the same or broader access modifier than the method in the superclass (e.g., if the superclass method is protected, the subclass method can be protected or public).
  3. The method in the subclass can throw a broader exception or no exception at all, but not a narrower exception.

For example, consider the 'makeSound()' method from the earlier 'Animal' and 'Dog' classes:

public class Animal {
    public void makeSound() {
        System.out.println("The animal makes a sound");
    }
}

public class Dog extends Animal {
    @Override
    public void makeSound() {
        System.out.println("The dog barks");
    }
}

In this case, the 'Dog' subclass overrides the 'makeSound()' method from the 'Animal' superclass, providing its own implementation.

Method Hiding vs. Method Overriding

It's essential to understand the difference between method hiding and method overriding in Java. Method hiding occurs when a subclass provides a method with the same name and return type as a method in the superclass, but the subclass method has a different return type or throws a different exception.

Method hiding is less common than method overriding and can lead to unexpected behavior if not used carefully. To avoid method hiding, ensure that the method in the subclass has the same return type and throws the same or broader exceptions as the method in the superclass.

In conclusion, inheritance is a powerful feature in Java that enables code reuse, promotes consistent design, and facilitates polymorphism. By understanding the key concepts, access modifiers, and method overriding rules, developers can effectively leverage inheritance to create robust and maintainable Java applications. To further explore inheritance, consider experimenting with different inheritance hierarchies and observing the behavior of subclasses and superclasses. Additionally, familiarize yourself with other related concepts, such as abstract classes and interfaces, to gain a comprehensive understanding of Java's object-oriented programming capabilities.