M7 — Inheritance and Refactoring

SENG 365 — Software Engineering

Neil Ernst

University of Victoria

2026-07-24

Learning Objectives

Learning Objectives

Reading: Robillard, 3rd ed. — Ch. 7, Inheritance (pp. 157–191). (Code smells and refactoring are supplementary.)

  • Inheritance and where to use it
  • Types and subtypes
  • Cautions about inheritance
  • Template Method pattern as an inheritance example
  • Code smells and refactoring

Inheritance

Inheritance in Java

  • Basic mechanics: implements vs. extends.
  • Why: remove duplicate code (reuse) and/or improve modelling (classification hierarchy).
  • Inheritance of semantics vs. inheritance of mechanics.
class Stack extends ArrayList {
    public void push(Object value) {}
    public Object pop() {}
}
  • Compile-time types vs. runtime types; downcasting.

What is Inherited?

  • Fields: all fields are inherited, even if not visible (access is a static concept).
  • Employee alice = new Manager();
    • Runtime type Manager, compile-time type Employee.
    • Can’t call Manager methods on alice without a cast, but overridden methods dispatch to Manager.
  • final class: cannot be extended. super(). Static vs. instance methods.

Key Principles

  • Liskov Substitution: you can use the subtype anywhere you use the supertype.
  • Open–Closed: open for extension, closed for modification — a variant of encapsulation.
  • Default methods vs. abstract methods in interfaces.
  • Abstract classes: some classes make no sense to instantiate but provide skeleton implementations.
  • Dynamic dispatch: the most specific runtime type handles the message.

Inheritance vs. Composition

Prefer Composition

  • The claimed benefit of inheritance is reducing duplicate code — but do we need Shape/Rectangle examples?
  • Prefer composition (“has-a”) to inheritance (“is-a”).
  • Composition lets you “uncompose” or change parts; inheritance is inflexible.
  • With interfaces, change behaviour at runtime.
  • Composition favours run-time flexibility; inheritance favours compile-time configuration.

Inheritance Caveats

  • Violates encapsulation: depends on the superclass implementation.
  • Don’t inherit across package boundaries or classes you/your team don’t own.
  • Any non-final class can be extended, introducing dependencies.
  • Misuses: semantic relations (Stack vs. ArrayList), fragile base class, cross-domain relationships.

Template Method

  • Abstract “up” a common algorithm pattern; subclasses fill in the details.
  • Compare with sort in Java Collections: implement Comparable to “fill in the details.”
  • Video 7-2: implementing a template method.

Refactoring

Code Smells and Refactoring

Books to read:

Smells and Refactorings

  • Smells: Long Method, Shotgun Surgery, Feature Envy, Long Parameter List
  • Refactorings: Rename, Extract Method, Introduce Parameter Object
  • Heuristics in software development — practice recognizing them.

In-Class Activities

Day 1

  • Group discussion

Day 2 — Code Review

Day 3

  • Office hours