Inherit Java Class: Mastering Keyword

Benjamin Jul 11, 2026

Inheritance is a fundamental concept in object-oriented programming, allowing classes to acquire properties and behaviors from other classes. In Java, this is achieved using the 'extends' keyword. Let's delve into the world of inheritance in Java, focusing on the 'extends' keyword and its role in class inheritance.

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

Before we dive into the 'extends' keyword, let's briefly understand what inheritance is. Inheritance enables a new class to acquire the properties and methods of an existing class. The class whose properties or methods are inherited is known as the superclass, and the class that inherits those properties or methods is known as the subclass.

Java Programming
Java Programming

Understanding the 'extends' Keyword

The 'extends' keyword in Java is used to indicate that a class is a subclass of another class. It allows the subclass to inherit the fields and methods of the superclass, and can also override or extend the behavior of the superclass methods.

Inheritance in Java
Inheritance in Java

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

```java public class Animal { public void eat() { System.out.println("Eating"); } } public class Dog extends Animal { public void bark() { System.out.println("Barking"); } } ```

Inheriting Fields and Methods

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

In the above example, the 'Dog' class is a subclass of the 'Animal' class. This means that the 'Dog' class inherits the 'eat' method from the 'Animal' class. So, an instance of the 'Dog' class can call the 'eat' method.

Here's how you can create an instance of the 'Dog' class and call its inherited method:

```java Dog myDog = new Dog(); myDog.eat(); // Outputs: Eating ```

Overriding Methods

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

Subclasses can override the behavior of inherited methods using the '@Override' annotation. This allows the subclass to provide its own implementation of the method while keeping the same method signature.

Here's an example of overriding the 'eat' method in the 'Dog' class:

```java public class Dog extends Animal { @Override public void eat() { System.out.println("Dog is eating"); } public void bark() { System.out.println("Barking"); } } ```

Constructors and Inheritance

Java List of Keywords (All Reserved Words) | Complete Java Reference Guide
Java List of Keywords (All Reserved Words) | Complete Java Reference Guide

When a subclass is instantiated, the superclass constructor is called first, followed by the subclass constructor. This is known as constructor chaining.

Here's an example illustrating constructor chaining:

Java Final Keyword Cheat Sheet – Explained Simply
Java Final Keyword Cheat Sheet – Explained Simply
The new keyword in Java programming language - Codeforcoding
The new keyword in Java programming language - Codeforcoding
Keywords in JAVA
Keywords in JAVA
java keywords  you must know
java keywords you must know
What is Inheritance in Java – 5 Types, Use and Advantages
What is Inheritance in Java – 5 Types, Use and Advantages
10 Examples of Inheritance in Java 🔗
10 Examples of Inheritance in Java 🔗
the key words in this language are used to describe something
the key words in this language are used to describe something
Inheritance in Java
Inheritance in Java
Java Class Inheritance
Java Class Inheritance
Types of Inheritance in Java: A Comprehensive Guide
Types of Inheritance in Java: A Comprehensive Guide
What is the Difference Between Abstract Class and Interface in Java - Pediaa.Com
What is the Difference Between Abstract Class and Interface in Java - Pediaa.Com
🔥 Java Super Keyword Explained! 🚀
🔥 Java Super Keyword Explained! 🚀
✅ Java Inheritance Example Explained Simply
✅ Java Inheritance Example Explained Simply
Super Keyword in Java Explained Simply | Java OOP Concepts | BCA Notes
Super Keyword in Java Explained Simply | Java OOP Concepts | BCA Notes
Keywords in Java - Java Reserved Words - TechVidvan
Keywords in Java - Java Reserved Words - TechVidvan
Java Programming Tutorial 27- Inheritance, super keyword, Superclass Reference (IDE) (in English)
Java Programming Tutorial 27- Inheritance, super keyword, Superclass Reference (IDE) (in English)
Types of Inheritance in Programming
Types of Inheritance in Programming
What is Inheritance in Java with example - Object Oriented Programming Tutorial
What is Inheritance in Java with example - Object Oriented Programming Tutorial
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

```java public class Animal { public Animal() { System.out.println("Animal created"); } } public class Dog extends Animal { public Dog() { System.out.println("Dog created"); } } ```

Calling Superclass Constructors

Subclasses can explicitly call the superclass constructor using the 'super' keyword. This can be useful when you want to perform some initialization in the superclass constructor.

Here's an example of calling the superclass constructor from the subclass:

```java public class Animal { public Animal(String name) { System.out.println("Animal " + name + " created"); } } public class Dog extends Animal { public Dog(String name) { super(name); System.out.println("Dog " + name + " created"); } } ```

In conclusion, the 'extends' keyword in Java is a powerful tool for achieving code reuse and establishing 'is-a' relationships between classes. It enables subclasses to inherit fields and methods from superclasses, override their behavior, and call superclass constructors. Understanding and mastering the 'extends' keyword is crucial for any Java developer looking to write maintainable and extensible code.