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(); }
2
Q
Why are interfaces useful?
A
3
Q
how to use interfaces in your code
A
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
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()); } }