Table of contents
1.
Introduction
2.
What is Java extends keyword?
3.
Syntax of extends Keyword in Java
4.
Example
4.1.
Java
5.
Extending Final Class
6.
Extending Interfaces
6.1.
Java
7.
Frequently Asked Questions
7.1.
Can a class extend multiple classes in Java?
7.2.
What happens if a subclass defines a method with the same name as a method in the superclass?
7.3.
Can we extend a class that is declared as private?
8.
Conclusion
Last Updated: Aug 4, 2024
Easy

Extends Keyword in Java

Author Sinki Kumari
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Java is an object-oriented programming language that has many features to help programmers create better code. One of these features is the "extends" keyword. This keyword allows a class to inherit properties & methods from another class. 

Extends Keyword in Java

In this article, we will learn about the extends keyword in Java, its syntax, examples of how to use it, and what happens when we try to extend a final class or interface.

What is Java extends keyword?

The "extends" keyword in Java is used to create a subclass (also known as a derived class or child class) from an existing class (also known as a superclass, base class, or parent class). When a class extends another class, it inherits all the non-private fields & methods of the superclass.

By using the extends keyword, we can create a new class that is a modified version of an existing class. The subclass can add new fields & methods or override the existing methods of the superclass. This allows for code reuse & helps to create a hierarchy of classes that share common properties.

For example, let's say we have a class called "Animal" with properties like name, age, and a method called speak(). We can create a subclass called "Dog" that extends the Animal class & adds a new method called bark(). The Dog class will inherit the name, age, and speak() method from the Animal class, and it will also have its own bark() method.

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

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

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 AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass