What is the Difference Between Implements and Extends?

🆚 Go to Comparative Table 🆚

The main difference between "extends" and "implements" in Java lies in the kind of inheritance they provide. Here are the key differences:

  1. Extends: This keyword is used to create a new class that inherits properties and methods from an existing class, and it can add new properties and methods or override existing ones. In other words, "extends" is used for extending a class or interface. A class can extend only one class, but it can implement any number of interfaces.
  2. Implements: This keyword is used to define a contract that a class must follow, such as implementing a set of method signatures. In other words, "implements" is used for implementing interfaces. It is compulsory for a class implementing an interface to implement all the methods of that interface.

In summary:

  • "Extends" is used to create a subclass that inherits from a superclass and can override or extend its functionality.
  • "Implements" is used for a class to follow a contract defined by an interface, ensuring that the class provides the required behavior specified by the interface.

Comparative Table: Implements vs Extends

Here is a table comparing the differences between the implements and extends keywords in Java:

Feature Implements Extends
Purpose Used when a class wants to implement an interface, providing the implementation of all the methods of that interface. Used when a class wants to inherit properties (fields and methods) from another class or when an interface wants to inherit properties from another interface.
Inheritance Associated with Abstraction. Associated with Inheritance.
Methods A class must implement all the methods of the interface it implements. A subclass may or may not override all the methods present in the parent class.
Class Limitations A class can implement one or more interfaces at the same time. A class can extend only one class at the same time.
Interface Limitations Interfaces can extend other interfaces. Interfaces can never implement other interfaces.

In summary, implements is used when a class wants to provide the implementation of an interface, while extends is used when a class wants to inherit properties from another class or when an interface wants to inherit properties from another interface. A class can implement multiple interfaces, but it can extend only one class at a time.