CS core subjects

Stage 2 · Lesson 3 of 8

Inheritance and when not to use it

Object-oriented programming and design

lets a new class, called a subclass, reuse and extend a parent class. A SavingsAccount is an Account that also earns interest. Inheritance fits genuine "is a" relationships, and code written for the parent then works with any subclass.

It is also the most overused feature. In a deep chain of classes, each subclass depends on the inner details of its parents. A change at the top can break classes far below.

And a subclass that cannot keep every promise its parent makes causes subtle bugs. Learn this rule: a subclass must work anywhere the parent is expected. You will meet it again as the .

Common trapusing inheritance just to share code between classes that are not really the same kind of thing.

A question to check yourself, an AI practice session and 1 cheat sheet entry come with the full roadmap. Unlock

Resources for this stage

Start with· follow one of these

  • CoursePaid
    Grokking SOLID Design Principles

    Design Gurus · Beginner · $50 one-time

    Each SOLID principle with a broken example and its fix, at a beginner-friendly pace

  • CourseFree
    Java Programming MOOC

    University of Helsinki · Beginner

    Free, exercise-heavy course whose second half is a thorough, tested introduction to OOP

Keep handy

Also worth your time

Mark this stage as not done?