Connecting objects

Ways we can connect objects

  • Instance variables
  • Collections of other classes (e.g. List and Map)
  • Arguments to methods
  • Return values
  • Implementing an interface
  • Inheritance (to be discussed later)

Is-a vs has-a

Classes can have two main relationships to each other: is-a and has-a.

A Car is a Vehicle

A Car has a SteeringWheel

When dealing with interfaces (and, later, inheritance), we should only implement an interface if it makes sense to say the class is an example of the interface.

Liskov substitution principle

Defined by Barbara Liskov, MIT professor and 2008 Turing Award Winner.

Subtype Requirement: Let ⁠\(ϕ(x)\)⁠ be a property provable about objects ⁠\(x\) of type \(T\). Then ⁠\(ϕ(y)\) should be true for objects ⁠\(y\) of type \(S\) where \(S\) is a subtype of \(T\).

Huh?

In terms of classes that implement interfaces:

While different classes have different behavior (that’s the whole point), classes that implement the same interface should be enough the same that you can substitute an instance of one class that implements an interface for an instance of another class that also implements the interface anywhere the interface is called for.

Liskov cont’

Exactly what the same means is a big part of defining your abstraction.

This is also applied to inheritance which we’ll talk about later.