Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

What Is Inheritance in Java and How Does extends Work?

Java
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedNov 29, 2026
5 min read

Inheritance in Java lets one class reuse and extend the fields and methods of another. You declare the relationship with the extends keyword: class Dog extends Animal makes Dog a subclass of the superclass Animal, so a Dog automatically has everything accessible in Animal and can add or change behaviour.

This guide covers extends, constructor chaining with super, method overriding, the Object root class, and when to choose composition instead. Examples work on Java 21 and Java 25 LTS.

Table of Contents

  • The extends keyword and the is-a relationship
  • Constructors and the super keyword
  • Overriding methods
  • Types of inheritance and the Object class
  • When to prefer composition
  • Frequently Asked Questions
    • Does Java support multiple inheritance?
    • Are constructors inherited?
    • What is the difference between overloading and overriding?
    • How do I stop a class from being extended?
  • Summary
  • Related Articles

The extends keyword and the is-a relationship

Inheritance models an is-a relationship: a dog is an animal, a car is a vehicle. The subclass inherits non-private members of the superclass, and Java allows a class to extend exactly one class.

java
java
java
text

Dog did not write eat(), but it has it. Private members exist in the object but are not directly accessible from the subclass. Constructors are never inherited.

Constructors and the super keyword

When you create a Dog, the Animal part must be built first. Every constructor starts by calling a superclass constructor, either explicitly with super(...) or implicitly with super() if you write nothing.

java

If the superclass has no no-argument constructor and you do not call super(args), the code does not compile. Since Java 22 you may run some statements before super(...) in preview or newer language levels, but the plain rule above is what you should learn first.

The super keyword also reaches a parent method that you have overridden: super.eat().

Overriding methods

A subclass can replace the behaviour of an inherited method by declaring a method with the same name and parameters. Add @Override so the compiler checks your intent.

java
text

The overriding method must have the same signature, a compatible return type and access that is not narrower than the original. You cannot override private, static or final methods. If you mark a class final, no one can extend it, as with String.

Types of inheritance and the Object class

Java supports single inheritance (one parent), multilevel inheritance (A to B to C) and hierarchical inheritance (many children of one parent). A class cannot extend two classes, which avoids the ambiguity known as the diamond problem. To share behaviour from several sources, a class implements several interfaces.

Every class without an explicit extends clause inherits from java.lang.Object. That is why every object has toString(), equals() and hashCode(). A reference of the parent type can hold a child object:

java

Sealed classes (final since Java 17) let you restrict which classes may extend a parent with sealed and permits, useful when you want a closed hierarchy.

When to prefer composition

Inheritance couples the child tightly to the parent. A change in the superclass can break every subclass. Use it only when the is-a relationship is true and stable. If the relationship is has-a, hold the other object in a field instead.

QuestionUse inheritanceUse composition
RelationshipDog is an AnimalCar has an Engine
CouplingTightLoose
Swap behaviour at runtimeHardEasy

Extending a class just to reuse a couple of methods is a common beginner mistake. A Stack that extends ArrayList exposes methods like add(index, x) that break the idea of a stack.

Frequently Asked Questions

Does Java support multiple inheritance?

Not for classes. A class can extend only one class, but it can implement many interfaces, which gives most of the benefits without the diamond problem.

Are constructors inherited?

No. A subclass must declare its own constructors, and each one calls a superclass constructor with super(...), explicitly or implicitly.

What is the difference between overloading and overriding?

Overloading means several methods with the same name and different parameters in one class. Overriding means a subclass redefines a method with the same signature as its parent.

How do I stop a class from being extended?

Declare it final. In Java 17 and later you can also use sealed to allow only a named list of subclasses.

Summary

Inheritance with extends lets a subclass reuse and specialise a superclass, calling the parent constructor with super and replacing behaviour through overriding. Java allows single class inheritance, with Object at the top of every hierarchy. Use it for true is-a relationships and choose composition for has-a ones.

Related Articles

  • What Is Encapsulation in Java and How Do Getters and Setters Help?
  • What is the purpose of the super keyword in Java?
  • Learn Java: Complete Study Roadmap from Beginner to Advanced
Share

Similar posts

  • What Is Composition vs Inheritance in Java and Which Should You Prefer?
  • What Is the equals and hashCode Contract in Java?
  • What Is the Object Class in Java? equals, hashCode and toString
  • What Is Method Overriding in Java and How Is It Different from Overloading?
  • What Is Polymorphism in Java? Compile-Time vs Runtime
  • What Is Encapsulation in Java and How Do Getters and Setters Help?
  • What Are Access Modifiers in Java: public, private, protected, default?
  • What Is the static Keyword in Java? Variables, Methods, Blocks