JAVA — CHAPTER 9

Inheritance, Polymorphism & Interfaces · Concept Cheat Sheet
extends Polymorphism abstract Interfaces sealed
1 SUPERCLASSES & SUBCLASSES
public class Employee { protected String name; } public class Manager extends Employee { private double bonus; }

"Is-a" relationship

  • A Manager is an Employee, plus extra behavior/data
  • Subclass inherits accessible members of the superclass
  • Every class ultimately extends Object (directly or indirectly)
  • super(...) calls the superclass constructor
2 POLYMORPHISM
Employee[] staff = { new Manager(), new Employee() }; for (Employee e : staff) { e.earnings(); // each object runs its OWN version }

Core idea

A single method call — e.earnings() — behaves differently depending on the actual object type at runtime, not its declared reference type. This lets you write general code (loop over Employee) that automatically works correctly for every subclass.

3 abstract CLASSES & METHODS
public abstract class Shape { public abstract double area(); // no body } public class Circle extends Shape { public double area() { return Math.PI * r * r; } }

Rules

  • An abstract class cannot be instantiated directly
  • An abstract method has no body — subclasses must override it
  • Used as a common base for related classes (Payroll's Case Study: Employee → SalariedEmployee, HourlyEmployee)
4 final METHODS/CLASSES & CONSTRUCTOR CAUTIONS

final

  • A final method cannot be overridden by a subclass
  • A final class cannot be extended at all

Constructor pitfall

Avoid calling an overridable instance method from a constructor — the subclass's version may run before the subclass has finished initializing its own fields.

5 CREATING & USING INTERFACES
public interface Payable { double getPaymentAmount(); // implicitly public abstract } public class Invoice implements Payable { public double getPaymentAmount() { return total; } }

Interface features

  • A class can implement multiple interfaces (unlike single class inheritance)
  • Can include default methods (with a body) and static methods
  • All fields are implicitly public static final constants
6 "PROGRAM TO AN INTERFACE" & sealed TYPES

Program to an interface, not an implementation

Declare variables/parameters using the interface type (Payable p = new Invoice();) so code stays flexible — any class implementing Payable can be swapped in without changing the calling code.

public sealed interface Shape permits Circle, Square { } // only Circle and Square may implement Shape
7 private CONSTRUCTORS & protected MEMBERS

private constructor

Prevents a class from being instantiated from outside — common for utility classes (all-static methods) or singleton patterns.

protected members

Visible to the class itself, its subclasses (even in other packages), and other classes in the same package — a middle ground between private and public.

8 METHOD REFERENCE TABLE — Comparable & Comparator

interface Comparable<T>

MethodPurpose
compareTo(other)negative/0/positive — defines this object's natural order

Implement this to let Collections.sort() sort your own class.

interface Comparator<T>

MethodPurpose
compare(a, b)defines an external/custom ordering

Pass a Comparator when you need an ordering different from the class's natural one.