Java extends vs implements

Object-Oriented Programming languageoffers different features like encapsulation, inheritance, polymorphism, etc. As Java isan OOP, it supports the concept of inheritance. The two keywords extends and implements are used to achieve inheritance in the Java program. There are somedifferences between extends and implements.

Java extends keyword

  • In Java, inheritance allows a child class to use the methods and variables of already declared base class. It reduces the rewriting of code.
  • Inheritance is implemented using extends keyword.
  • The data members declared inside the super class are accessible to the child class depending on the access specifier used.
  • Any class in Java can only inherit one super class.
  • For example, class ArrayList extends AbstractList class. And AbstractList extends AbstractCollection class.

The following Java program shows the use of extends keyword.

Sample.java

Output:

The above code shows the inheritance is achieved in a Java program. First a BaseClass is declared with some functionalites, and then theChildClass is declared which extends the BaseClass. And in the main() method, instance of  the ChildClass is created to access data members from both the classes.

Java implements keyword

  • Java doesn’t allow multiple inheritance. But it offers concept of interface in order to achieve multiple inheritance.
  • An interface in Java is a similar to a class but it can only have abstract methods and final variables.
  • The class that implements the interface has to define data members declared inside the interface.
  • If the class doesn’t want to implement any method from the interface, it should be declared as an abstract class.
  • An interface can extend multiple interfaces but it cannot implement another interface.

The following Java program shows the use of implements keyword.

InterfaceDemo.java

Output:

In the above Java program, two interfaces, Shape and Color are declared. Class Fruits implements the interfaces and overrides the methods in them. Class InterfaceDemo consists of a main() method that creates an instance of the Fruits class and methods from theFruits class are executed.

Java extends vs implements

Sr. No.extendsImplements
1.extends is a keyword available in Java programming. It is used to inherit a super class.implements is a keyword given in Java programming. It is used to implement an interface.
2.A Java class can extend one super class.A Java class can implement multiple interfaces.
3.An interface is allowed to extend multiple interfaces.An interface is not allowed to implement another interface.
4.The child class has to override all the methods from the base class.The class that implements an interface has to define logic for all the methods of that interface.
5.extends is associated with the concept of inheritance.It is associated with the concept of abstraction.

In conclusion, we can say that inheritance is an important feature of object-oriented programming. In Java, if the developer wants to achieve single inheritance, extends keyword can be used and to achieve multiple inheritance, interfaces can be used with help of implements keyword.

In this article we have discussed two important Java keywords, extends and implements, what they are used for and differences between them.

Pin It on Pinterest

Share This