Syntax error Difference Between extends and implements keywords in Java

Difference Between extends and implements keywords in Java



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 {
}
Updated on: 2021-03-24T12:42:58+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements