Skip to content

Chapter 13 of 37

Abstract Classes and Interfaces

Design extensible contracts with abstract, default, sealed, and implemented behavior.

38 minutes 10 quick checksBy Subha Prasad
Lesson 13 of 37Course navigation

Lesson content

Read, practise, then check your understanding

An abstract class cannot be instantiated and can mix fields, constructors, concrete methods, and abstract methods. An interface primarily specifies a role; a class may implement many interfaces.

Contracts

interface Identified {
    String id();
    default boolean hasId() { return !id().isBlank(); }
}

abstract class Entity implements Identified {
    private final String id;
    protected Entity(String id) { this.id = id; }
    @Override public final String id() { return id; }
}

Interface fields are implicitly public static final; abstract interface methods are public. Default methods evolve interfaces but conflicting inherited defaults must be resolved explicitly. Private interface methods share implementation internally. Choose an abstract class for shared state and controlled construction, an interface for an independently implementable capability, and composition for pluggable behavior. Sealed classes/interfaces list permitted direct subtypes, enabling controlled domain models and more exhaustive pattern handling.

Knowledge check

Answer every question correctly to complete this chapter.

Which statement best describes abstract class?
Which Java term matches this description: A non-instantiable class that may combine state and implemented behavior.
Which statement best describes abstract method?
Which Java term matches this description: A method declaration whose implementation must come from a concrete subtype.
Which statement best describes interface?
Which Java term matches this description: A contract supporting multiple type inheritance.
Which statement best describes default method?
Which Java term matches this description: An interface instance method with an implementation.
Which statement best describes sealed type?
Which Java term matches this description: A type that explicitly restricts permitted direct subtypes.

0 of 10 checks passed

Your progress is saved on this device.