Table of Contents
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 learning about 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 uncertainity. Therefore, a class can extend only one class to avoid uncertainity.
Learn Coding in your Language! Enroll Here!
Example:
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(); } } |
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.
Example
// 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: A class can extend a class and can implement any number of interfaces simultaneously.
Example
// Defining the interface interface One { // Abstract method void methodOne(); } // Defining a class class Two { // Defining a method public void methodTwo() { } } // Class which extends the class Two // and implements the interface One class Three extends Two implements One { public void methodOne() { // Implementation of the method } } |
Note: An interface can extend any number of interfaces at a time.
Learn to code from industry experts! Enroll here
Example:
// 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 { } |
Implements vs Extends
Implements and Extends are two keywords found in Java programming language that provides a means of transferring added functionality to a new class. Implements keyword is used explicitly for implementing an interface, while Extends keyword is used for inheriting from a (super) class. Please note that the concepts of inheritance and interfaces are present in most of the other object oriented programming languages like C# and VB.NET, but they offer different syntax or keywords for applying those concepts.
Extends
Extends keyword is used to implement the concept of inheritance in Java programming language. Inheritance essentially provides code reuse by allowing extending properties and behavior of an existing class by a newly defined class. When a new subclass (or derived class) extends a super class (or parent class) that subclass will inherit all attributes and methods of the super class. The subclass can optionally override the behavior (provide new or extended functionality to methods) inherited from the parent class. A subclass cannot extend multiple super classes in Java. Therefore, you cannot use extends for multiple inheritance. In order to have multiple inheritance, you need to use interfaces as explained below.
Implements
1: What is the default value of a boolean in Java?
Implements keyword in Java programming language is used for implementing an interface by a class. An interface in Java is an abstract type that is used to specify a contract that should be implemented by classes, which implement that interface. Usually an interface will only contain method signatures and constant declarations. Any interface that implements a particular interface should implement all methods defined in the interface, or should be declared as an abstract class. In Java, the type of an object reference can be defined as an interface type. But that object must either be null or should hold an object of a class, which implements that particular interface. Using Implements keyword in Java, you can implement multiple interfaces to a single class. An Interface cannot implement another interface. However an interface can extend a class.
Grab the opportunity to learn Python with Entri! Click Here
Difference between Implements and Extends
Although, Implements and Extends are two keywords that provide a mechanism to inherit attributes and behavior to a class in Java programming language, they are used for two different purposes. Implements keyword is used for a class to implement a certain interface, while Extends keyword is used for a subclass to extend from a super class. When a class implements an interface, that class needs to implement all the methods defined in the interface, but when a subclass extends a super class, it may or may not override the methods included in the parent class. Finally, another key difference between Implements and Extends is that, a class can implement multiple interfaces but it can only extend from one super class in Java. In general, usage of Implements (interfaces) is considered more favorable compared to the usage of Extends (inheritance), for several reasons like higher flexibility and the ability to minimize coupling. Therefore in practice, programming to an interface is preferred over extending from base classes.
The following table explains the difference between the extends and interface:
S.No. | Extends | Implements |
---|---|---|
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 |