DEV Community

Vignesh . M
Vignesh . M

Posted on

INTERFACE

What is the interface in Java?
*In Java, an interface specifies the behavior of a class by providing an abstract type. As one of Java's core concepts, abstraction, polymorphism, and multiple inheritance are supported through this technology. Interfaces are used in Java to achieve abstraction.

What is the use of interface?
**Interfaces are used to define a contract, ensuring that classes adhere to a specific set of behaviors*. They are valuable for defining APIs, simulating multiple inheritance, enabling callback mechanisms, and promoting polymorphism.

Different between Class and interface?

Image description

Different between Abstract and Interface?

Image description

Key Features
* Declaration: Interfaces are declared using the interface keyword.
* Members: Interfaces can have:
* Abstract Methods: Methods without implementation.
* Constants: Variables that are implicitly public, static, and final.
* Default Methods: Methods with a default implementation

The advantages of using
interfaces in Java are as follows:

  * Without bothering about the implementation part, we can achieve the security of the implementation.
  * In Java, multiple inheritances are not allowed, however, you can use an interface to make use of it as you can implement more than one interface
Enter fullscreen mode Exit fullscreen mode

How To Declare Interfaces?
**public interface NameOfTheinterface
{

// Any final, static fields here

// Any abstract method declaration here

}
//This is Declaration of the interface**
--------------------------

EXAMPLE PROGRAM: for Interface //(File name : car.java)

// This is Program To implement the Interface
interface car
{
void display();
}

class model implements car
{
public void display()
{
System.out.println("im a Car");
// the code output will print "im a car"
}
public static void main(String args[])
{
model obj = new model();
obj.display();
}
}

OUTPUT
im a Car

REFERRED LINKS

  1. https://www.simplilearn.com/tutorials/java-tutorial/java-interface#:~:text=In%20Java%2C%20an%20interface%20specifies,in%20Java%20to%20achieve%20abstraction.
  2. https://www.geekster.in/articles/interfaces-in-java/
  3. https://www.geekster.in/articles/interfaces-in-java/

Top comments (0)