Abstract classes and interfaces, step by step

Problem 1

new Vehicle("Ford")

It compiles, but a “generic vehicle” does not exist

Problem 2

Vehicle.start()

A body invented just so it compiles

Abstract class

An incomplete mold

Brings state and solved behavior, and leaves gaps

Interface

A pure contract

Says what whoever signs it must be able to do

State

protected final String name;

Fields and a constructor, like any class

Gap

public abstract double area();

No body: the subclass must write it

Solved

public void describe()

Has a body: inherited as is

Cannot be instantiated · a class extends ONE only

Constant

int MAX_LAYERS = 10;

public static final

Abstract

void draw();

Public and abstract by default

default

default void highlight()

Inheritable implementation (Java 8+)

static

static Drawable empty()

A utility of the interface itself

No instance state · implement as many as you want

Before Java 8

Adding a method broke everything

The thousand classes already implementing it stopped compiling

default

A method with a body

Classes inherit it and may override it or not

Sparingly

Not a disguised abstract class

default is for evolving a contract, not for writing logic

static

Payable.isValidAmount(500)

Not inherited: called through the interface

Adapter class

WindowListener → WindowAdapter

Seven empty methods already written: you override just one

extends

Bird

A single line of inheritance

implements

Swimmer, Flyer

As many contracts as needed

The same object, several angles

Bird b = duck Swimmer s = duck Flyer f = duck
Criterion Abstract class Interface

Relationship it expresses

“is a”: shared identity

“is capable of”: shared capability

How many you can use

Only one (extends)

As many as you want (implements)

Instance state

Yes, ordinary fields

No, only static final constants

Constructors

Yes

No

Method visibility

Anything, including protected

Always public

Adding a method later

Breaks subclasses if abstract

Breaks nothing if default

Interface by default. Abstract class only with shared state or code.

namespace

A container that gives every type a unique context

Stops two classes with the same name from competing in one global scope

1 package

package com.facundouferer.shop;

Defines the namespace of the whole file

2 FQCN

com.facundouferer.shop.domain.Product

The real identity of the class for the JVM

3 import

import ...shop.domain.Product;

A lexical shortcut: it loads no bytecode

Reversed domain: com.facundoufererAll lowercaseOne .java per public classGroup by responsibilityStatic imports, sparinglypackage-private: a trust boundary

Association

Teacher → Course

They know each other, neither owns the other

Aggregation

Team ◇ Player

Receives the parts from outside

Composition

House ◆ Room

Creates the parts and does not expose them

Abstract class where an interface belongsA default on every methodMutable fields in an interfacePackage that does not match the folderName collision when importingAggregation that was compositionOne giant package

1 Step

Payable

pay · isAvailable · default payIfPossible

2 Step

CreditCard · DigitalWallet

implements Payable

3 Step

DigitalPaymentMethod

Abstract class: email + active

4 Step

DigitalWallet extends

Reuses super.isAvailable()

5 Step

Payable[]

One loop, no instanceof and no casting

1 · Abstract class: incomplete mold2 · Interface: pure contract3 · Interface first4 · default: evolve without breaking5 · Extends one, implements many6 · package = namespace · FQCN7 · If I destroy the whole, does the part exist?