An Interface in Java
An interface in the Java programming language is defined as an abstract type used to specify the behavior of a class. It is a blueprint of a behavior and contains static constants and abstract methods.
Syntax for Java Interfaces
interface {
// declare constant fields
// declare methods that are abstract
// by default.
}
To declare an interface, use the interface
keyword.
It is used to provide total abstraction. That means all the methods in an interface are declared with an empty body and are public, and all fields are public, static, and final by default. A class that implements an interface must implement all the methods declared in the interface. To implement the interface, use the implements
keyword.
Uses of Interfaces in Java
- It is used to achieve total abstraction.
- Since Java does not support multiple inheritances in the case of a class, by using an interface, it can achieve multiple inheritances.
- Any class can extend only one class but can implement multiple interfaces.
- It is also used to achieve loose coupling.
- Interfaces are used to implement abstraction.
Difference Between Class and Interface
Although Class and Interface may seem similar, there are certain differences between them. The major differences are outlined below:
Class | Interface |
---|---|
In a class, you can instantiate variables and create an object. | In an interface, you must initialize variables as they are final, but you can’t create an object. |
A class can contain concrete (with implementation) methods. | An interface cannot contain concrete (with implementation) methods. |
The access specifiers used with classes are private , protected , and public . |
In an interface, only one specifier is used—public . |
code example:
import java.io.*;
/*
Today we are going to learn about interfaces.
>>> Interfaces are a reference type similar to class
that can contain only constants, method signatures,
default methods, static methods, and nested types.
*/
// Example
interface vehicle {
void speedup(int a);
void accelerate(int a);
void brake(int a);
}
class automobile implements vehicle {
int gear;
int speed;
@Override
public void speedup(int newgear) {
gear = newgear;
}
@Override
public void accelerate(int increment) {
speed = speed + increment;
}
@Override
public void brake(int decrement) {
speed = speed - decrement;
}
public void displayStatus() {
System.out.println("Current Gear: " + gear);
System.out.println("Current Speed: " + speed + " km/h");
}
}
public class vehicle__demo {
public static void main(String[] args) {
automobile myAutomobile = new automobile();
myAutomobile.speedup(4);
myAutomobile.accelerate(80);
myAutomobile.brake(30);
System.out.println("Vehicle Status:");
myAutomobile.displayStatus();
}
}
Conclusion
Interfaces play a crucial role in Java by enabling total abstraction, loose coupling, and achieving multiple inheritances. They are powerful tools for designing robust and scalable applications. By understanding and effectively using interfaces, you can write cleaner and more maintainable code.
Happy coding! 🎉
Top comments (1)
Discover the power of Java interfaces! This article covers everything from the basics to practical applications, including syntax, uses, and an example implementation. Whether you're a beginner or looking to refine your Java skills, this guide will help you unlock the potential of interfaces. Happy coding! 🎉