Flashcards
Tap a card to flip it
20 flashcards in this kit — the app reviews them with
spaced repetition so the right card returns on the right day.
Question
What is an **interface** in Java?
tap to flip ↻
Answer
A blueprint of methods that a class promises to implement, defining *what* a class can do, not *how*.
Question
What do interfaces declare?
tap to flip ↻
Answer
Method **signatures** (names, parameters, return types) without bodies (prior to Java 8+).
Question
Which keyword is used for a class to adopt an interface?
tap to flip ↻
Answer
`implements`
Question
What must a class do when it implements an interface?
tap to flip ↻
Answer
Provide bodies for all its abstract methods.
Question
Can a Java class implement multiple interfaces?
tap to flip ↻
Answer
Yes, a class can implement multiple interfaces.
Question
How does implementing multiple interfaces relate to multiple inheritance in Java?
tap to flip ↻
Answer
It allows Java to achieve a form of multiple inheritance for behavior, as Java classes cannot extend multiple classes.
Question
What are interface variables implicitly?
tap to flip ↻
Answer
`public static final` (constants).
Question
What are interface methods implicitly (before Java 8)?
tap to flip ↻
Answer
`public abstract`.
Question
What types of methods can interfaces have since Java 8?
tap to flip ↻
Answer
`default` and `static` methods with bodies.
Question
What types of methods can interfaces have since Java 9?
tap to flip ↻
Answer
`private` helper methods, in addition to `default` and `static`.
Question
Can you instantiate an interface directly?
tap to flip ↻
Answer
No, you cannot. You create objects of classes that implement the interface.
Question
In the example `interface Animal { void sound(); }`, what kind of method is `sound()`?
tap to flip ↻
Answer
An abstract method.
Question
In the example `interface Animal { default void sleep() { ... } }`, what kind of method is `sleep()`?
tap to flip ↻
Answer
A default method (introduced in Java 8+).
Question
What does `Animal a = new Dog();` demonstrate?
tap to flip ↻
Answer
**Polymorphism**, where the variable type is the interface and the object is the concrete class.
Question
Can an interface have constructors?
tap to flip ↻
Answer
No, interfaces cannot have constructors.
Question
Can an abstract class have constructors?
tap to flip ↻
Answer
Yes, abstract classes can have constructors.
Question
What kind of fields can an interface have?
tap to flip ↻
Answer
Only constants (`public static final`).
Question
What kind of fields can an abstract class have?
tap to flip ↻
Answer
Any type of fields (constants, instance variables, etc.).
Question
When should you generally use an interface over an abstract class?
tap to flip ↻
Answer
When defining a shared capability or contract that multiple unrelated classes might implement.
Question
When should you generally use an abstract class over an interface?
tap to flip ↻
Answer
When defining a shared partial implementation for related classes that share a common base type.