📕 Definition
Java Interface is a mechanism used to achieve abstraction in Java. It is a collection of abstract methods that are used to specify a behavior that a class must implement. In Java, interfaces are used to achieve multiple inheritance, where a class can implement multiple interfaces.
Here are the key topics related to Java interfaces that will be covered in this explanation:
Interface syntax and declaration
Implementing an interface
Using interfaces for multiple inheritance
Default methods in interfaces
Static methods in interfaces
Marker interfaces
Benefits of using interfaces
📕 Interface syntax and declaration
The syntax for declaring an interface in Java is as follows:
public interface MyInterface {
// Method signatures (no implementation)
}
The public keyword indicates that the interface can be accessed from anywhere in the code. The interface name (MyInterface in this case) should be in PascalCase format.
An interface can have any number of method signatures (also called abstract methods), and no implementation is provided for these methods. For example:
public interface Vehicle {
void start();
void stop();
}
In this example, the Vehicle interface has two method signatures, start() and stop(). Any class that implements the Vehicle interface must provide an implementation for these methods.
📕 Implementing an interface
To implement an interface, a class must use the implements keyword followed by the interface name. For example:
public class Car implements Vehicle {
public void start() {
// Implementation for starting a car
}
public void stop() {
// Implementation for stopping a car
}
}
In this example, the Car class implements the Vehicle interface, which requires it to provide an implementation for the start() and stop() methods.
♦ Default methods in interfaces
Starting from Java 8, interfaces can have default methods, which provide a default implementation for a method. Default methods are declared using the default keyword. For example:
public interface Vehicle {
void start();
void stop();
default void horn() {
System.out.println("Beep beep!");
}
}
In this example, the Vehicle interface has a default method called horn(), which provides a default implementation for the method. Any class that implements the Vehicle interface can choose to override the horn() method or use the default implementation.
♦ Static methods in interfaces
Starting from Java 8, interfaces can also have static methods, which are declared using the static keyword. For example:
public interface Calculator {
static int add(int a, int b) {
return a + b;
}
}
In this example, the Calculator interface has a static method called add(), which can be called without creating an instance of a class that implements the interface.
📕 Marker interfaces
A marker interface is an interface that does not contain any methods or constants. Its only purpose is to mark or tag a class. Examples of marker interfaces in Java include Serializable and Cloneable.
Example:
public interface MyMarkerInterface {
}
♦ Default Methods:
In Java 8, interfaces introduced the concept of default methods. Default methods are the methods that have a default implementation in the interface itself. Default methods can be used to add new functionalities to the interface without affecting the classes that implement the interface.
Example:
public interface MyInterface {
default void myMethod() {
System.out.println("Default implementation of myMethod");
}
}
♦ Static Methods:
Starting from Java 8, interfaces can also have static methods. Static methods in an interface can be called using the interface name and do not require any instance of the interface.
Example:
public interface MyInterface {
static void myStaticMethod() {
System.out.println("Static method in interface");
}
}
♦ Functional Interfaces:
A functional interface is an interface that has only one abstract method. Functional interfaces are used in lambda expressions and method references.
Example:
@FunctionalInterface
public interface MyFunctionalInterface {
void myMethod();
}
♦ Nested Interfaces:
An interface can also be declared inside another interface or class. This is called a nested interface.
Example:
public interface MyOuterInterface {
void myMethod();
interface MyInnerInterface {
void myInnerMethod();
}
}
📕 Inheritance of Interfaces:
An interface can extend one or more interfaces, and the classes that implement the interface must provide implementation for all the methods in the interface hierarchy.
Example:
public interface MyParentInterface {
void parentMethod();
}
public interface MyChildInterface extends MyParentInterface {
void childMethod();
}
public class MyClass implements MyChildInterface {
public void parentMethod() {
System.out.println("Implementation of parentMethod");
}
public void childMethod() {
System.out.println("Implementation of childMethod");
}
}
♦ Multiple Inheritance through Interfaces:
In Java, multiple inheritance is not supported through classes, but it can be achieved through interfaces. A class can implement multiple interfaces, and this allows it to inherit behavior from multiple sources. Example:
public interface MyInterface1 {
void method1(); }
public interface MyInterface2 {
void method2();
}
public class MyClass implements MyInterface1, MyInterface2 {
public void method1() {
System.out.println("Implementation of method1");
}
public void method2() {
System.out.println("Implementation of method2");
}
}
♦ Interface as Method Parameters:
An interface can also be used as a method parameter in Java. This allows us to pass objects of different classes that implement the same interface, and the method can work with all these objects polymorphically. Example:
public interface MyInterface {
void myMethod();
} public class MyClass implements MyInterface {
public void myMethod() { System.out.println("Implementation of myMethod");
}
}
public class MyOtherClass implements MyInterface {
public void myMethod() { System.out.println("Implementation of myMethod in another class");
}
}
public class MyMainClass {
public static void main(String[] args) {
MyInterface obj1 = new MyClass();
MyInterface obj2 = new MyOtherClass();
obj1.myMethod();
obj2.myMethod();
}
}
♦ Interface as Return Type:
An interface can also be used as the return type of a method in Java. This allows us to return objects of different classes that implement the same interface, and the caller of the method can work with all these objects polymorphically. Example:
public interface MyInterface {
void myMethod();
}
public class MyClass implements MyInterface {
public void myMethod() {
System.out.println("Implementation of myMethod");
}
}
public class MyOtherClass implements MyInterface {
public void myMethod() {
System.out.println("Implementation of myMethod in another class"); }
}
public class MyMainClass {
public static MyInterface getObject() {
return new MyClass();
}
public static void main(String[] args) {
MyInterface obj = getObject();
obj.myMethod();
}
}
Interfaces are useful in Java because they allow you to define a contract between different parts of a program without specifying how that contract is implemented. This makes it easier to build flexible and extensible programs that can be easily modified or extended in the future.
Thanks for reading, and happy coding!
Java Interface: Usage, Syntax and Examples. -> Java Polymorphism: Understanding Dynamic Binding and Method Overriding