Inheriting Java Classes: Mastering Keyword

Benjamin Jul 11, 2026

In the realm of object-oriented programming, inheritance is a powerful mechanism that allows classes to acquire properties and methods 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 keyword used to inherit a class.

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 in Java enables a new class to acquire the properties (attributes) and methods of an existing class. It represents the 'is-a' relationship, meaning a subclass is a type of superclass.

Inheritance in Java
Inheritance in Java

Understanding the 'extends' Keyword

The 'extends' keyword in Java is used to create a subclass from a superclass. It allows the subclass to inherit the properties and methods of the superclass. Here's a basic syntax:

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

class SubClassName extends SuperClassName { ... }

Inheriting Properties and Methods

Java Programming
Java Programming

When a subclass inherits from a superclass, it automatically gains access to all the properties (variables) and methods of the superclass, unless they are marked as private or final. Here's an example:

class Animal {
    String name;
    void eat() {
        System.out.println("Eating");
    }
}

class Dog extends Animal {
    void bark() {
        System.out.println("Barking");
    }
}

In this example, the 'Dog' class inherits the 'name' variable and the 'eat' method from the 'Animal' class.

Constructors and Inheritance

Programming Books Archives - Techprofree
Programming Books Archives - Techprofree

When a subclass is instantiated, the superclass constructor is called first, followed by the subclass constructor. This is known as constructor chaining. To call the superclass constructor explicitly, use the 'super' keyword. Here's an example:

class Animal {
    Animal() {
        System.out.println("Animal created");
    }
}

class Dog extends Animal {
    Dog() {
        super(); // Calls the superclass constructor
        System.out.println("Dog created");
    }
}

Overriding Methods

Inheritance also allows for method overriding, where a subclass provides a different implementation of a method that already exists in the superclass. The '@Override' annotation is used to ensure that the method is indeed overriding a superclass method. Here's an example:

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

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");
    }
}

Method Hiding vs Method Overriding

It's important to note the difference between method hiding and method overriding. Method hiding occurs when a subclass has a method with the same name and parameter list as a method in the superclass, but the superclass method is not overridden. Instead, the subclass method hides the superclass method. Method overriding, on the other hand, provides a new implementation of an existing method in the superclass.

the key words in this language are used to describe something
the key words in this language are used to describe something
The new keyword in Java programming language - Codeforcoding
The new keyword in Java programming language - Codeforcoding
Inheritance in Java
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
Types of Inheritance in Java: A Comprehensive Guide
Types of Inheritance in Java: A Comprehensive Guide
Keywords in JAVA
Keywords in JAVA
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
Inheritance in Java Explained | OOP Concepts for Beginners
Inheritance in Java Explained | OOP Concepts for Beginners
java keywords  you must know
java keywords you must know
Keywords in Java - Java Reserved Words - TechVidvan
Keywords in Java - Java Reserved Words - TechVidvan
✅ Java Inheritance Example Explained Simply
✅ Java Inheritance Example Explained Simply
Super Keyword In Java With Examples
Super Keyword In Java With Examples
Super Keyword in Java: Syntax, Uses and Examples - Scientech Easy
Super Keyword in Java: Syntax, Uses and Examples - Scientech Easy
Java Class Inheritance
Java Class Inheritance
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 🔗
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
What is Inheritance in Java with example - Object Oriented Programming Tutorial
What is Inheritance in Java with example - Object Oriented Programming Tutorial
Interface in Java programming language - Codeforcoding
Interface in Java programming language - Codeforcoding
Java Keywords
Java Keywords

In conclusion, the 'extends' keyword in Java is a powerful tool for creating inheritance hierarchies. It allows classes to inherit properties and methods from other classes, promoting code reuse and creating a clear, hierarchical structure. Understanding how to use 'extends' and the related concepts of constructor chaining, method overriding, and method hiding is crucial for effective object-oriented programming in Java.