Extend or Implement
In Java, deciding whether to extend a class or implement an interface depends on the nature of the relationship and what you're trying to achieve. Here's a breakdown:
1. Use extends
to Extend a Class
You extend a class when:
- You need inheritance: The subclass should inherit properties and behavior (methods) from the superclass.
- There is an "is-a" relationship: The subclass is a specific type of the superclass. For example:java
class Animal { ... } class Dog extends Animal { ... } // A Dog is an Animal
- You want to reuse code: The subclass can use or override methods defined in the superclass.
- You can inherit only one class (Java allows single inheritance of classes).
However, extending a class should be used sparingly, as it tightly couples the subclass to the parent class, making code harder to maintain.
2. Use implements
to Implement an Interface
You implement an interface when:
- You want to define a contract: The class agrees to provide implementations for all the abstract methods defined in the interface.
- There is a "can-do" relationship: The class "can" perform a certain behavior. For example:java
interface Swimmable { void swim(); } class Fish implements Swimmable { @Override public void swim() { System.out.println("Fish is swimming."); } } // A Fish can swim
- Multiple inheritance is needed: Java allows a class to implement multiple interfaces, enabling you to combine different capabilities.java
interface Flyable { void fly(); } interface Swimmable { void swim(); } class Duck implements Flyable, Swimmable { @Override public void fly() { System.out.println("Duck is flying."); } @Override public void swim() { System.out.println("Duck is swimming."); } }
Key Differences: extends
vs. implements
Aspect | extends | implements |
---|---|---|
Relationship | "is-a" relationship | "can-do" relationship |
Usage | Extends an existing class | Implements methods from an interface |
Inheritance | Single class inheritance | Multiple interfaces can be implemented |
Abstract Methods | May inherit concrete methods | Must implement all methods of the interface |
Code Reuse | Can reuse inherited code | Requires explicit implementation of methods |
When to Use Which?
Extend a class:
- When the subclass is a specialized version of the superclass.
- To reuse existing functionality provided by the parent class.
- Avoid extending if it leads to forced inheritance of unrelated functionality.
Implement an interface:
- When you need to guarantee specific behavior across multiple classes.
- To create more flexible and decoupled designs (a class can implement multiple interfaces).
- Prefer interfaces for defining capabilities or functionality without forcing a class hierarchy.
Combination of Both
In some cases, a class might extend a class and implement an interface:
java
class Animal { ... }
interface Runnable { void run(); }
class Dog extends Animal implements Runnable {
@Override
public void run() {
System.out.println("Dog is running.");
}
}
This approach allows the class to inherit core behavior from one class while adhering to contracts defined by one or more interfaces.