Interface:

In Java, an interface is a reference type that defines a contract or a set of abstract methods that a class can implement. It provides a way to achieve 100% abstraction.

By using an interface we can achieve multiple inheritance.

Start by declaring the interface using the interface keyword, followed by the interface name. 

By convention, interface names are typically capitalized and should represent the behavior or functionality being defined. 

For example:

public interface <InterFace Name>

{

//code

}

1. Inside the interface, you can declare abstract method signatures that define the behavior or functionality that implementing classes must provide. 

2. Abstract methods do not have a body. They only specify the method name, return type (if applicable), and parameter list.

 Here's an example of an interface with two abstract methods:

java
public interface MyInterface {
void method1()
int method2(String str)
}
  1. Optionally, you can also define constants and default methods within the interface.
  2. Constants are implicitly public, static, and final, and they can be accessed using the interface name.
  3. Default methods provide a default implementation within the interface itself.
  4. Here's an example:
java
public interface MyInterface {
int MY_CONSTANT = 100; // Constant declaration 
void method1(); // Abstract method 
default void defaultMethod() {
// Default method implementation
}
 }

That's it! You have created an interface in Java. Interfaces define a contract that classes can implement, providing a way to achieve abstraction and establish common behavior among multiple classes.

A class can implement an interface using the implements keyword. It must provide implementations for all the abstract methods declared in the interface.

Here's an example of how to define an interface in Java:

java
public interface MyInterface
// Abstract method declarations 
void method1()
int method2(String str)
}

Here's an example of a class implementing the MyInterface interface:


java
public class MyClass implements MyInterface {
// Implementing the abstract methods 
public void method1() {
// Implementation code here 
 } 
public int method2(String str) {
// Implementation code here 
return 0; }
 }

The class MyClass implements MyInterface by providing implementations for method1() and method2(). It must adhere to the method signatures defined in the interface.