In Java, an interface is a reference type similar to a class. It can contain:
public
, static
, and final
by default.NOTE:
An interface is used to achieve abstraction and multiple inheritance in Java.
default
or static
). This means that they don’t have any body and must be implemented by the classes that implement the interface.Public methods: All methods in an interface are implicitly public
, meaning you don't have to specify the public
modifier. For example:
interface MyInterface {
// This is implicitly public and abstract
void myMethod();
}
Constants: All variables declared in an interface are implicitly public, static, and final. This means you can’t change their value after initialization.
interface MyInterface {
// This is implicitly public static final
int MY_CONSTANT = 10;
}
Syntax of Interface
Here's a basic example of an interface:
public interface Drawable {
// Abstract method
void draw();
// Abstract method
int getColor();
}
import java.util.*;
interface Drawable {
void draw(); // Abstract method
String getColor(); // Abstract method
}
class Circle implements Drawable {
private String color = "Green";
public void draw() {
System.out.println("Drawing a circle");
}
public String getColor() {
return this.color;
}
}
public class Test
{
public static void main(String[] args)
{
Drawable d = new Circle();
d.draw();
System.out.println(d.getColor());
}
}
Drawing a circle
Green
In this example, the Circle
class implements the Drawable
interface by providing concrete implementations for the abstract methods draw()
and getColor()
.
Polymorphism: Interfaces can be used polymorphically. A class that implements an interface can be referred to by a reference variable of the interface type.
Drawable drawable = new Circle(); // Polymorphism
drawable.draw(); // Calls the draw() method of Circle
xxxxxxxxxx
// Multiple inheritance
interface A {
void methodA();
}
interface B {
void methodB();
}
public class MyClass implements A, B {
public void methodA() {
System.out.println("Implementing methodA");
}
public void methodB() {
System.out.println("Implementing methodB");
}
}
Interfaces could contain only abstract methods (i.e., method declarations without any implementation).
public interface Animal {
void sound();
}
Java 8 introduced default methods and static methods in interfaces, enhancing their capabilities.
xxxxxxxxxx
public interface MyInterface {
default void start() {
System.out.println("Vehicle is starting");
}
static int add(int a, int b) {
return a + b;
}
}
// If a class implements this interface, it inherits the start() method unless you chooses to override it.
public class MyClass implements MyInterface {
// Inherits the default start() method
MyInterface i = new MyClass();
i.start();
// Usage
int result = MyInterface.add(5, 3); // Calls static method of interface
}
xxxxxxxxxx
public interface Logger {
default void logInfo(String message) {
log(message, "INFO");
}
default void logError(String message) {
log(message, "ERROR");
}
private void log(String message, String level) {
System.out.println(level + ": " + message);
}
}
Interfaces support multiple inheritance, allowing an interface to extend multiple interfaces:
xxxxxxxxxx
interface X {
void methodX();
}
interface Y {
void methodY();
}
interface Z extends X, Y {
void methodZ();
}
class MyClass implements Z {
public void methodX() { System.out.println("Method X"); }
public void methodY() { System.out.println("Method Y"); }
public void methodZ() { System.out.println("Method Z"); }
}
In this case, MyClass
must implement methods from all the interfaces it extends (X
, Y
, and Z
).
xxxxxxxxxx
interface A {
int x = 10; // public static final
void m1();
}
interface B {
int x = 20; // public static final
void m1();
}
class C implements A, B {
public void m1() {
System.out.println("Implementing m1 from both interfaces");
}
System.out.println(A.x); // Refers to A's x
System.out.println(B.x); // Refers to B's x
}