Inheritance in Java: Understanding the 'extends' Keyword

Benjamin Jul 11, 2026

In the realm of object-oriented programming, inheritance is a powerful feature that allows classes to derive properties and behaviors from other classes. In Java, this is achieved using a specific keyword. Let's delve into the world of Java inheritance and explore the keyword that enables this crucial aspect of object-oriented design.

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

Java, being a statically-typed language, requires explicit declaration of the classes from which a new class can inherit. This is where our keyword comes into play. But before we dive into the keyword, let's understand why inheritance is important in Java.

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

Understanding Inheritance in Java

Inheritance in Java promotes code reusability and establishes 'is-a' relationships between classes. It allows new classes to inherit the properties and methods of existing classes, reducing redundancy and enhancing code maintainability.

The new keyword in Java programming language - Codeforcoding
The new keyword in Java programming language - Codeforcoding

Inheritance also enables polymorphism, a fundamental principle of object-oriented programming that allows objects of different classes to be treated as objects of a common superclass. This leads to more flexible and extensible code.

Inheritance Syntax in Java

What is Inheritance in Java with example - Object Oriented Programming Tutorial
What is Inheritance in Java with example - Object Oriented Programming Tutorial

Now, let's discuss the keyword that enables inheritance in Java. The keyword is 'extends'. It's used to create a new class that inherits from an existing class, known as the superclass or parent class.

Here's a simple example illustrating the use of the 'extends' keyword:

```java class Animal { void eat() { System.out.println("eating"); } } class Dog extends Animal { void bark() { System.out.println("barking"); } } ```

Inheritance Hierarchy and Access Modifiers

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

'extends' not only allows a class to inherit from another but also establishes an inheritance hierarchy. A class can extend only one superclass, following the single inheritance principle in Java. However, it can implement multiple interfaces, providing a form of multiple inheritance.

The access modifiers in Java (public, private, protected) also play a crucial role in inheritance. They determine the accessibility of the inherited properties and methods, ensuring encapsulation and data hiding.

Key Features of Inheritance in Java

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

Inheritance in Java comes with several features that make it a potent tool for object-oriented design. Let's explore some of these features.

Method Overriding

Java Programming
Java Programming
http://chatgpt.com/g/g-lvtjtj09i-darkhorse-dns
http://chatgpt.com/g/g-lvtjtj09i-darkhorse-dns
Java Class Inheritance
Java Class Inheritance
the key words in this language are used to describe something
the key words in this language are used to describe something
Java List of Keywords (All Reserved Words) | Complete Java Reference Guide
Java List of Keywords (All Reserved Words) | Complete Java Reference Guide
Class and Object in Java Explained | OOP Concepts for Beginners
Class and Object in Java Explained | OOP Concepts for Beginners
✅ Java Inheritance Example Explained Simply
✅ Java Inheritance Example Explained Simply
10 Examples of Inheritance in Java 🔗
10 Examples of Inheritance in Java 🔗
30 Days of Java – Day 14 | Java Inheritance Explained for Beginners
30 Days of Java – Day 14 | Java Inheritance Explained for Beginners
Static Keyword In Java : All Scenarios
Static Keyword In Java : All Scenarios
Java OOP Concepts Explained: Encapsulation, Inheritance, Polymorphism & Abstraction
Java OOP Concepts Explained: Encapsulation, Inheritance, Polymorphism & Abstraction
30 Days of Java – Day 12 | Java Classes and Objects Explained for Beginners
30 Days of Java – Day 12 | Java Classes and Objects Explained for Beginners
Data Types in Java
Data Types in Java
Java Scanner Class Made Easy: Your Ultimate Input Cheat Sheet 🎯💻
Java Scanner Class Made Easy: Your Ultimate Input Cheat Sheet 🎯💻
Java Inheritance - Learn its Importance and Types with Coding Examples.
Java Inheritance - Learn its Importance and Types with Coding Examples.
Abstract Class Vs Interface in Java
Abstract Class Vs Interface in Java
☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
🔥 Java Super Keyword Explained! 🚀
🔥 Java Super Keyword Explained! 🚀
java for complete beginners - user input
java for complete beginners - user input
Java Keywords
Java Keywords

Method overriding allows a subclass to provide a different implementation of a method that is already defined in its superclass. This is achieved using the '@Override' annotation in Java.

Here's an example of method overriding:

```java class Animal { void makeSound() { System.out.println("The animal makes a sound"); } } class Dog extends Animal { @Override void makeSound() { System.out.println("The dog barks"); } } ```

Constructors and Inheritance

In Java, constructors are not inherited. However, a subclass can invoke the superclass constructor using the 'super' keyword. This ensures that the superclass is initialized before the subclass.

Here's an example illustrating the use of the 'super' keyword:

```java class Animal { String name; Animal(String name) { this.name = name; } } class Dog extends Animal { Dog(String name) { super(name); } } ```

In conclusion, the 'extends' keyword in Java is pivotal for achieving inheritance, a cornerstone of object-oriented programming. It enables code reusability, establishes 'is-a' relationships, and promotes polymorphism. Understanding and effectively using 'extends' can significantly enhance your Java programming skills and lead to more efficient, maintainable code.

Now that you've grasped the concept of inheritance in Java, why not explore it further by trying to implement your own inheritance hierarchy? Start with a simple 'Animal' superclass and create subclasses like 'Dog', 'Cat', and 'Bird', each with its unique behaviors and properties. Happy coding!