Inheritance vs. Association

Inheritance can be thought of as an is-a relationship. For example, a dictionary is a book.

class Book
end

class Dictionary < Book
end

Association can be thought of as a has-a relationship. For example, a library has books, so there is an associative relationship between objects of class Library and objects of class Book.

class Library
  def initialize
    @book = Book.new
  end
end

class Book
end

See also Collaborator Objects in What is Object Oriented Programming.

Links to this page