Interface Flashcards

1
Q

What are interfaces?

A

An interface is a contract or blueprint for a class.

Interfaces define what a class does, but not how it does it.

public interface Playable {
    void play();
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Why are interfaces useful?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

how to use interfaces in your code

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
public class Video implements Playable {
    @Override
		    public void play() {
				    //play the video
				}
}
				
public class Audio implements Playable {
			@Override
			public void play() {
			    // play the audio
			 }
}
A

Classes implement interfaces to provide the how

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What’s the point of interfaces?

A

Interfaces allow us to write code that is agnostic to the implementation

public class Player{
		    public void play(Playable playable) {
				    playable.play();
				}
}
				
public class Main {
			public static void main(String[] args) {
			    Player player = new Player();
					player.play(new Video());
					player.play(new Audio());
			 }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly