Classes and Objects


Thinking of objects

  • An Object:
    • Is a person, place, or thing, or concept (a noun)
    • Has permanance and identity
    • Well defined boundaries and meaning
    • Can be simple or complex
    • Can be real - a car, a chair
    • ...or imaginary - a color, a date
  • Objects have:
    • Attributes
      • Properties
      • "What the object has"
    • Operations
      • Behaviours
      • Methods
      • "What the object does"
  • A Class
    • A set of objects sharing a common structure and common behaviour
    • An object is an instance of a class
    • Blueprint, or tamplate, for creating objects

The terms instance and object are used interchangably


Identifying Classes

Similar to entity/relationship identification

Get a written description of a situation

Identify the nouns or noun phrases

  • These might indicate classes of objects

Testing if an item is a candidate for a class

  • Relevance to the problem domain
  • Can exist independently
  • Has attributes and operations

Interface and Implementation

Interface is the outside view

  • Public - items accessible to all external objects through the interface (portal)

Implementation - is the inner workings

  • Private - items accessible only from inside the object, hidden from outside (brick wall)

Typically:

  • Attributes (properties) are private
  • Operations (methods) are public
  • Why?
  • This is called Data Hiding or Encapsulation

Class Relationships

System's behaviour is accomplished by collaboration of objects

  • Objects collaborate by calling methods, passing parameters, and receiving returned values

An Association between classes means that an object uses data or methods in another object

For example: Car and Passenger

Association is the most common type of relationship between classes

Other types of relationships:

  • Aggregation: - "has a"
    • One object is a part of another object
    • Whole/part relationship
    • Container and contents
    • Can navigate from whole to it's parts
      • Example: JavaScript - document => form => elements
  • Generalization - "is a"
    • One class is a special case of another class
    • More general / more specific relationship
    • Inheritance
    • More specific class inherits properties and operations of the more general class
    • More specific clas extends more general class