Extends vs Implements in Java - GeeksforGeeks

22 May, 2020
Inheritance is an important pillar of OOP(Object Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. There are two main keywords, “extends” and “implements” which are used in Java for inheritance. In this article, the difference between extends and implements is discussed.
Before getting into the differences, lets first understand in what scenarios each of the keywords are used.
Extends: In Java, the extends keyword is used to indicate that the class which is being defined is derived from the base class using inheritance. So basically, extends keyword is used to extend the functionality of the parent class to the subclass. In Java, multiple inheritances are not allowed due to ambiguity. Therefore, a class can extend only one class to avoid ambiguity.
Example:
class One {    public void methodOne()    {          // Some Functionality    }}  class Two extends One {      public static void main(String args[])    {        Two t = new Two();          // Calls the method one        // of the above class        t.methodOne();    }}
Implements: In Java, the implements keyword is used to implement an interface. An interface is a special type of class which implements a complete abstraction and only contains abstract methods. To access the interface methods, the interface must be “implemented” by another class with the implements keyword and the methods need to be implemented in the class which is inheriting the properties of the interface. Since an interface is not having the implementation of the methods, a class can implement any number of interfaces at a time.
Example
// Defining an interfaceinterface One {    public void methodOne();}  // Defining the second interfaceinterface Two {    public void methodTwo();}  // Implementing the two interfacesclass Three implements One, Two {    public void methodOne()    {          // Implementation of the method    }      public void methodTwo()    {          // Implementation of the method    }}
Note: A class can extend a class and can implement any number of interfaces simultaneously.
Example
// Defining the interfaceinterface One {      // Abstract method    void methodOne();}  // Defining a classclass Two {      // Defining a method    public void methodTwo()    {    }}  // Class which extends the class Two// and implements the interface Oneclass Three extends Two implements One {      public void methodOne()    {          // Implementation of the method    }}
Note: An interface can extend any number of interfaces at a time.
Example:
// Defining the interface Oneinterface One {    void methodOne();}  // Defining the interface Twointerface Two {    void methodTwo();}  // Interface extending both the// defined interfacesinterface Three extends One, Two {}
The following table explains the difference between the extends and interface:
java-inheritance
Java-Object Oriented
Articles
Difference Between
Java
Java Programs
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Analysis of Algorithms | Set 1 (Asymptotic Analysis)
Mutex vs Semaphore
SQL | Views
Time Complexity and Space Complexity
SQL Interview Questions
Difference between BFS and DFS
Class method vs Static method in Python
Differences between TCP and UDP
Difference between var, let and const keywords in JavaScript
Difference Between Method Overloading and Method Overriding in Java