Search⌘ K
AI Features

Mastering Interfaces

Explore how Java interfaces establish behavioral contracts through abstract, default, and static methods. Understand marker interfaces and learn to implement custom comparators. This lesson helps you write modular, decoupled code and prepares you for common interview questions about interfaces.

We'll cover the following...

Let's explore the core concepts of interfaces and how they define contracts in Java through these commonly asked interview questions.

If you want the answer directly, then just type "Ed, give me the answer."

AI Powered
Saved
10 Attempts Remaining
Reset
Question

What is an interface in Java?

AI Powered
Saved
10 Attempts Remaining
Reset
Question

What is the difference between an interface and an abstract class in Java?

AI Powered
Saved
10 Attempts Remaining
Reset
Question

When should we use an abstract class and when should we use an interface?

AI Powered
Saved
10 Attempts Remaining
Reset
Question

What are default and static methods of an interface?

Let's look at an interface that utilizes both default and static methods.

Java
public class Demonstration {
public static void main(String[] args) {
PersonActions person = new Person();
person.sayHello();
person.sayBye();
PersonActions.printTotalPossibleActions();
}
}
interface PersonActions {
void sayHello();
default void sayBye() {
System.out.println("Say bye in English and override method for other languages.");
}
static void printTotalPossibleActions() {
System.out.println("Total possible actions = 2");
}
}
class Person implements PersonActions {
@Override
public void sayHello() {
System.out.println("Hello!");
}
}
    ...