Difference Between extends and implements keywords in Java

JavaServer Side ProgrammingProgramming

In this post, we will understand the differences between ‘Extends’ and ‘Implements’ keyword.

Extends

  • Using this, a class can be used as a base class, and another class inherits this base class.

  • An interface can also inherit other interfaces using this keyword.

  • Only one superclass can be extended by a class.

  • Any number of interfaces can be extended by an interface.

  • It is not required for the subclass (that extends a superclass) to override all the methods in the superclass.

Following is an example of the extends keyword −

Example

class Super {
   .....
   .....
}
class Sub extends Super {
   .....
   .....
}

Implements

  • This keyword helps a class to implement an interface.

  • A class can implement any number of interfaces at a point in time.

  • It is required for a class (that implements an interface) to implement all the methods of that specific interface.

  • It can never be used implement any other interface.

Following is an example of the implements keyword

Example

public interface Animal {
}
public class Mammal implements Animal {
}
public class Dog extends Mammal {
}
raja
Updated on 24-Mar-2021 12:42:58

Advertisements