An Interface in Java is a formal reference type that defines a strict contract of operational capabilities. It specifies what an implementing class must do, but provides no instance state fields.
public static final (constants).public abstract.default keyword providing default fallback implementations.InterfaceName.method()).interface AudioPlayer {
void playAudio(String filename);
}
interface VideoPlayer {
void playVideo(String filename);
}
// MediaCenter implements MULTIPLE interfaces
public class ModernMediaCenter implements AudioPlayer, VideoPlayer {
@Override
public void playAudio(String filename) {
System.out.println("Playing MP3 Audio: " + filename);
}
@Override
public void playVideo(String filename) {
System.out.println("Playing 4K Video: " + filename);
}
public static void main(String[] args) {
ModernMediaCenter mc = new ModernMediaCenter();
mc.playAudio("song.mp3");
mc.playVideo("movie.mp4");
}
}
| Parameter | Abstract Class | Interface |
|---|---|---|
| Inheritance | Single inheritance (extends OneClass) |
Multiple inheritance (implements A, B, C) |
| State / Fields | Can have instance variables, static variables, private state | Only public static final constants |
| Constructors | Yes (invoked via super()) |
No constructors allowed |
| Speed | Slightly faster method dispatch | Requires interface method table search |
Exam Question: "What is a Marker Interface in Java?"
Answer: A Marker Interface (or Tagging Interface) is an interface that contains zero methods and zero constants (e.g. java.io.Serializable, java.lang.Cloneable). Implementing a marker interface signals to the JVM runtime or compiler that the class possesses special capabilities.
Now let us explore Java Packages, directory mapping, and the Java 9+ Platform Module System (JPMS). Proceed to: Java Packages & Modules (JPMS) →
Accelerate your Java mastery by studying these tightly connected core tutorials:
Detailed comparison.
Single abstract method interfaces.
Interface-based dynamic dispatch.
Organizing interfaces into packages.