Table of contents
1.
Introduction
2.
Extends
2.1.
Example
3.
Implements
3.1.
Example
4.
Difference between Extends & Implements
5.
Frequently Asked Questions
5.1.
Can a class extend multiple classes in Java?
5.2.
Is it mandatory for a class implementing an interface to implement all its methods?
5.3.
Can an interface extend another interface?
6.
Conclusion
Last Updated: Nov 8, 2024
Easy

Difference between Extends and Implements in Java

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

Introduction

Keywords in Java are reserved words that the compiler understands for performing specific operations or defining data structures and control flow. Among these, "extends" and "implements" are used to define inheritance relationships between classes & interfaces. Inheritance allows classes to acquire properties and methods from other classes or interfaces. Specifically, "extends" is used for class inheritance, which enables a class to inherit characteristics from another class, while "implements" is used when a class adopts the methods of an interface implementation. 

Difference between Extends and Implements in Java

In this article, we will discuss the differences between "extends" & "implements" in Java with the help of proper examples.

Extends

The "extends" keyword creates a subclass (or derived class) from an existing class, known as the superclass (or base class). The subclass inherits all the non-private fields and methods of the superclass, allowing code reuse and specialization. When a class extends another class, it forms an "is-a" relationship, meaning that the subclass is a specialized version of the superclass.

To use the "extends" keyword, you simply add it after the subclass name, followed by the superclass name. 

The syntax of “extends” is:

class Subclass extends Superclass {
    // Subclass fields & methods
}


By extending a class, the subclass can:

1. Inherit fields & methods from the superclass
 

2. Override inherited methods to provide its own implementation
 

3. Add new fields & methods specific to the subclass

Example

Let’s look at an example to understand how to implement this keyword in our programs. Suppose we have a base class called "Animal" with common properties & behaviors shared by all animals. We can then create a subclass called "Dog" that extends the "Animal" class to add dog-specific characteristics.

class Animal {
    protected String name;    
    public void eat() {
        System.out.println(name + " is eating.");
    }
}

class Dog extends Animal {
    private String breed;
    
    public void bark() {
        System.out.println(name + " is barking.");
    }
}


In this example, the "Dog" class extends the "Animal" class using the "extends" keyword. The "Dog" class inherits the "name" field and the "eat()" method from the "Animal" class. It also adds a new field, "breed" and a new method, "bark()" specific to dogs.

We can create objects of the "Dog" class & access both the inherited & dog-specific members:

Dog myDog = new Dog();
myDog.name = "Buddy";
myDog.eat();    // Output: Buddy is eating.
myDog.bark();   // Output: Buddy is barking.


This example shows how the "extends" keyword allows the "Dog" class to inherit & reuse code from the "Animal" class while adding its own unique features.

Implements

In Java, the "implements" keyword is used to implement an interface in a class. An interface is a collection of abstract methods (methods without a body) & constant fields that define a contract for classes to follow. When a class implements an interface, it agrees to provide an implementation for all the methods declared in the interface.

To use the "implements" keyword, you add it after the class name, followed by the interface name. If a class implements multiple interfaces, they are separated by commas. 
 

The syntax is:

class ClassName implements InterfaceName1, InterfaceName2, ... {
    // Implementations of interface methods
}


When a class implements an interface:

1. It must provide implementations for all the methods declared in the interface.
 

2. It can implement multiple interfaces, achieving a form of multiple inheritance.
 

3. It can provide additional methods beyond those specified in the interface.


Note: Implementing an interface allows a class to adhere to a specific relationship, which enables polymorphism and loose coupling between objects.

Example

Let's discuss an example to understand the implementation of the "implements" keyword. Suppose we have an interface called "Drawable" that declares a method "draw()". We can then create classes that implement the "Drawable" interface to provide their own implementations of the "draw()" method.

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.");
    }
}


In this example, the "Circle" & "Rectangle" classes implement the "Drawable" interface using the "implements" keyword. Each class provides its own implementation of the "draw()" method as required by the interface.

We can create objects of these classes & treat them as instances of the "Drawable" interface:

Drawable shape1 = new Circle();
Drawable shape2 = new Rectangle();

shape1.draw();  // Output: Drawing a circle.
shape2.draw();  // Output: Drawing a rectangle.


This example explains how the "implements" keyword allows classes to adhere to a common interface, enabling polymorphism. We can assign objects of the implementing classes to variables of the interface type & invoke the interface methods on them.

Difference between Extends & Implements

ParametersExtendsImplements
PurposeThe extends keyword creates a subclass from an existing class, establishing an inheritance relationship between the subclass and the superclass.The implements keyword is used to implement an interface in a class, indicating that the class will provide implementations for all methods declared in the interface.
Inheritance TypeUsed for class inheritance, where a subclass inherits properties and methods from a superclass, forming an "is-a" relationship.Used for interface implementation, where a class adheres to the contract defined by an interface, establishing a "can-do" relationship.
Multiple InheritanceA class can extend only one superclass, as Java does not support multiple inheritance of classes.A class can implement multiple interfaces, allowing a form of multiple inheritance through interfaces.
Method ImplementationInherits non-private methods of the superclass and can override them with its own implementations.Must provide implementations for all methods declared in the interface.
Access to FieldsA subclass can inherit and access non-private fields of its superclass.An interface cannot have fields, only constants, and implementing classes cannot access fields through the interface directly.
ConstructorsConstructors are not inherited by subclasses, but a subclass can call the superclass constructor using the super keyword.Interfaces do not have constructors. Implementing classes define their own constructors.
FlexibilityClass inheritance is more rigid, defining an "is-a" relationship and creating a tighter coupling between the subclass and superclass.Interface implementation provides flexibility, defining a "can-do" relationship and allowing classes to implement multiple interfaces for a more flexible design.

Frequently Asked Questions

Can a class extend multiple classes in Java?

No, a class cannot extend multiple classes in Java. Java does not support multiple inheritance of classes to avoid ambiguity & complexity. However, a class can implement multiple interfaces.

Is it mandatory for a class implementing an interface to implement all its methods?

Yes, a class that implements an interface must provide implementations for all the methods declared in the interface. If any method is not implemented, the class must be declared as abstract.

Can an interface extend another interface?

Yes, an interface can extend another interface using the "extends" keyword. This allows for creating hierarchies of interfaces & defining more specialized contracts.

Conclusion

In this article, we discussed the differences between the "extends" & "implements" keywords in Java. We learned that "extends" is used for class inheritance, which allows a subclass to inherit properties & methods from a superclass, while "implements" is used for interface implementation, which enables a class to adhere to the specific relationship defined by an interface. Apart from this, we also discussed important aspects like multiple inheritance, method implementation, & flexibility. 

You can also check out our other blogs on Code360.

Live masterclass