extends Keyword in Java

feature-async-await.jpg

The extends keyword in Java plays a big role in how Java developers are able to write smarter and more usable code. Have you ever wondered how a class can “inherit” properties from another, or how developers avoid writing the same logic twice across different classes? This is the concept at work.

The extends keyword, at its core, enables inheritance. Inheritance is what allows one class to borrow features from another. Very much like how a child inherits traits from their parents. 

In this guide, we’ll break down the extends keyword with examples, real-world use cases, and tips you won’t find in just any tutorial. Let’s get started.

Table of Contents

What is the extends Keyword in Java?

In Java, the extends keyword is used to establish an inheritance relationship between two classes or interfaces. When one class extends another, it inherits the properties and behaviours of that class. This mechanism is what allows Java developers to create hierarchical structures in code where a child class can reuse or even possibly enhance the functionality of its parent class.

Syntax of the extends Keyword in Java

Even simple things in Java can have a profound effect; the extends keyword is a prime example of just this.

Java

That’s it. With just this one line, you’re telling Java that your SubclassName inherits all non-private methods and variables from SuperclassName.

Example of Java Extends: Real-Life Analogy

Example 1: 

Think of an online course platform, like Intellipaat. Let’s name the base class Course. If you think about it, every course will have some form of title, instructor name, duration, and many more functionalities that would be the same.

Now, imagine that you want to build a CodingCourse class. It’s still a course and so would share all of the previously mentioned functionalities along with maybe interactive compilers, code challenges, and downloadable project files. Instead of rewriting all the common course features, you’d just extend the base class:

Java

Output:

first-output
  • CodingCourse is a subclass of Course
  • It inherits the courseInfo() method automatically
  • And it adds a new method: runCode()

Example 2:

Let’s say you are designing a system for a fitness club. Every member of the club should be able to access the gym. However, premium members get extra privileges like a spa and sauna. You need to model this situation using classes, where the general Member class represents all members, and the PremiumMember class extends it to include the additional benefits.

Java

Output:

second-output
  • Member is the superclass (the general category)
  • PremiumMember is the subclass (a specialized type)
  • accessGym() was inherited from Member
  • accessSpa() is a new method added only to PremiumMember

Why use the extends Keyword in Java

  1. It encourages code reuse: Write once, use in many subclasses.
  2. It helps you build cleaner, modular code: Logical separation of shared and specific behavior.
  3. It enables polymorphism: You can treat a subclass object like its superclass.

Without extends, Java would be a lot more repetitive. This small keyword saves you from rewriting logic again and again, and makes your programs easier to manage and scale.

A Few Java extends Syntax Rules to Remember

  • A class can only extend one class (Java doesn’t support multiple class inheritance).
  • The extends keyword must appear right after the class name you’re declaring.
  • All public and protected members of the superclass are inherited by default.
  • Constructors are not inherited, but you can call a parent constructor using super().

Once you’ve mastered this syntax, you will be able to explore more powerful uses of inheritance, like method overriding, constructor chaining, and even polymorphism. Exciting, isn’t it?

How Inheritance Works in Java

Inheritance is one of the core concepts in object-oriented programming in Java. The extends keyword is how Java makes it happen.

In simple terms, inheritance allows one class (subclass) to receive the properties and behaviour of another class (superclass). 

Types of inheritance in Java

Java supports the following types of inheritance:

  • Single Inheritance: One class extends another
  • Multilevel Inheritance: A class extends a class that itself extends another
  • Hierarchical Inheritance: Multiple classes extend the same parent class

Using Java extends to Implement Inheritance

Using the extends not only saves you time and lines of code, it also enables you to build smarter, more organized applications. Let’s walk through a few examples of the extends keyword implementations in Java.

1. Creating a Superclass and Subclass

Here is a basic implementation where one class extends another.

Java

Output:

nine-output
  • User is the general class.
  • AdminUser is a specialized version of a user with extra powers.
  • It inherits everything from User and builds on top of it.

2. Method Overriding (Replacing Inherited Behavior)

Sometimes you would need to override an inherited method, i.e, replace it with your own version

Java

Output:

tenth-output

The @Override annotation isn’t mandatory, but it’s a good habit. It tells the compiler you’re intentionally overriding something, and warns you if you mess up the method signature.

3. Inheriting Fields and Methods

Inheritance is not only for methods; classes can inherit variables(fields) too.

Java

Output:

seventh-output

So, to sum up, with Java extends, you can:

  • Inherit fields and methods from a parent class
  • Add new behavior in your subclass
  • Override inherited methods to tailor their functionality

Constructor Behavior in Inheritance

When you use the extends keyword in Java, your subclass will inherit all non-private methods and fields from the superclass, but not the constructor. That’s right! Constructors are not inherited in Java. However, you can use the super() keyword to access them. Let’s quickly see how.

What Is Constructor Chaining?

When an object is created from a subclass in Java, it automatically calls the constructor of the parent class before the subclass’s constructor, even if you don’t specify it to. This is known as constructor chaining. It ensures that the base of the object is set up before the extended part.

Java

Output:

eight-output

Even though we only created an object of JavaCourse, the constructor of Course ran first. This is Java doing constructor chaining behind the scenes using an implicit call to super().

What if the Superclass Has Parameters?

If the superclass constructor requires parameters, the subclass must explicitly call it using super(arguments). Otherwise, you’ll get a compile-time error.

Java

Output:

fourth-image

Here are some key takeaways:

  • Java does not inherit constructors from a superclass.
  • The superclass constructor is always called first.
  • If the superclass has no default constructor, you must call one using super(…).
  • The call to super() must be the first line in the subclass constructor.

Working with Access Modifiers in Java extends

As you already know, when you use the extends keyword, the subclass gains access to the modifiers and variables in the superclass. You can also determine how much the child class can access by using access modifiers. Let’s dig a little deeper. 

The Four Access Levels in Java

In Java, the access modifiers offer 4 access levels that allow you to control access to modifiers and variables in a superclass. Understanding the difference between these levels can help you manage your code more efficiently, as not all subclasses need access to all the methods and variables in the superclass. 

Modifier Accessible in Same Class Accessible in Same Package Accessible in Subclass (Different Package) Accessible Everywhere
private Yes No No No
default (no modifier) Yes Yes No No
protected Yes Yes Yes No
public Yes Yes Yes Yes

Example: Access Modifiers in Java extends 

Now that you have an understanding of the different options that you have in access modifiers to manage access to a superclass, let’s look at each of them in action.

Java

Output:

sixth-output

As you can see, we have defined four variables in Book.

  • title is public and can be accessed from anywhere.
  • author is protected, but is still available to Ebook even though they’re different packages. 
  • publisher is default (package-private), which means it’s only visible within the library package
  • price is private, so it’s locked away inside Book and invisible to any other class.

Extending Interface in Java

So far, we have discussed how we can use the extends keyword for classes. In Java, it also applies to interfaces, but is implemented differently. 

How Java extends works with interfaces

  • An interface can extend one or more other interfaces.
  • Unlike classes, interfaces can inherit from multiple parents.
  • This allows you to build hierarchies of interfaces without the restrictions of single inheritance.

Let’s look at an example of extending an interface in Java.

Java

Output:

eleventh-image
  • Readable is a base interface that declares a read() method.
  • Writable extends Readable, meaning it inherits the read() method and adds its own write() method.
  • Document is a class that implements Writable.
  • Because Writable extends Readable, Document must provide concrete implementations for both read() and write().
  • When we create a Document object, we can call both methods, one inherited from Readable and one from Writable.

This setup is powerful because it allows you to chain interface contracts together without locking yourself into a rigid class hierarchy.

Limitations and Edge Cases of extends in Java

As you have figured out by now, the extends keyword in Java is straightforward in most scenarios. But, there are some limitations and edge cases that you should keep in mind while implementing the extends keyword in your own code. Let’s have a look at some of them.

  1. You Can’t Extend a Final Class
    A final class cannot be extended. You will get a compile error if you try to do so.
  2. Extending Abstract Classes
    Abstract classes are designed to be extended. If you do, you must implement all abstract methods.
Java
  1. Interface-to-Interface Inheritance
    An interface can extend one or more interfaces, allowing multiple inheritance.

Difference Between extends and implements in Java

At first glance, the implements keyword and the extends keyword might look similar. But make no mistake, they are quite different and serve very different purposes in Java’s object-oriented world. Let’s have a look at their key differences.

  • extends: used for class-to-class inheritance or interface-to-interface inheritance.
  • implements: used when a class adopts an interface.
Feature extends (Class) extends (Interface) implements
Used Between Class → Class Interface → Interface Class → Interface
Multiple Allowed? No (single class only) Yes Yes
What’s Inherited? Methods & fields Method declarations Must implement all methods
Access to Code? Yes (methods & variables) No (just contracts) No (just contracts)
Main Purpose Reuse existing implementation Build contract hierarchies Enforce the contract in class

Example: extends (Class-to-Class)

Java

Output:

fifth-output

Example: implements (Class-to-Interface)

Java

Output:

third-output

Best Practices When Using extends in Java

If you are trying to implement Java extends for the first time, it can seem confusing at first. Here are some best practices to keep in mind.

  1. Use Inheritance for True “Is-A” Relationships
    Ask yourself: “Is my subclass really a specialized version of the superclass?”
    If the answer is “not exactly,” inheritance might not be the right choice.
  1. Favor Composition Over Inheritance (When Appropriate)
    If you just want to reuse a method or two from another class, consider composition — using an instance of that class inside your own, rather than extending it. This avoids unnecessary coupling.
  1. Keep Class Hierarchies Shallow
    Deep inheritance chains can be hard to follow and maintain. Try not to go beyond two or three levels unless absolutely necessary.
  1. Avoid Overriding Without Good Reason
    Overriding methods just to tweak small details can lead to confusion. If you override, make sure the new behavior is clear and necessary.
  1. Document Inherited Behavior
    When your subclass relies heavily on inherited methods, leave comments or JavaDoc notes so future developers understand where the functionality is coming from.

If you keep these best practices and tips in mind when implementing the extends keyword, you should have no trouble getting your desired results.

Conclusion

The extends keyword, even though small, plays a huge role in building flexible and maintainable code. Enabling inheritance allows you to reuse existing logic, structure your programs logically, and reduce duplication.
We’ve looked at how extends works with classes and interfaces, its rules and limitations, and the best practices to follow. You’ve also seen how it interacts with access modifiers, constructors, abstract classes, and even interface hierarchies. If you are looking to dig deeper into advanced Java concepts, check out our Web development courses.

extends Keyword in Java – FAQs

1. What does the extends keyword do in Java?

The extends keyword in Java is used to create an inheritance relationship between two classes(or interfaces). The child class automatically gains the non-private fields and methods of the superclass.

2. Can a class extend more than one class?

No. Java supports single inheritance for classes; a class can only extend one other class.
For multiple inheritance of behavior, use interfaces.

3. How does extends work with abstract classes?

When you extend an abstract class, you must implement all of its abstract methods unless your subclass is also declared abstract.

4. What is the difference between implements and extends?
  • extends: For class-to-class inheritance or interface-to-interface inheritance (reuses existing structure/behavior).
  • implements: For a class to fulfill the contract of an interface (promises to provide behavior).
5. Can a subclass call a superclass constructor?

Yes. Use super() inside the subclass constructor to call the superclass constructor.
This must be the first statement in the constructor.

6. What if a subclass doesn’t override a method?

The subclass simply inherits the method as is. It can still call it directly, but the behavior will be whatever the superclass defined.

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.

Full Stack Developer Course Banner