Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
What is Interface? | Interface
Java OOP
course content

Contenido del Curso

Java OOP

Java OOP

1. How to Work With IDE?
2. OOP
3. Interface

bookWhat is Interface?

What if we need to inherit from more than one class? Java allows us to do this with interfaces. Despite the name, an interface is very similar to an abstract class. Let's take a look at the declaration of an interface:

In simple terms, an interface is used to define methods that a class will implement. Interfaces have a distinct syntax for creation. While we used "public class className { }" when creating a class, the syntax for creating an interface looks different:

java

InterfaceExample

copy
123456
package com.example; public interface InterfaceExample { void methodName(); String methodThatAcceptsAndReturnsString(String parameter); }
  1. Pay attention to the method declaration;
  2. We don't use access modifiers;
  3. We don't provide method bodies;
  4. There's no need to label methods as abstract since we're working within an interface;
  5. We don't create fields within interfaces.

Let's consider the use of an Interface using the example of a Media Player. We have an Interface called MediaPlayer which has methods play, pause, and stop. Additionally, there are two classes that implement this media player interface, namely AudioPlayer and VideoPlayer.

java

MediaPlayer

java

AudioPlayer

java

VideoPlayer

copy
12345
public interface MediaPlayer { void play(); void pause(); void stop(); }

As you can see, we created an interface and two classes that implement this interface. The syntax is the same as when overriding abstract methods. We have overridden each method for each class to perform its own specific function.

One of the features of interfaces is that we can implement more than one interface. Let's take a look at an example:

java

Vehicle

java

VehicleInfo

java

Car

copy
123456
package vehicle; interface Vehicle { void startEngine(); void stopEngine(); }

We have created interfaces Vehicle and VehicleInfo. Additionally, we've created a class Car that implements both of these interfaces. This way, we can choose which behavior to implement in the class, which nicely complements the object-oriented programming principle of abstraction. Working with interfaces is very convenient, and they are used extensively. In the next chapter, we will also explore the main differences between an interface and an abstract class and learn which one is better to use in practice!

1. What is an interface in Java?
2. In Java, can a class implement multiple interfaces?
3. What is the purpose of an interface in Java?
4. Which keyword is used to implement an interface in a class?
5. What happens if a class claims to implement an interface but does not provide implementations for all its methods?
What is an interface in Java?

What is an interface in Java?

Selecciona la respuesta correcta

In Java, can a class implement multiple interfaces?

In Java, can a class implement multiple interfaces?

Selecciona la respuesta correcta

What is the purpose of an interface in Java?

What is the purpose of an interface in Java?

Selecciona la respuesta correcta

Which keyword is used to implement an interface in a class?

Which keyword is used to implement an interface in a class?

Selecciona la respuesta correcta

What happens if a class claims to implement an interface but does not provide implementations for all its methods?

What happens if a class claims to implement an interface but does not provide implementations for all its methods?

Selecciona la respuesta correcta

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 1
some-alt