Java OOPs Concepts - Interfaces

Java OOPs Concepts - Interfaces

What's is it, how to use, exemples!

Introduction

The interface is a feature widely used in Java, as well as in most object-oriented languages, to “force” a certain group of classes to have methods or properties in common to exist in a certain context, but the methods can be implemented in each class in a different way. It can be said that an interface is a contract that, when assumed by a class, must be implemented.

Using interface in Java

Inside the interfaces there are only method and property signatures, being the class responsible for implementing the signatures, giving practical behavior to the methods.

Below you can see an example of an interface called Animal with two method signatures that will be implemented by the classes referring to Animal interface.

/ Interface
interface Animal {
  public void animalSound(); // interface method (does not have a body)
  public void sleep(); // interface method (does not have a body)
}

To make the call/reference to an interface by a certain class, it is necessary to add the keyword "implements" to the end of the signature of the class that will implement the chosen interface.

Syntax:

public class class_name implements interface_name

Below you can see a class that implements the Animal interface, called pig with two methods.

animalSound(); sleep();

// Pig "implements" the Animal interface
class Pig implements Animal {
  public void animalSound() {
    // The body of animalSound() is provided here
    System.out.println("The pig says: wee wee");
  }
  public void sleep() {
    // The body of sleep() is provided here
    System.out.println("Zzz");
  }
}

class Main {
  public static void main(String[] args) {
    Pig myPig = new Pig();  // Create a Pig object
    myPig.animalSound();
    myPig.sleep();
  }

As you can see, the class followed the Animal interface contract.

Unlike inheritance which limits a class to inheriting only one parent class at a time, it is possible for a class to implement multiple interfaces at the same time.

Imagine, for example, an interface called Vehicle and another called Engine.

Vehicle Interface

public interface Vehicle {

    public String getName();
    public String getId();
}

Engine Interface

public interface Engine {

    public String getModel();
    public String getAutomaker();
}

Next, you can see the implementation of the interfaces in a class called Car.

public class Car implements Vehicle,Engine{

    @Override
    public String getId() {

   }

    @Override
    public String getName() {

    }

    @Override
    public String getAutomaker() {

    }

    @Override
    public String getModel() {

    }

  }
}

Finally, an interface is nothing more than a kind of contract of rules that a class must follow in a given context. As in Java there is no multiple inheritance, the interface becomes an alternative.

I hope this content, although brief, can be as useful as it was for me in my studies of Java and OOP

See you!