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:
- 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.
- 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.
On this pageWhat is the Difference Between Implements and Extends? Comparative Table: Implements vs Extends
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.
Read more:
- Class vs Interface
- Abstract Class vs Interface
- Abstract Class vs Inheritance
- Inheritance vs Interface in Java
- Overriding vs Overloading
- Extension vs Extention
- Abstract Class vs Concrete Class
- Overloading vs Overriding in Java
- Objects vs Classes
- Superclass vs Subclass
- Virtual vs Abstract
- Package vs Interface in Java
- Intellij vs Eclipse
- Inheritance vs Composition
- Encapsulation vs Abstraction
- Polymorphism vs Inheritance
- Type vs Kind
- Constructor vs Destructor
- Polymorphism vs Inheritance in OOP