Java Inheritance Keyword: Extends Explained

Benjamin Jul 11, 2026

In the realm of object-oriented programming, inheritance is a fundamental concept that allows new classes to be derived from existing ones. In Java, this is achieved using the 'extends' keyword. Let's delve into the world of inheritance in Java, exploring the 'extends' keyword, its syntax, benefits, and best practices.

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

Inheritance in Java is a powerful tool that promotes code reusability and establishes 'is-a' relationships between classes. It enables a new class, known as the subclass or derived class, to inherit properties and behaviors from an existing class, called the superclass or base class.

Inheritance in Java
Inheritance in Java

Understanding Inheritance in Java

Inheritance in Java is implemented using the 'extends' keyword. It allows a subclass to inherit fields and methods from its superclass, providing a way to create a hierarchical relationship between classes.

the key words in this language are used to describe something
the key words in this language are used to describe something

To illustrate, consider a simple example. Suppose we have a superclass named 'Animal' with basic properties like 'name' and a method 'makeSound()'. We can create a subclass 'Dog' that extends 'Animal' and adds its own unique properties and behaviors:

```java public class Animal { protected String name; public Animal(String name) { this.name = name; } public void makeSound() { System.out.println("The animal makes a sound"); } } public class Dog extends Animal { public Dog(String name) { super(name); } public void makeSound() { System.out.println("The dog barks"); } } ```

Inheriting Fields and Methods

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

In the above example, the 'Dog' class inherits the 'name' field and the 'makeSound()' method from the 'Animal' class. The 'Dog' class can access and use these inherited properties and methods as if they were defined within the 'Dog' class itself.

However, it's crucial to note that private fields and methods are not inherited. They are only accessible within the class they are defined in, promoting encapsulation and data hiding.

Overriding Methods

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

Inheritance in Java also allows subclasses to override methods defined in their superclass. This means that a subclass can provide its own implementation of a method that is already defined in its superclass. In the 'Dog' example, the 'makeSound()' method is overridden to provide a specific implementation for dogs.

To override a method, the subclass must use the '@Override' annotation, which helps catch errors at compile time if the method being overridden doesn't exist in the superclass.

Benefits of Inheritance in Java

Java Final Keyword Cheat Sheet – Explained Simply
Java Final Keyword Cheat Sheet – Explained Simply

Inheritance offers several benefits in Java, including code reusability, establishing 'is-a' relationships, and achieving polymorphism. Let's explore each of these benefits in more detail.

Code Reusability

Static Keyword In Java : All Scenarios
Static Keyword In Java : All Scenarios
✅ Java Inheritance Example Explained Simply
✅ Java Inheritance Example Explained Simply
Types of Inheritance in Java: A Comprehensive Guide
Types of Inheritance in Java: A Comprehensive Guide
Mastering Inheritance in Java with the `super` Keyword
Mastering Inheritance in Java with the `super` Keyword
Types of Inheritance in Java | OOP Concepts Explained
Types of Inheritance in Java | OOP Concepts Explained
Java List of Keywords (All Reserved Words) | Complete Java Reference Guide
Java List of Keywords (All Reserved Words) | Complete Java Reference Guide
What is Inheritance in Java with example - Object Oriented Programming Tutorial
What is Inheritance in Java with example - Object Oriented Programming Tutorial
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
Inheritance in Java
Inheritance in Java
Keywords in Java - Java Reserved Words - TechVidvan
Keywords in Java - Java Reserved Words - TechVidvan
Java Inheritance - Learn its Importance and Types with Coding Examples.
Java Inheritance - Learn its Importance and Types with Coding Examples.
Type of Inheritance in Python
Type of Inheritance in Python
a white man standing in front of a red sign that says, final keyword
a white man standing in front of a red sign that says, final keyword
Java Programming
Java Programming
Constructor in Inheritance and Super keyword in JAVA || JAVA Tutorial in Hindi #26 || Death Code
Constructor in Inheritance and Super keyword in JAVA || JAVA Tutorial in Hindi #26 || Death Code
Java Keywords
Java Keywords
10 Examples of Inheritance in Java 🔗
10 Examples of Inheritance in Java 🔗
🔥 Java Super Keyword Explained! 🚀
🔥 Java Super Keyword Explained! 🚀
Hierarchical Inheritance in Java: How It Works and Why It Matters
Hierarchical Inheritance in Java: How It Works and Why It Matters
Exception Handling in Java | Try Catch Finally Explained with Example
Exception Handling in Java | Try Catch Finally Explained with Example

Inheritance promotes code reusability by allowing subclasses to inherit fields and methods from their superclass. This means that developers don't have to write the same code multiple times, reducing the likelihood of errors and increasing efficiency.

Moreover, if a superclass is modified, all its subclasses will automatically inherit those changes, making maintenance easier and more manageable.

Establishing 'Is-A' Relationships

Inheritance in Java helps establish 'is-a' relationships between classes. For example, a 'Dog' is an 'Animal', so the 'Dog' class extends the 'Animal' class. This relationship makes it clear that all dogs are animals, but not all animals are dogs.

This 'is-a' relationship is reflected in the inheritance hierarchy, making the code more intuitive and easier to understand.

Achieving Polymorphism

Inheritance is a key enabler of polymorphism in Java. Polymorphism allows objects of different classes to be treated as objects of a common superclass, enabling a single interface to represent multiple implementations.

For instance, we can create an array of 'Animal' objects that contains both 'Dog' and 'Cat' objects. When we call the 'makeSound()' method on each object, the appropriate implementation will be executed based on the actual type of the object.

Best Practices for Inheritance in Java

While inheritance is a powerful tool, it's essential to use it judiciously to avoid creating brittle and difficult-to-maintain code. Here are some best practices for using inheritance in Java.

Favor Composition Over Inheritance

In some cases, it's more appropriate to use composition instead of inheritance. Composition allows one class to have another class as a member, promoting better separation of concerns and more flexible design.

For example, instead of creating a 'FlyingAnimal' class that extends 'Animal', we could create a 'FlyingAnimal' class that has an 'Animal' member and implements the flying behavior using composition.

Keep Inheritance Hierarchies Shallow

Deep inheritance hierarchies can be difficult to understand, maintain, and test. It's generally a good idea to keep inheritance hierarchies shallow by limiting the number of levels in the hierarchy.

Instead of creating a deep hierarchy, consider using interfaces to achieve multiple inheritance and promote better separation of concerns.

Use Interfaces for Multiple Inheritance

Java does not support multiple inheritance of classes, but it does support multiple inheritance of types through interfaces. Interfaces allow a class to implement multiple 'is-a' relationships, providing more flexibility in designing class hierarchies.

For example, a 'FlyingAnimal' class could implement both the 'Animal' interface and the 'Flying' interface, allowing it to inherit properties and behaviors from both interfaces.

In the world of Java programming, inheritance plays a crucial role in creating maintainable, extensible, and reusable code. By understanding and leveraging the 'extends' keyword, developers can establish clear relationships between classes, promote code reuse, and achieve powerful polymorphism. As you continue your Java journey, keep these concepts in mind and strive to create elegant and efficient code.