Syntax of extends Keyword in Java
To create a subclass using the extends keyword, we use the following syntax:
class SubclassName extends SuperclassName {
// fields and methods of the subclass
}
Here, "SubclassName" is the name of the new class we are creating, and "SuperclassName" is the name of the class that we are extending.
For example, let's create a subclass called "Dog" that extends the "Animal" class:
class Animal {
String name;
int age;
void speak() {
System.out.println("The animal speaks.");
}
}
class Dog extends Animal {
void bark() {
System.out.println("The dog barks.");
}
}
In this example, the Dog class extends the Animal class using the extends keyword. The Dog class inherits the name and age fields, as well as the speak() method from the Animal class. It also adds its own bark() method.
Example
Let's look at a complete example of using the extends keyword in Java:
Java
class Animal {
String name;
int age;
void speak() {
System.out.println("The animal speaks.");
}
}
class Dog extends Animal {
void bark() {
System.out.println("The dog barks.");
}
}
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.name = "Buddy";
myDog.age = 3;
System.out.println("Name: " + myDog.name);
System.out.println("Age: " + myDog.age);
myDog.speak();
myDog.bark();
}
}

You can also try this code with Online Java Compiler
Run Code
Output:
Name: Buddy
Age: 3
The animal speaks.
The dog barks.
In this example, we have an Animal class with name & age fields and a speak() method. We also have a Dog class that extends the Animal class & adds a bark() method.
In the Main class, we create an object of the Dog class called myDog. We set the name & age of myDog using the fields inherited from the Animal class. We then print the name & age of myDog.
Finally, we call the speak() method inherited from the Animal class & the bark() method defined in the Dog class.
Extending Final Class
In Java, a final class is a class that cannot be subclassed or inherited by another class. If we try to extend a final class, we will get a compilation error.
For example, let's say we have a final class called "FinalClass":
final class FinalClass {
void method() {
System.out.println("This is a final class.");
}
}
If we try to create a subclass that extends the FinalClass, like this:
class SubClass extends FinalClass {
// this will cause a compilation error
}
We will get the following compilation error:
error: cannot inherit from final FinalClass
This is because a final class is meant to be a complete & unchangeable unit, and allowing it to be subclassed would go against this principle.
Final classes are often used for classes that contain sensitive or critical code that should not be modified or overridden by subclasses. They are also used for classes that are not designed to be inherited, such as utility classes with static methods.
Extending Interfaces
In Java, a class can also extend an interface using the extends keyword. When a class extends an interface, it inherits the abstract methods of the interface & must provide an implementation for each of them.
For example :
Java
interface Drawable {
void draw();
}
class Circle implements Drawable {
public void draw() {
System.out.println("Drawing a circle.");
}
}
class Rectangle implements Drawable {
public void draw() {
System.out.println("Drawing a rectangle.");
}
}
public class Main {
public static void main(String[] args) {
Circle circle = new Circle();
Rectangle rectangle = new Rectangle();
circle.draw();
rectangle.draw();
}
}

You can also try this code with Online Java Compiler
Run Code
Output
Drawing a circle.
Drawing a rectangle.
In this example, we have an interface called Drawable with a single abstract method draw(). We then have two classes, Circle & Rectangle, that implement the Drawable interface using the implements keyword.
Each class provides its own implementation of the draw() method, which is called polymorphically through the Drawable interface reference in the Main class.
A class can extend multiple interfaces, separated by commas, like this:
class MyClass implements Interface1, Interface2, Interface3 {
// implementation of abstract methods from all three interfaces
}
Frequently Asked Questions
Can a class extend multiple classes in Java?
No, a class in Java can only extend one class at a time. This is because Java does not support multiple inheritance of classes. However, a class can implement multiple interfaces.
What happens if a subclass defines a method with the same name as a method in the superclass?
If a subclass defines a method with the same name & signature as a method in the superclass, it overrides the superclass method. The subclass method will be called instead of the superclass method when invoked on an object of the subclass.
Can we extend a class that is declared as private?
No, we cannot extend a class that is declared as private. A private class is only accessible within the same class file & cannot be subclassed or instantiated outside of that file.
Conclusion
In this article, we learned about the extends keyword in Java & how it is used to create subclasses that inherit properties & methods from a superclass. We saw the syntax for using the extends keyword & looked at examples of extending classes & interfaces. We also discussed the limitations of extending final classes & the ability to override methods in subclasses. The extends keyword is a powerful feature of Java that allows for code reuse & the creation of hierarchical relationships between classes.
You can also practice coding questions commonly asked in interviews on Coding Ninjas Code360.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.