Learn Everything About Implements in Java
In the previous Tutorial of Java, we discussed the Keyword extends in Java. In this article, we have come up with the Keyword implements in Java. We will learn what is an implements keyword in Java and where and how we can use it in Java programs.
We will discuss implements in java with examples and programs to learn the concept better.
Implements Keyword in Java
Java implements keyword is another reserved word in Java that conveys special meaning to the compiler. The implements keyword is useful when we want to use an interface in the class. In short, the implements keyword is useful to implement the interfaces in classes.
We know that an interface in Java is defined as a special type of class that only contains abstract methods inside it; the methods which do not have any implementation or body inside them. It just provides a plan, contract, or a prototype that the classes must follow.
The implements keyword is used by a class so that it can follow or adhere to the contract provided by the interface. The class that implements the interface must provide the concrete implementation of all the methods declared in the interface by providing the method body to the methods.
And, if the class does not provide the implementation of these methods, then it must declare as abstract, otherwise, there will be a compilation error.
Examples of Java Implements
Before moving to the examples of the implements keyword, let’s see the syntax of using this keyword:
Syntax of using implements keyword in java:
1. Interface:
interface InterfaceName { //Abstract methods }
2. Class:
class MyClass implements InterfaceName { //Implementation of all the methods declared in the interface }
Let’s see an example code to understand the use of implements keyword:
Code to understand the implements keyword:
package com.techvidvan.implementskeyword; //Defining the Shape interface interface Shape { //declaring an abstract method of the interface public double getArea(); } //class implements the Shape interface using the implements keyword class Circle implements Shape { public double radius = 5; public double getArea() { return 3.142 * radius * radius; } } class Rectangle implements Shape { public double length = 5; public double breadth = 10; //providing implementation to the interface methods public double getArea() { return length * breadth; } } public class Demo { public static void main(String args[]) { Circle c = new Circle(); double circleArea = c.getArea(); System.out.println("Area of Circle is: " + circleArea); Rectangle r = new Rectangle(); double rectangleArea = r.getArea(); System.out.println("Area of Rectangle is: " + rectangleArea); } }
Output
Area of Rectangle is: 50.0
Example 2:
package com.techvidvan.implementskeyword; //Defining an interface interface Calculator { //Declaring method of the interface public void addition(int a, int b); public void subtraction(int a, int b); } class Calci implements Calculator { //implemeting the methods of the interface public void addition(int a, int b) { System.out.println("Addition: " + (a + b)); } public void subtraction(int a, int b) { System.out.println("Subtraction: " + (a - b)); } } public class MyCalci { public static void main(String[] args) { Calci calci = new Calci(); calci.addition(5, 4); calci.subtraction(9, 4); } }
Output
Subtraction: 5
Multiple Interfaces in Java
Java interface contains the Variables and Methods the same as the class but unlike a class, the interface has undefined methods i.e. methods without any definition. Java Programming Language also provides the feature of defining multiple interfaces in the same program.
Interface in Java is implemented by another class. We created a program that only contains the interface as a communication bridge.
Java does not support multiple inheritances with classes and this feature can implement using multiple interfaces in Java. The following program explains the concept of multiple Interfaces.
Code to explain Java multiple interfaces:
package com.techvidvan.implementskeyword; interface MachineOn { void onMechanism(); } interface MachineOff { void offMechanism(); } //Implementing multiple interfaces class Machine implements MachineOn, MachineOff { public void onMechanism() { System.out.println("Machine is ON"); } public void offMechanism() { System.out.println("Machine is OFF"); } } public class MyExample { public static void main(String args[]) { Machine m = new Machine(); m.onMechanism(); m.offMechanism(); } }
Output
Machine is OFF
Conclusion
So, this was all about the implements keyword in Java. The implements keyword enables a class to use the contract defined by the interfaces in Java. The class provides the actual implementation of the interface.
We discussed two example programs that make the use of the implements keyword in Java. The class can also implement multiple interfaces using the implements keyword and the example for the same has also been covered in the article.