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.
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.
Note: A class can extend a class and can implement any number of interfaces simultaneously.
Note: An interface can extend any number of interfaces at a time.
The following table explains the difference between the extends and interface:
|1.||By using “extends” keyword a class can inherit another class, or an interface can inherit other interfaces||By using “implements” keyword a class can implement an interface|
|2.||It is not compulsory that subclass that extends a superclass override all the methods in a superclass.||It is compulsory that class implementing an interface has to implement all the methods of that interface.|
|3.||Only one superclass can be extended by a class.||A class can implement any number of an interface at a time|
|4.||Any number of interfaces can be extended by interface.||An interface can never implement any other interface|
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Main App Implements Runnable | Concurrent Programming Approach 2
- Inner Class And Anonymous Inner Class that Implements Runnable | Concurrent Programming Approach 3
- Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
- Java.util.function.LongPredicate interface in Java with Examples
- Java Deprecated API Scanner tool (jdepscan) in Java 9 with Examples
- Java.util.function.DoublePredicate interface in Java with Examples
- Java lang.Long.reverse() method in Java with Examples
- Java lang.Long.byteValue() method in Java with Examples
- Java.util.concurrent.RecursiveTask class in Java with Examples
- Java.lang.Short toString() method in Java with Examples
- Java.util.function.IntPredicate interface in Java with Examples
- Java.util.concurrent.RecursiveAction class in Java with Examples
- Java.util.LinkedList.offer(), offerFirst(), offerLast() in Java
- Java.util.LinkedList.peek() , peekfirst(), peeklast() in Java
- Java.util.Collections.disjoint() Method in java with Examples
- Java lang.Long.highestOneBit() method in Java with Examples
- Java.util.function.BiPredicate interface in Java with Examples
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
- Java.util.Collections.rotate() Method in Java with Examples
- Java.util.concurrent.Phaser class in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.