← Introduction to Computer Science

Unit 12: Object-Oriented Programming

Bundling data with the behavior that acts on it, and the four ideas that make it scale: encapsulation, inheritance, polymorphism, composition.

8 topics · 28 questions

Each topic is a path, not a menu: work down it in order and every question builds on the one above. Click any of them to watch the answer draw itself.

Classes and Objects: Blueprint Versus Instance

  1. How it worksHow does a class act as a blueprint while each object built from it holds its own separate data?
  2. Worked exampleTrace class Dog: def __init__(self, name): self.name = name, then d1 = Dog('Rex') and d2 = Dog('Fido').
  3. Common mistakeWhy doesn't changing d1's name field ever change d2's name field?
  4. Going furtherWhat happens once a third object, d3 = Dog('Zoe'), joins d1 and d2?
Your turn1 of 2
A class Dog is used to create 697 separate dog objects, each with its own independent name field. How many separate name fields exist in total across all n objects?

Constructors and Instance State

  1. How it worksHow does __init__ run automatically the instant an object is created, filling in its starting fields?
  2. Worked exampleTrace class Account: def __init__(self, balance): self.balance = balance, called as Account(100).
  3. Going furtherWhat happens when a class defines two fields in __init__ but a caller only supplies one argument?
Your turn1 of 2
class Account has __init__(self, balance) which sets self.balance = balance. It is called as Account(699) for 13 separate accounts, each with the same starting balance of n. What is the combined starting balance across all k accounts?

Encapsulation: Hiding Data Behind Methods

  1. How it worksHow does marking a field private and exposing a deposit method stop outside code from corrupting it?
  2. Worked exampleTrace calling account.withdraw(50), which checks the balance internally, versus setting account.balance directly.
  3. Common mistakeWhy isn't encapsulation just about making a program's code shorter to read?
Your turn1 of 2
An account object exposes a withdraw() method instead of letting outside code set the balance field directly. Out of 142 withdrawal attempts through the method, 6 are rejected because they exceed the balance. How many of the n attempts actually succeed?

Inheritance: Sharing Behavior Across a Class Hierarchy

  1. How it worksHow does a subclass automatically gain every method and field its parent class already defines?
  2. Worked exampleTrace class Animal: def speak(self): return 'sound', then class Dog(Animal): pass, then Dog().speak().
  3. Common mistakeWhy doesn't a subclass need to redefine every method its parent class already has?
Your turn1 of 2
A parent class Animal defines 14 methods. A subclass Dog inherits all of them automatically and adds 13 new methods of its own. How many total methods can a Dog object call?

Method Overriding

  1. How it worksHow does defining speak again inside Dog replace the version it would have inherited from Animal?
  2. Worked exampleTrace class Animal: def speak(self): return 'sound', class Dog(Animal): def speak(self): return 'woof', then Dog().speak().
  3. Going furtherWhat happens when an overriding method calls super().speak() before adding its own behavior?
Your turn1 of 2
class Dog(Animal) overrides speak(). A loop creates 70 separate Dog objects and calls speak() on each one. How many of the n calls use Dog's overriding version instead of Animal's original?

Polymorphism: Same Call, Different Behavior

  1. How it worksHow does calling the same method name on different subclass objects run each one's own overridden version?
  2. Worked exampleTrace looping over [Dog(), Cat(), Cow()] calling speak() on each and printing whatever each one returns.
  3. Common mistakeWhy isn't polymorphism the same thing as one function having several different names?
  4. Ties togetherHow does polymorphism let a sorting function work on any object as long as it defines a comparison method?
Your turn1 of 2
A list of 140 objects, a mix of Dog, Cat, and Cow instances, each has speak() called on it in a loop. How many total speak() calls does the loop make?

Composition Versus Inheritance

  1. How it worksHow does composition build a class out of other objects as fields instead of extending a parent class?
  2. Worked exampleTrace class Car: def __init__(self): self.engine = Engine(), then car.engine.start().
  3. Common mistakeWhy isn't every relationship between two classes best modeled as inheritance?
  4. Going furtherWhat changes when a Car needs to swap out its Engine for a different kind at runtime?
Your turn1 of 2
A Car class holds 3 Engine objects and 6 Wheel objects as fields, using composition. How many total component objects does one Car object hold?

Abstract Classes and Abstract Data Types

  1. How it worksHow does an abstract class declare a method every subclass must implement without providing a body itself?
  2. Worked exampleTrace class Shape: def area(self): raise NotImplementedError, then class Circle(Shape): def area(self): return 3.14·r·r.
  3. Common mistakeWhy isn't an abstract data type the same thing as a specific class written in one particular language?
  4. Ties togetherHow does an abstract data type like a stack describe behavior separately from any one array or linked-list build?
Your turn1 of 2
An abstract class Shape declares 7 methods every subclass must implement, providing no bodies itself. A subclass Circle implements all n of them. How many methods does Circle need to define to satisfy the abstract class?