Open In App

Extends vs Implements in Java

Last Updated : 27 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Java, the extends keyword is used to inherit all the properties and methods of the parent class while the implements keyword is used to implement the method defined in an interface.

Difference Between extends and implements Keyword

S.No.ExtendsImplements
1.By using "extends" keyword a class can inherit another class, or an interface can inherit from other interfacesBy using "implements" keyword a class can implement an interface
2.It is not compulsory for a subclass that extends a superclass to override all the methods in a superclass.It is compulsory for a class implementing an interface to implement all the methods of that interface.
3.A class can extend only one super classA class can implement any number of an interface at the same time
4.Any number of interfaces can be extended by interface.An interface can extend other interfaces but cannot implements them.

"extends" Keyword in Java

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: Below example demonstrating the use of extends keyword in java.

Java
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();
    }
}

Note: A class can extend a class and can implement any number of interfaces simultaneously.

"implements" Keyword in Java

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: Below example demonstrating the use of implements keyword in java.

Java
// Defining an interface
interface One {
    public void methodOne();
}

// Defining the second interface
interface Two {
    public void methodTwo();
}

// Implementing the two interfaces
class Three implements One, Two {
    public void methodOne()
    {

        // Implementation of the method
    }

    public void methodTwo()
    {

        // Implementation of the method
    }
}

Note: An interface can extend any number of interfaces at a time.


Example: Below example demonstrating that an interface can extend multiple interface.

Java
// Defining the interface One
interface One {
    void methodOne();
}

// Defining the interface Two
interface Two {
    void methodTwo();
}

// Interface extending both the
// defined interfaces
interface Three extends One, Two {
}

Next Article

Similar Reads