CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Java > Extends Keyword in Java: A Deep Dive into Inheritance

Java Tutorial

  • Introduction
    • What is Java
    • History Of Java
    • Install Java
    • What is JVM
    • JDK vs JRE vs JVM
    • Java Bytecode
    • OOP vs POP
    • Compile and Run Java
  • Tokens, Expressions and Control Structures
    • Primitive data types
    • Integers
    • Floating Points
    • Characters
    • Booleans
    • User Defined Data Type
    • Declarations
    • Constants
    • Identifiers
    • Literals
    • Type Conversion and Casting
    • Variables
    • Default Variable Initialization
    • Command Line Arguments
    • Arrays of Primitive Types
    • Comment Syntax
    • Garbage Collection
    • Expressions
    • Operators
    • Arithmetic Operator
    • Bitwise and Shift Operator
    • Comparison or Relational Operators
    • Logical Operators
    • Assignment Operators
    • Ternary Operator
    • Increment and Decrement Operator
    • Control Statements
  • OOP Concepts
    • Class and Object
    • Create Class Instance
    • Method
    • Abstraction
    • Encapsulation
    • this keyword
    • Constructor
    • Pass by Value
    • Access Modifier/Control
    • Polymorphism
    • Method Overloading vs Method Overriding
    • Recursion
    • Nested and Inner Class
  • Inheritance and Packaging
    • Inheritance
    • extends Keyword
    • super Keyword
    • Object Class
    • Abstract class
    • Final Class
    • Java Package
    • Interface
  • Handling Error/ Exceptions
    • What is Exception
    • Exception Handling Keywords
    • Common Java Errors
    • User Defined Exception
    • Throwing and re-throwing Exception
    • finally Block
  • Strings
    • Java String Tutorial
  • Threads
    • Introduction
    • Create Thread
    • Thread Lifecycle
    • Thread Priority
    • Thread Synchronization
    • Inner Thread Communication
    • Thread Deadlock
  • IO and Streams
    • java.io Package
    • Files and Directories
    • Byte Stream
    • Character Stream
    • Console Input and Output
    • Serializable and Deserializable
  • Core Packages
    • java.lang Package
    • Math
    • Wrapper Classes
    • java.lang.Number
    • Double
    • Float
    • Integers
    • java.lang.Byte
    • java.lang.Short
    • java.lang.Long
    • java.lang.Character
    • java.lang.Boolean
    • java.util package
    • Vector Class
    • Stack Class
    • Dictionary Class
    • Hashtable
    • Enumeration or Enum
    • Generate Random Number
  • Holding Collection of Data
    • Arrays
    • Map
    • List
    • Set
    • Collection Interface
    • Collections Class
    • ArrayList
    • HashSet
    • TreeSet
    • Comparator
  • Java Bean
    • What is Java Bean
    • Advantages and Disadvantages of Java Bean
    • Java Beans API
    • Introspection
    • Java Bean Properties
    • Bound and Constrained Properties
    • BeanInfo Interface
    • Customizers
    • Java Beans Persistence
    • BeanDescriptor
  • Home

Extends Keyword in Java: A Deep Dive into Inheritance

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 · 3 min read · 0 Comments
Share: in X
extends keyword in java

The extends keyword in Java is used to establish a class inheritance relationship. Extends keyword in Java allows a class (known as the subclass or derived class) to inherit the properties and behaviors (fields and methods) of another class (known as the superclass or base class).

Table of Contents

  • What Does the extends Keyword Do?
    • Key Features:
  • Syntax of the extends Keyword
  • Extends keyword example
  • Common Errors with extends (and Fixes)
    • 1. Extending Multiple Classes
    • 2. Missing Superclass Constructor
  • extends vs. implements
  • Conclusion
  • FAQs
    • Can a class extend itself?
    • Can final classes be extended?
    • Can a subclass override static methods?

What Does the extends Keyword Do?

The extends keyword creates an inheritance relationship between two classes:

class Superclass { }  
class Subclass extends Superclass { } // Inherits Superclass  

Key Features:

  • Enables code reuse (subclasses inherit fields/methods).
  • Supports method overriding (subclass redefines superclass methods).
  • Establishes an “is-a” relationship (e.g., Dog is a Animal).

Syntax of the extends Keyword

The syntax for using the extends keyword is as follows:

class SubclassName extends SuperclassName {
    // Class members and methods of SubclassName
}

Following is a breakdown of what this means:

  1. SubclassName: This is the name of the subclass that you want to create.
  2. SuperclassName: This is the name of the superclass from which you want the subclass to inherit.

By using the extends keyword, the subclass SubclassName inherits all the non-private members (fields and methods) from the superclass SuperclassName. This means that the subclass can access and use those inherited members as if they were defined within the subclass itself.

Extends keyword example

Following is a simple example to illustrate the use of extends:

class Animal {
    String name;
    
    void makeSound() {
        System.out.println("Some generic animal sound");
    }
}

class Dog extends Animal {
    // Dog class inherits the 'name' and 'makeSound()' method from Animal class
    
    void makeSound() {
        System.out.println("Woof!");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.name = "Buddy";
        dog.makeSound(); // Output: Woof!
    }
}

In this example, the Dog class extends the Animal class. It inherits the name field and the makeSound() method from the Animal class. The makeSound() method is overridden in the Dog class, which means it provides its own implementation of the method.

Common Errors with extends (and Fixes)

1. Extending Multiple Classes

Error:

class A extends B, C { } // Not allowed!  

Fix: Use interfaces for multiple inheritance:

class A implements B, C { }  

2. Missing Superclass Constructor

Error:

class Parent { Parent(int x) { } }  
class Child extends Parent { } // Error: No default Parent()  

Fix: Define a matching constructor in Child:

class Child extends Parent {  
    Child(int x) { super(x); }  
}  

extends vs. implements

extendsimplements
Used for class inheritance.Used for interface implementation.
Supports single inheritance.Supports multiple inheritance.
Inherits fields and methods.Inherits method signatures only.

Conclusion

The extends keyword in Java is your gateway to building reusable, hierarchical code. By mastering inheritance, method overriding, and proper use of super, you’ll design robust applications that leverage OOP principles effectively.

FAQs

Can a class extend itself?

No. A class cannot extend itself (compiler error).

Can final classes be extended?

No. final classes block inheritance (e.g., String).

Can a subclass override static methods?

No. Static methods are hidden, not overridden.

Related Posts:

  • Inheritance in Java: A Developer’s Guide to Code Reusability
  • Final Class in Java: Enforcing Immutability and Security
  • Abstract Class in Java: Bridging Code Reusability…
  • Interface in Java: Mastering Abstraction and…
  • final Keyword in Java
  • Most Frequently Asked Spring Boot Interview…
Tags:javalanguage-fundamentals
Was this article helpful?
← Previous ArticleWhy Multiple Inheritance is not Supported in Java?
Next Article →Exception Handling Keywords in Java

Recent Posts

  • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
© 2026 CoderSathi. All rights reserved. Privacy Policy · Sitemap