Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen What is Interface? | Section
Java Object-Oriented Programming (OOP) Fundamentals

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:

InterfaceExample.java

InterfaceExample.java

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.

MediaPlayer.java

MediaPlayer.java

AudioPlayer.java

AudioPlayer.java

VideoPlayer.java

VideoPlayer.java

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:

Vehicle.java

Vehicle.java

VehicleInfo.java

VehicleInfo.java

Car.java

Car.java

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?

question mark

What is an interface in Java?

Select the correct answer

question mark

In Java, can a class implement multiple interfaces?

Select the correct answer

question mark

What is the purpose of an interface in Java?

Select the correct answer

question mark

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

Select the correct answer

question mark

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

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 24

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

bookWhat is Interface?

Swipe um das Menü anzuzeigen

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:

InterfaceExample.java

InterfaceExample.java

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.

MediaPlayer.java

MediaPlayer.java

AudioPlayer.java

AudioPlayer.java

VideoPlayer.java

VideoPlayer.java

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:

Vehicle.java

Vehicle.java

VehicleInfo.java

VehicleInfo.java

Car.java

Car.java

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?

question mark

What is an interface in Java?

Select the correct answer

question mark

In Java, can a class implement multiple interfaces?

Select the correct answer

question mark

What is the purpose of an interface in Java?

Select the correct answer

question mark

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

Select the correct answer

question mark

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

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 24
some-alt