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?
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 aAnimal
).
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:
SubclassName
: This is the name of the subclass that you want to create.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
extends | implements |
---|---|
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.