Search Authority

What is "extends" in Java? A Concise Guide to Inheritance

Extends in Java is a core mechanism that lets one class build on another, promoting code reuse and clearer type modeling. The keyword defines a subclass that inherits fields and...

Mara Ellison
What is "extends" in Java? A Concise Guide to Inheritance

Extends in Java is a core mechanism that lets one class build on another, promoting code reuse and clearer type modeling. The keyword defines a subclass that inherits fields and methods from a parent class while allowing new behavior and stricter contracts through annotations.

Design teams rely on extends to structure layered architectures, where base classes capture shared logic and derived classes specialize behavior. Understanding how extends interacts with access rules, method signatures, and object construction is essential for robust applications.

Keyword Target Inheritance Type Accessibility Impact
extends Single class Class inheritance Subclass sees protected and public members
implements Interface contract Type adoption Requires public method declarations
super Parent reference Delegation within subclass Accesses inherited but possibly hidden members
final Class or method Restricts extension Prevents further overriding or subclassing

Extends Class vs Interface Implementation

Java allows a class to extend exactly one superclass, capturing shared state and behavior in a single lineage. In contrast, a class can implement multiple interfaces, which enables polymorphic contracts without forcing implementation inheritance for every capability.

Choosing extends for core domain modeling keeps hierarchies focused, while interfaces define orthogonal skills. This combination reduces duplication and makes future refactoring more predictable when roles evolve independently of the parent structure.

Method Overriding and Dynamic Dispatch

Signature Rules and Substitution

When a subclass overrides a method, the name, parameter types, and return type must match or be a subtype, ensuring callers can rely on consistent behavior through a parent reference. The runtime type of the object, not the reference type, determines which method body executes, enabling polymorphic patterns central to extensible systems.

Restrictions on Overriding

A subclass cannot narrow the visibility of an inherited public or protected method, and checked exceptions must be compatible or narrower. Annotations like @Override help detect mismatches early, preventing accidental overloads instead of overrides that would break expected contracts.

Constructor Chaining and Initialization Order

Each subclass constructor implicitly or explicitly invokes a superclass constructor, establishing a chain that reaches the root Object before instance fields are set. This guarantees that shared invariants and initialization logic defined in ancestors are respected before subclass-specific setup runs.

Explicit calls to super must appear as the first statement, preventing access to not-yet-initialized members. Designing constructors to minimize side effects and favor immutable fields leads to safer inheritance graphs that are easier to test and reason about.

Design Guidelines and Composition Trade-offs

Prefer extending abstract, well-documented base classes that model an "is-a" relationship, because deep hierarchies can become fragile when changes ripple through descendants. Favor composition over inheritance when behavior varies independently or when multiple classification dimensions exist, reducing coupling and increasing flexibility.

  • Use extends to express clear subtype hierarchies with shared invariants.
  • Prefer interfaces for cross-cutting roles to avoid deep, rigid trees.
  • Document protected members and template methods that subclasses are expected to override.
  • Apply final to classes or methods when further specialization would violate design intent.
  • Leverage @Override consistently to clarify intent and enable compiler checks.

FAQ

Reader questions

Can a class extend another class and implement interfaces at the same time?

Yes, a class can extend a single parent while implementing one or more interfaces, combining inherited behavior with additional contracts that are enforced by the compiler.

What happens if a subclass declares a method with the same name but different parameters than its parent?

This defines method overloading, not overriding; the parent method remains hidden for the subclass type when called through subclass references, which can reduce clarity and is generally discouraged for polymorphic APIs.

Does extending a final class cause a compilation error?

Yes, the compiler rejects any attempt to extend a final class because final is designed to prevent inheritance and enforce a strict, unchanging implementation.

How does extends interact with package-private members in inherited code?

Subclasses in different packages lose access to package-private members, which can surface as compilation errors when relying on friendly access that was valid within the original package scope.

Related Reading

More pages in this topic cluster.

Who Designed the Nike Logo? The Story Behind the Swoosh

The Nike swoosh is one of the most recognizable symbols in the world, but few people know the story behind its creation. This piece explores who designed the Nike logo, why it h...

Read next
What is the World's Hottest Pepper? 🌶️🔥

When people ask about the world's hottest pepper, they usually mean the variety that currently holds the Guinness World Record and pushes the boundaries of capsaicin heat. Peppe...

Read next
Jon Huertas in This Is Us:角色, 出演时期与剧情影响详解

Jon Huertas 在《这就是我们》中饰演成年 Kevin Pearson,这一角色从2016年首播持续至2022年最终季,构成了剧集核心家庭叙事的重要组成部�...

Read next