Programming Concepts
Object-Oriented Programming
Java
Code Implementation
Software Development

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:

java
1public interface Movable {
2    void move(int deltaX, int deltaY);
3}
4
5public class Car implements Movable {
6    @Override
7    public void move(int deltaX, int deltaY) {
8        // logic for Car moving by deltaX and deltaY
9        System.out.println("Car moves by coordinates: " + deltaX + ", " + deltaY);
10    }
11}

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:

java
1public class Vehicle {
2    public void startEngine() {
3        System.out.println("Engine started.");
4    }
5}
6
7public class Truck extends Vehicle {
8    @Override
9    public void startEngine() {
10        super.startEngine();  // Call Vehicle's startEngine
11        System.out.println("Truck engine started with heavy load capacity.");
12    }
13}

Here, Truck extends Vehicle, inheriting and enhancing the startEngine method.

Summary Table

KeywordTypeInheritance TypeJava SupportUse Case
extendsClassSingle inheritanceSingle ClassSpecializing or extending a class
implementsInterfaceMultiple inheritanceMultiple InterfacesImplementing specified behaviors in multiple classes

Additional Considerations

Choosing Between Implements and Extends:

  • Use extends when classes share common attributes and behaviors (is-a relationship).
  • Use implements when classes share a contractual obligation to perform certain actions, but are not fundamentally the same thing (has-a capability).

Practical Implications:

  • extends limits flexibility as Java does not support multiple class inheritance.
  • implements provides 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.


Course illustration
Course illustration

All Rights Reserved.