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

What is Interface?What 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:

An interface in Java is like a blueprint for a class. It defines a set of methods that a class must implement. Think of it as a contract that a class agrees to follow. When a class implements an interface, it promises to provide specific functionality that the interface outlines. This allows different classes to share common methods without being tightly connected in terms of inheritance.

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.java

  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

java

AudioPlayer.java

java

VideoPlayer.java

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

java

VehicleInfo.java

java

Car.java

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?

Select the correct answer

In Java, can a class implement multiple interfaces?

Select the correct answer

What is the purpose of an interface in Java?

Select the correct answer

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

Select the correct answer

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

Select the correct answer

Everything was clear?

Section 3. Chapter 1
course content

Course Content

Java OOP

What is Interface?What 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:

An interface in Java is like a blueprint for a class. It defines a set of methods that a class must implement. Think of it as a contract that a class agrees to follow. When a class implements an interface, it promises to provide specific functionality that the interface outlines. This allows different classes to share common methods without being tightly connected in terms of inheritance.

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.java

  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

java

AudioPlayer.java

java

VideoPlayer.java

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

java

VehicleInfo.java

java

Car.java

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?

Select the correct answer

In Java, can a class implement multiple interfaces?

Select the correct answer

What is the purpose of an interface in Java?

Select the correct answer

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

Select the correct answer

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

Select the correct answer

Everything was clear?

Section 3. Chapter 1
some-alt