1.5 Introduction to Object Technology

  • Building software quickly, correctly and economically is important
  • Objects, or more precisely, the classes objects come from, are essentially reusable software components
  • Almost any noun can be reasonably represented as a software object in terms of attributes (e.g., name, color and size) and behaviors (e.g., calculating, moving and communicating)

Automobile as an Object

  • Before you can drive a car, someone has to design it
  • A car typically begins as engineering drawings, similar to the blueprints that describe the design of a house
  • These drawings include the design for an accelerator pedal
  • The accelerator pedal hides from the driver the complex mechanisms that make the car go faster
  • The brake pedal “hides” the mechanisms that slow the car
  • The steering wheel “hides” the mechanisms that turn the car
  • Enables people with little or no knowledge of how engines, braking and steering mechanisms work to drive a car easily

Automobile as an Object

  • Cannot drive a car’s engineering drawings
  • First, must build a car from the engineering drawings that describe it
  • A completed car has an actual accelerator pedal to make it go faster when the driver presses it

Methods and Classes

  • Performing a task in a program requires a method
    • Houses the program statements that perform its tasks
    • Method hides these statements from its user, just as the accelerator pedal of a car hides from the driver the mechanisms of making the car go faster
  • A program unit called a class houses the set of methods that perform the class’s tasks
    • A class that represents a bank account might contain one method to deposit money to an account, another to withdraw money from an account and a third to inquire what the account’s balance is
  • A class is similar in concept to a car’s engineering drawings

Instantiation

  • You must build an object of a class before a program can perform the tasks that the class’s methods define
  • This is called instantiation
  • An object is then referred to as an instance of its class

Reuse

  • A car’s engineering drawings can be reused many times to build many cars
  • Similarly, you can reuse a class many times to build many objects
  • Reuse of existing classes when building new classes and programs saves time and effort
  • Also helps you build more reliable and effective systems because existing classes and components often have undergone extensive testing, debugging and performance tuning
  • In Python, you’ll typically use a building-block approach
    • Use existing high-quality pieces wherever possible

Messages and Method Calls

  • When you drive a car, pressing its gas pedal sends a message to the car to perform a task—that is, to go faster
  • You send messages to an object
  • Each message is implemented as a method call that tells a method of the object to perform its task
    • E.g., call a bank-account object’s deposit method to increase the account’s balance

Attributes and Instance Variables

  • A car, also has attributes, such as its color, its number of doors, the amount of gas in its tank, its current speed and its record of total miles driven (i.e., its odometer reading)
  • Attributes are represented as part of its design in its engineering diagrams
  • As you drive an actual car, these attributes are carried along with the car
  • Every car maintains its own attributes
    • e.g., each car knows how much gas is in its own gas tank, but not how much is in the tanks of other cars

Attributes and Instance Variables (cont.)

  • An object has attributes that it carries along as it’s used in a program
  • Specified as part of the object’s class
    • A bank-account object has a balance attribute that represents the amount of money in the account
    • Each bank-account object knows the balance in the account it represents, but not the balances of the other accounts in the bank
  • Attributes are specified by the class’s instance variables
  • A class’s attributes and methods are intimately related, so classes wrap together their attributes and methods

Inheritance

  • A new class of objects can be created conveniently by inheritance
  • The new class (called the subclass) starts with the characteristics of an existing class (called the superclass), possibly customizing them and adding unique characteristics of its own

Object-Oriented Analysis and Design (OOAD)

  • Large and complex projects should follow a detailed analysis process for determining the project’s requirements (what the system is supposed to do), then develop a design that satisfies them (how the system should do it)
  • Should go through this process and carefully review the design (and have others review the design) before writing any code
  • If this process involves analyzing and designing your system from an object-oriented point of view, it’s called an object-oriented analysis-and-design (OOAD) process
  • Languages like Python are object-oriented
  • Programming in such a language, called object-oriented programming (OOP), allows you to implement an object-oriented design as a working system

©1992–2020 by Pearson Education, Inc. All Rights Reserved. This content is based on Chapter 1 of the book Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and the Cloud.

DISCLAIMER: The authors and publisher of this book have used their best efforts in preparing the book. These efforts include the development, research, and testing of the theories and programs to determine their effectiveness. The authors and publisher make no warranty of any kind, expressed or implied, with regard to these programs or to the documentation contained in these books. The authors and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of these programs.