Skip to main content
Java Basics
CHAPTER 18 Beginner

Interfaces and Abstract Classes

Updated: May 17, 2026
5 min read

# CHAPTER 18

Interfaces and Abstract Classes

1. Introduction

Interfaces define a contract — a set of method signatures that a class promises to implement. They are Java's answer to multiple inheritance, allowing a class to implement multiple interfaces while avoiding the diamond problem.

2. Defining and Implementing Interfaces

java
123456789101112131415
interface Flyable {
    void fly();        // Abstract by default
    int MAX_ALTITUDE = 40000; // public static final by default
}

interface Swimmable {
    void swim();
}

class Duck implements Flyable, Swimmable {
    @Override
    public void fly() { System.out.println("Duck is flying!"); }
    @Override
    public void swim() { System.out.println("Duck is swimming!"); }
}

3. Default Methods (Java 8+)

Interfaces can have methods with implementations:
java
123456
interface Logger {
    void log(String message);
    default void logError(String error) {
        System.err.println("ERROR: " + error);
    }
}

4. Static Methods in Interfaces

java
1234
interface MathUtils {
    static int square(int n) { return n * n; }
}
MathUtils.square(5); // 25

5. Functional Interfaces (Java 8+)

An interface with exactly ONE abstract method — usable with Lambda expressions:
java
1234567
@FunctionalInterface
interface Greeting {
    void sayHello(String name);
}

Greeting g = (name) -> System.out.println("Hello, " + name);
g.sayHello("Alice");

6. Interface vs Abstract Class

FeatureInterfaceAbstract Class
MethodsAbstract + default/staticAbstract + concrete
Variablespublic static final onlyAny type
ConstructorNoYes
Multiple inheritanceYesNo
extends or implementsimplementsextends

7. MCQ Quiz with Answers

Question 1

Which keyword implements an interface?

Question 2

Can a class implement multiple interfaces?

Question 3

Interface variables are implicitly:

Question 4

A functional interface has:

Question 5

Can interfaces have constructors?

Question 6

Default methods were added in Java:

Question 7

An interface method is implicitly:

Question 8

Can an abstract class implement an interface?

Question 9

@FunctionalInterface is:

Question 10

Which allows multiple inheritance in Java?

8. Interview Questions

  • Q: When would you use an interface vs an abstract class?
  • Q: What is a marker interface (e.g., Serializable)?
  • Q: Can an interface extend another interface?
  • Q: What are default methods and why were they added in Java 8?

9. Summary

Interfaces define contracts with abstract methods. Java 8 added default and static methods. Functional interfaces enable lambda expressions. A class can implement multiple interfaces, solving Java's multiple inheritance limitation. Choose interfaces for "can-do" relationships and abstract classes for "is-a" relationships.

10. Next Chapter Recommendation

In Chapter 19: Exception Handling, we'll learn how to gracefully handle errors using try-catch, finally, throw, and custom exceptions.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·