Implements vs extends When to use? What's the difference?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In object-oriented programming, the concepts of "implements" and "extends" are fundamental to inheritance and polymorphism. Each serves a specific purpose and choosing the right one is crucial depending on the scenario. Here, we'll explore the differences between these two mechanisms, when to use each, and present real-world examples in Java, a language that distinctively uses both constructs.
Implements
The keyword implements in Java is used when a class needs to adhere to a particular interface. An interface in Java is an abstract type that is used to specify a behavior that classes must implement, without specifying how the behaviors should be implemented.
Characteristics of Interfaces:
- Interfaces cannot contain concrete methods (methods with a body) unless they are defined as default or static.
- They can only contain method signatures (abstract methods), static methods, and default methods (from Java 8 onwards).
- A class that implements an interface must provide explicit implementations of the interface's abstract methods.
When to Use Implements:
- When you need multiple class implementations sharing the same method signatures.
- To enforce certain functionalities across several classes.
- When you are dealing with multiple inheritance of type, since Java does not support multiple class inheritance but allows a class to implement multiple interfaces.
Example:
Here, Car implements the Movable interface, which requires implementing the move method.
Extends
The keyword extends in Java is used when a class inherits from a superclass, acquiring its methods and properties. Inheritance is a mechanism wherein a new class is derived from an existing class.
Characteristics of Class Inheritance:
- Subclasses inherit all the accessible methods and properties from their superclass but can also introduce their own methods and properties.
- A subclass can override methods of the superclass to provide specific implementations.
- Unlike interfaces, Java classes support single inheritance, meaning a class can only extend one other class.
When to Use Extends:
- To provide an enhanced or specialized functionality of an existing class.
- To promote code reusability by using functionality from the superclass.
- When there's a clear hierarchical relationship (is-a relationship).
Example:
Here, Truck extends Vehicle, inheriting and enhancing the startEngine method.
Summary Table
| Keyword | Type | Inheritance Type | Java Support | Use Case |
| extends | Class | Single inheritance | Single Class | Specializing or extending a class |
| implements | Interface | Multiple inheritance | Multiple Interfaces | Implementing specified behaviors in multiple classes |
Additional Considerations
Choosing Between Implements and Extends:
- Use
extendswhen classes share common attributes and behaviors (is-a relationship). - Use
implementswhen classes share a contractual obligation to perform certain actions, but are not fundamentally the same thing (has-a capability).
Practical Implications:
extendslimits flexibility as Java does not support multiple class inheritance.implementsprovides greater flexibility and is preferred in many design patterns, like Strategy or Observer patterns.
By understanding the nuances and appropriate applications of implements and extends, programmers can design more efficient, logical, and maintainable object-oriented applications.

