I N T E R F A C E (JAVA-8)
Published: 2024-02-20 07:34:30

##<u>I n t r o d u c t i o n</u>

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.

---

####<u> Why Interfaces Were Introduced in Java? </u>

Interfaces were introduced in Java to achieve : 

- abstraction

- multiple inheritance

- polymorphism 

- promoting modular, flexible, and reusable code.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u5wsihheen8u50j6pldn.jpg)


---

####<u> I N T E R F A C E </u>

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.

---

#### <u> The relationship between classes and interfaces </u>

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.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ju2ufx80b0j56x4wa3hh.jpg)

---

####<u> How to declare interface in java? </u>

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 :

####<u>S y n t a x </u>

```java
interface <interface_name>{  
        // declare constant fields  
        // declare methods that abstract   
        // by default.  
}  
```
---

####<u>Java Interface Example</u>


```java
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.

---
####<u>Implementing Interfaces</u>
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:

```java
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.

```java
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.

---

####<u>New features added in interface from java 8</u>

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.

```java 
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.

```java
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. 


####<u>3. Functional Interface</u>

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.

```java
@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.

####<u>Important points about interface</u>
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/feaqxn9w78peug1oofll.jpg)

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!