java
  1. java-interface

Java Interface

In Java, an interface is a collection of abstract methods that are used to define a set of behaviors that a class should implement. An interface can be used to achieve abstraction and multiple inheritances in Java. In this article, we'll take a look at how to create and use interfaces in Java.

Syntax

To create an interface in Java, use the following syntax:

interface InterfaceName {
    // abstract methods
}

To implement an interface in a class, use the following syntax:

class ClassName implements InterfaceName {
    // methods and fields
}

Example

Let's take a look at a simple example of an interface. Suppose we have an interface called Animal that defines a method called makeSound().

interface Animal {
    void makeSound();
}

Now, suppose we have a class called Dog that implements the Animal interface and implements the makeSound() method.

class Dog implements Animal {
    public void makeSound() {
        System.out.println("Woof!");
    }
}

Output

If we create an instance of the Dog class and call the makeSound() method, we get the following output:

Woof!

Explanation

In the example above, the Animal interface defines a single abstract method called makeSound(). This means that any class that implements the Animal interface must implement this method.

The Dog class implements the Animal interface and overrides the makeSound() method with its own implementation. When we create an instance of the Dog class and call the makeSound() method, it prints "Woof!" to the console.

This example demonstrates how we can use interfaces to achieve abstraction and polymorphism in Java.

Use

Interfaces are commonly used in Java to achieve abstraction and multiple inheritances. By using interfaces, we can define a set of behaviors that a class should implement without specifying how those behaviors should be implemented. This allows us to write flexible and reusable code.

Interfaces are also useful for defining callback methods in Java. For example, we can define an interface that has a method called onResponse() and pass an instance of this interface to an API call. When the API call completes, it can call the onResponse() method on the interface to pass the result back to the calling code.

Important Points

  • An interface is a collection of abstract methods that a class can implement.
  • A class can implement multiple interfaces in Java.
  • An interface cannot be instantiated on its own; it must be implemented by a class.
  • All methods in an interface are implicitly public and abstract.
  • A class that implements an interface must implement all of its methods.

Summary

Interfaces are a powerful feature of Java that allow us to achieve abstraction and multiple inheritances. They are commonly used to define a set of behaviors that a class should implement, without specifying how those behaviors should be implemented. By using interfaces, we can write flexible and reusable code that is easy to maintain and extend.

Published on: