I n t r o d u c t i o n
Java interfaces are a fundamental concept in the world of Java programming.We'll start by understanding what interfaces are? and then explore their syntax, usage and the various features of interfaces.
Additionally, we'll talk about the evolution of interfaces in Java. It will cover how interfaces have developed and improved over the years, starting from their basic form in the earlier versions of Java to the enhancements and changes introduced in Java 8.
Why Interfaces Were Introduced in Java?
Interfaces were introduced in Java to achieve :
abstraction
multiple inheritance
polymorphism
promoting modular, flexible, and reusable code.
I N T E R F A C E
In Java, an interface is a blueprint of a class and is a key mechanism for achieving abstraction, that can contain only constants, method signatures, default methods, static methods, and nested types. However, it cannot contain method implementations. Instead, classes that implement interfaces
must have to provide the method implementations.
Java Interface also represents the IS-A relationship.
The relationship between classes and interfaces
A class can extend another class similar to this an interface can extend another interface using extends
keyword and a class can implement another interface using implements
keyword but vice-versa is not allowed.
How to declare interface in java?
Use the interface
keyword followed by the name of the interface.
Inside the interface, define method signatures. These are declarations of methods with the empty body.
By default, methods in interfaces are abstract and all the fields are public, static and final.
A class that implements an interface must implement all the methods declared in the interface using implements
keyword.
use the following syntax to define interface in java :
S y n t a x
interface <interface_name>{
// declare constant fields
// declare methods that abstract
// by default.
}
Java Interface Example
public interface play {
final int a = 10; // public, static and final
void cricket(); // Abstract method
void football(); // Abstract method
}
In this example, The play interface declares a constant 'a', which is public, static, and final, along with abstract methods cricket() and football().
Any class that implements the play interface must provide implementations for these methods.
Implementing Interfaces
To implement the play interface, you'll need to create a class that implements it and provide concrete implementations for the abstract methods.
A class uses the implements
keyword to implement an interface . Let's say we have a class named "student" that implements the "play" interface:
public class student implements play {
public void cricket() {
System.out.println("student is playing cricket");
}
public void football() {
System.out.println("student is playing football");
}
}
By implementing the "play" interface, the "student" class must have to provide implementations for the cricket() and football() methods.
In Java, you cannot instantiate an interface directly; instead, you create instances of classes that implement the interface, allowing you to create references of the interface type and assign them to objects of implementing classes.
public class Main {
public static void main(String[] args) {
play student = new student(); // Instantiating the student class
student.cricket(); // Output: student is playing cricket
student.football(); // Output: student is playing football
}
}
In the above example, student is a reference variable of type play, which is assigned an object of type student. Through this reference variable, you can call the cricket() and football() methods defined in the play interface, and the implementations provided in the student class will be executed.
New features added in interface from java 8
Java 8 introduced default and static methods as well as functional interfaces to enhance the versatility and power of interfaces.
1. Default Methods :
Interfaces can now have default methods, which provide a default implementation for a method. Default methods are declared using the default
keyword and can be overridden by implementing classes if needed. This allows interfaces to evolve without breaking existing implementations.
public interface play {
final int a = 10; // public, static and final
void cricket(); // Abstract method
void football(); // Abstract method
// Default method
default void hockey() {
System.out.println(" student playing hockey");
}
}
In the above modification, I've added a default method named hockey to the play interface, which prints "student playing hockey" when invoked.
2. Satic Methods :
Java 8 introduced static methods in interfaces. These methods are declared using the static
keyword and can be called directly on the interface, without requiring an instance of a class. Static methods in interfaces provide utility methods that are closely related to the interface's purpose.
public interface play {
final int a = 10; // public, static and final
void cricket(); // Abstract method
void football(); // Abstract method
// Static method
static void tennis() {
System.out.println("student playing tennis");
}
}
In the above modification, I've added a static method named tennis to the play interface, which prints "student playing tennis" when invoked.
3. Functional Interface
Java 8 introduced the concept of functional interfaces, which are interfaces that contain exactly one abstract method and may contain multiple default or static methods.
Functional interfaces enable the use of lambda expressions, providing a concise way to express instances of single-method interfaces. The @FunctionalInterface annotation was introduced to denote such interfaces, allowing the compiler to enforce the single abstract method requirement.
@FunctionalInterface
public interface Play {
// Abstract methods
void playGame();
}
Built-in Java Functional Interfaces
Runnable - This interface only contains the run() method.
Comparable - This interface only contains the compareTo()
method.
ActionListener – This interface only contains the actionPerformed() method.
Callable – This interface only contains the call() method.
Important points about interface
Thank you for taking the time to engage with my document! i hope that this overview has provided you with a clearer understanding of Java interfaces.
In our next blog post, we'll explore another method of achieving abstraction in Java: abstract classes. We'll also examine how abstract classes differ from interfaces, exploring their distinct approaches to abstraction. Stay tuned to enhance your understanding of these fundamental concepts of Java!
Top comments (0)