Extends in Java vs. Implements in Java — What's the Difference?
By Tayyaba Rehman — Published on January 12, 2024
Extends in Java is used for class inheritance, allowing a class to inherit properties from another class. Implements in Java is used by a class to adhere to an interface, defining specific methods.
Difference Between Extends in Java and Implements in Java
Table of Contents
ADVERTISEMENT
Key Differences
Extends in Java is a keyword used for inheritance in class hierarchies. When one class extends another, it inherits all non-private members (methods and variables) from the parent class. In contrast, Implements in Java is used when a class needs to implement an interface. Interfaces define methods that the implementing class must provide concrete implementations for.
Extends is used for establishing an "is-a" relationship, where the subclass is a more specific version of the superclass. For example, a Dog class extending an Animal class indicates that a Dog is a type of Animal. On the other hand, Implements indicates that a class agrees to perform certain behaviors as prescribed by the interface. For instance, a Car class implementing a Drivable interface suggests that a Car can be driven.
In Java, a class can only extend one superclass due to single inheritance, but it can implement multiple interfaces. This limitation in Extends is due to the complexity and ambiguity that multiple inheritances can introduce. However, Implements allows for more flexibility and helps to avoid the diamond problem, as interfaces do not hold an implementation.
Extends is about code reuse and relationship. Subclasses can leverage and override methods from the parent class. However, Implements is about defining a contract that the implementing class must follow. It is more about adhering to a specific behavior or interface rather than reusing code.
With Extends, subclasses inherit both the behavior and the state of the superclass. In contrast, with Implements, a class only gets the form (method signatures) from the interface but must define the behavior (method bodies) itself.
ADVERTISEMENT
Comparison Chart
Purpose
Inheritance of properties and methods from one class
Adherence to a contract defined by an interface
Relationship
Establishes an "is-a" relationship
Defines a contract to be fulfilled
Multiple Usage
Only one superclass can be extended
Multiple interfaces can be implemented
Method Implementation
Inherits concrete methods
Must provide implementation for abstract methods
Flexibility
Limited by single inheritance
Offers flexibility with multiple interfaces
Compare with Definitions
Extends in Java
Extends supports polymorphism in object-oriented programming.
Animal a = new Dog();
Implements in Java
Ensures classes adhere to specific method signatures.
Public void startEngine() { ... } // as defined in an interface
Extends in Java
It's used for code reuse and establishing relationships.
Class ElectricCar extends Car { ... }
Implements in Java
It requires providing concrete implementations of interface methods.
Public void drive() { ... } // in Car class
Extends in Java
Extends provides access to superclass methods and fields.
Super.eat(); // inside Dog class
Implements in Java
Implements provides a way to achieve abstraction.
Public interface Drivable { void drive(); }
Extends in Java
Allows overriding of superclass methods in the subclass.
@Override public void eat() { ... } // in Dog class
Implements in Java
Supports multiple implementations without inheritance issues.
Class ElectricCar implements Drivable, Rechargeable { ... }
Extends in Java
Extends enables a class to inherit from another class.
Class Dog extends Animal { ... }
Implements in Java
Implements is used to follow an interface's contract.
Class Car implements Drivable { ... }
Common Curiosities
Can a Java class extend multiple classes?
No, Java supports single inheritance, so a class can only extend one class.
How many interfaces can a Java class implement?
A class can implement multiple interfaces.
What does extends do in Java?
It allows a class to inherit properties and methods from another class.
What's the role of implements in Java?
It's used by classes to adhere to and implement the methods defined in an interface.
Is it mandatory to override methods when using extends?
It's not mandatory to override, but it's common to override methods for specific behaviors.
What happens if a class doesn't implement all interface methods?
The class must be declared abstract, or it will result in a compilation error.
Do we need to provide method bodies when implementing an interface?
Yes, all methods defined in an interface must be implemented with concrete bodies in the class.
Can a subclass override static methods of its superclass?
No, static methods cannot be overridden.
Why use extends in Java?
It's used for code reuse, polymorphism, and establishing hierarchical relationships.
Can a class both extend another class and implement an interface?
Yes, a class can extend one class and implement any number of interfaces.
Does extends affect access to private members of the superclass?
No, private members of the superclass are not accessible to the subclass.
Why use implements in Java?
To ensure a class follows a specific contract or behavior defined by an interface.
Can interfaces extend other interfaces?
Yes, interfaces can extend other interfaces.
Are implements and extends keywords interchangeable?
No, they serve different purposes and are not interchangeable.
How does implements help in Java?
It helps in achieving abstraction, decoupling, and multiple inheritance of types.
Share Your Discovery
Previous Comparison
Jews vs. ChristiansNext Comparison
Club Soda vs. Soda WaterAuthor Spotlight
Written by
Tayyaba RehmanTayyaba Rehman is a distinguished writer, currently serving as a primary contributor to askdifference.com. As a researcher in semantics and etymology, Tayyaba's passion for the complexity of languages and their distinctions has found a perfect home on the platform. Tayyaba delves into the intricacies of language, distinguishing between commonly confused words and phrases, thereby providing clarity for readers worldwide.