Adding an implement clause to the class declaration allows you to declare a class that complies with an interface.
A list of the interfaces that your class implements, separated by commas, comes after the keyword implements since your class may implement more than one interface.

What is an interface in Java?
An interface is a completely "abstract class" that is used to group functions with empty bodies that do similar things.
Example:
// interface in Java
interface Cat {
public void carSound(); // interface method (does not have a body)
public void run(); // interface default method (does not have a body)
}To gain access to the interface's methods, another class must "implement" the interface by using the implements keyword (instead of the extends keyword).
This is analogous to inheriting the interface's functionality.
The "implement" class is in charge of providing the body of the abstract method defined, which is often called the "implementation."
Example:
// Interface
interface Car {
public void carSound(); // interface method (does not have a body)
public void start(); // interface method (does not have a body)
}
// BMW "implements" the Car interface
class BMW implements Car {
public void carSound() {
// The body of carSound() is provided here
System.out.println("Brooommm Brooommm broooommmmm");
}
public void start() {
// The body of sleep() is provided here
System.out.println("Start engine");
}
}
public class Main {
public static void main(String[] args) {
BMW myCar = new BMW();
myCar.start();
myCar.carSound();
}
}Output:
Start engine
Brooommm Brooommm broooommmmmIn order for you to test the Java code provided in this lesson, you must test the code in your code editor.
But if you wish to run this code online, we also have an online compiler in Java for you to test your Java code for free.
You can test the above example here! ➡Java Online Compiler
How to declare an Interface in Java?
The interface keyword is used to declare an interface. It is applied to offer complete abstraction.
This means that by default, all fields in an interface are public, static methods, and final and that all methods in an interface are declared with an empty body.
Example of an interface in Java
Let us look at a Java Interface example that is more useful.
// program created by Glenn
// To use the sqrt function
interface Polygon {
void getArea();
// calculate the perimeter of a Polygon
default void getPerimeter(int... sides) {
int perimeter = 0;
for (int side: sides) {
perimeter += side;
}
System.out.println("Perimeter: " + perimeter);
}
}
class Triangle implements Polygon {
private int a, b, c;
private double s, area;
// initializing sides of a triangle
Triangle(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
s = 0;
}
// calculate the area of a triangle
public void getArea() {
s = (double) (a + b + c)/2;
area = Math.sqrt(s*(s-a)*(s-b)*(s-c));
System.out.println("Area: " + area);
}
}
public class Main {
public static void main(String[] args) {
Triangle t1 = new Triangle(6, 5, 7);
// calls the method of the Triangle class
t1.getArea();
// calls the method of Polygon
t1.getPerimeter(6, 5, 7);
}
}Implementation of interfaces
To declare a class that uses an interface, add a "implements" clause.
Your class can implement more than one interface, so the word implement is followed by a comma-separated list.
📌To continue the discussion click the link below: ⬇️⬇️⬇️
Conclusion
Implementing interfaces in Java is a powerful way to achieve abstraction and enforce consistent behavior across classes.
By using the implements keyword, developers can define contracts that classes must fulfill, promoting modularity, flexibility, and clean architecture.
With practice, mastering interfaces will elevate your Java programming skills and prepare you for more advanced object-oriented design.
If you find this article valuable, please consider leaving a comment below and sharing your thoughts.
Your feedback will not only help us improve our content but also benefit others in the community by providing diverse insights and experiences.
Itsourcecode.com🚀
Thank you for being a part of the Itsourcecode community!
Before you leave, please consider the following:
I would appreciate it if you could show your support by clapping 50 times and following the author.
Follow us on [Pinterest]
Follow us on [Facebook]