The main difference between extends and implements in Java is that the keyword “extends” helps to use properties and methods of a superclass while the keyword “implements” allows a class to implement an interface.
Java is a high level, general purpose programming language developed by James Gosling. It is used to build various applications such as standalone, mobile, web, etc. One major reason for the popularity of Java is that it supports Object Oriented Programming (OOP) paradigm. Using OOP, the developers can model the real world scenarios easily to create solutions. Two main concepts in OOP are inheritance and abstractions. The extends, and implements are keywords that are associated with these two paradigms.
Key Areas Covered
1. What is extends
– Definition, Functionality
2. What is implements
– Definition, Functionality
3. What is the Difference Between extends and implements in Java
– Comparison of Key Differences
Abstraction, extends, implements, Inheritance, Interface, Java, OOP
What is extends in Java
Inheritance allows a new class to use properties and methods of an already existing class. It improves code reusability. In other words, the programmer can create new classes using already existing classes. Therefore, it is not necessary to write programs from the beginning.
Moreover, the concept of inheritance builds parent-child relationships between the classes. Here, the existing class is called the parent class or a superclass while the new class is called the child class or subclass. When the subclass inherit properties and methods of the superclass, the child class uses the extends keyword.
In the above program, class Color has a method called display. Class Green has a method called displayNewColor. Class Green extends class Color. Therefore, Color is the superclass while Green is the subclass. In the main method, obj is an object of type Green. As Green is a subclass of Color, it can inherit properties and methods of Color class. Therefore, the programmer can call the displayNewColor as well as display method using the object.
What is implements in Java
Abstraction allows hiding the implementation details and displaying the functionality to the user. Interface is a way of achieving abstraction, and it consists of abstract methods. Abstract methods do not have method definitions. They only contain the method declarations. Therefore, it is not possible to create objects using an interface.
A class can implement the abstract methods in an interface. When the class implements an interface, it uses the keyword implements. An interface cannot implement an interface because it can never implement a method. In addition, an interface can extend one or more interfaces at a time.
In the above program, Draw is an interface, which has an abstract method called draw. It does not have a method definition. Triangle is a class, and it implements the interface Draw. Therefore, the Triangle class implements the draw method. In other words, it provides the method definition to the draw method. In the main method, obj is an object of type Draw. Finally, the programmer can call that method.
Difference Between extends and implements in Java
The extends is a keyword available in Java programming language that allows a class to use the features of an already existing class while the implements is a keyword available in Java programming language that allows a class to provide definitions to the abstract methods of an interface. Thus, this is the fundamental difference between extends and implements in Java.
Also, a class can extend one superclass while a class can implement one or more interfaces.
Another difference between extends and implements in Java is that an interface can extend one or more interfaces whereas an interface cannot implement another interface.
Associated OOP Concept
While extends keyword is associated with inheritance, implements keyword is associated with abstraction. Hence, this is another major difference between extends and implements in Java.
A programmer can use extends and implements keywords in OOP. The main difference between extends and implements in Java is that extends keyword helps to use properties and methods of a superclass while implements keyword allows a class to implement an interface.