Part A - Introduction

Object Terminology

Introduce objects and classes
Describe encapsulation, inheritance, and polymorphism

"The span of absolute judgement and the span of immediate memory impose severe limitations on the amount of information that we are able to receive, process and remember.  By organizing the stimulus input simultaneously into several dimensions and successively into a sequence of chunks, we manage to break this informational bottleneck" (Miller, 1956). 

Abstraction | Classes | Encapsulation | Polymorphism | Summary | Exercises


In 1956, Professor Miller of Princeton University demonstrated that most of us can only comprehend about seven chunks of information at a time.  We learn to play with small sets of chunks at an early age, and over time, to break down each problem into a set of manageable chunks. 

In an object-oriented language we refer to each chunk of a programming solution as an object.  These languages let us describe our solutions in terms of not only objects but also the common structures that subsets of objects share.  They refer to the structure shared by each subset of objects as their class.  They provide the syntax for combining detailed structural information and related logic (encapsulation), for constructing new structures from existing ones (inheritance) and for associating logic with an object's current structure, which may change during the object's lifetime (polymorphism). 

This chapter introduces these terms at a high level: objects, classes, encapsulation, inheritance and polymorphism.


Abstraction

In designing a solution to a programming problem, we abstract the most important components and identify them as objects.  In identifying the most important objects, we hide their less important details within themselves. 


entity


Each object has a crisp conceptual boundary and behaves only in ways that are appropriate to itself.  A book has pages that are bound and can be flipped, but their order cannot be changed.  Notes have loose pages that can be removed or rearranged in any order. 

The preceding chapter introduced the cout and cin objects as examples.  cout represents the standard output device, which may be a monitor or a printer.  The abstraction - the standard output device - is simple and distinct from other abstractions.  Internally, the cout object is quite complex.  Similarly, cin represents the standard input device, which may be a keyboard, a tablet or a touch screen.  The abstraction - the standard input device - is simple and distinct from other abstractions.  Internally, the cin object is also quite complex.


Classes

We describe the objects that share a common structure in terms of a class.  Objects of the same class, although they share a common structure, need not be identical.  Variables that describe their state typically have different values.  For example, all of the books in the figure above have a title and an author, but each book has a different title and a different author. 

We say that each object is an instance of its class and that its instance variables have their own specific values. 


classes


Relationships between Classes

The relationships between the classes introduced in the preceding chapter are illustrated in the figure below.  They include association, composition and inheritance.


class relationships

Association

Association is a loose relationship.  We call a unidirectional association a using relationship.  The EAN class uses the GS1Prefix class to extract the country code and company number.  We depict a unidirectional association by an open circle. 


using relationship

Associated classes need not share any common structure. 

Composition

Composition is a slightly tighter relationship.  We say that in a composition one class has another class.  An Order has an EAN.  We depict a composition by a filled circle. 


has a hierarchy

Composed classes, like associated classes, need not share any common structure. 

Inheritance

Inheritance is an even tighter relationship.  It is hierarchical.  We sat that in an inheritance, one class is a kind of another class.  A SpecialOrder is a kind of Order.  We depict inheritance by an arrow pointed towards the inherited class. 


is a kind of hierarchy

An inherited class, unlike associated and composed classes, shares the structure of its parent class.  A SpecialOrder includes the structure of an Order, plus more. 

UML

The Unified Modelling Language (UML) is a widely used language for designing object-oriented systems.  This language defines standard symbols for classes and objects and for the relationships between them.  The connectors shown in the relationship diagrams above are UML connectors.


Encapsulation

Encapsulation refers to the integration of data and logic within a class.  The class definition consists of the variables that store each object's data and the functions that contain the logic that operates on that data.  We call the variables data members and the functions that operate on their data the member functions of the class. 

encapsulation

Encapsulation separates an object's external appearance from the details of its implementation. 

For example, consider the following statement from the preceding chapter:

 cout << "Welcome to Object-Oriented";

cout refers to the standard output object.  Its class define how its data members are stored in memory.  How the object stores its data and the logic involved in manipulating that data is hidden from us.  The << operator simply copies the string to the output object without exposing any details. 

A well-encapsulated class hides all implementation details within itself.  Client applications cannot see the data that the class' objects store within themselves or the logic that they use to manage their internal data.  A client application only sees a clean and simple interface to the object. 

As long as the classes in a programming solution are well-encapsulated, any programmer can upgrade the internal structure of any object without disrupting the rest of the code within the solution. 

The Class Diagram

The simplest graphical representation in UML is the class diagram: a rectangular box with three compartments:

  1. the upper compartment identifies the class by its name
  2. the middle compartment identifies the names and types of its data members
  3. the lower compartment identifies the names, return types and parameter types of its member functions

For example,

Order
 quantity : int  
 ean : EAN 
 receive() : void 
 place() : void 

The naming conventions include:

  • begin each class name with an upper case letter
  • begin each member name with a lower case letter
  • use camel case throughout all names - capitalize the first letter of every word after the first word

UML refers to data members as "attributes" and to logic or member function as "operations".


Polymorphism

Polymorphism refers to the selection of the operation that is most appropriate to the object involved.  One example of polymorphism is the selection of an operation on an object that belongs to an inheritance hierarchy.  The set of inherited classes may implement the operation in the same way for each class or differently for different classes.  A polymorphic language selects the operation based on object's place within the hierarchy. 

Different Operations

The classes within the inheritance hierarchy that implement an operation differently require separate definitions.  The logic for the place() operation on an Order object differs from the logic for the place() operation on a SpecialOrder object.  The operation on a SpecialOrder object requires additional input of special instructions. 

different behavior polymorphism

A polymorphic language allows identical naming of a member function across different definitions and selects the appropriate definition based on the object's class. 

Identical Operations

The classes within the inheritance hierarchy that implement an operation identically require only one definition.  The logic for the receive() operation on a SpecialOrder object is identical to that for an Order object (a SpecialOrder is, after all, just a kind of Order).  Redefining this logic for the SpecialOrder class would only duplicate existing code. 

identical behavior polymorphism

Polymorphic languages avoid duplication of identical logic across objects that are instances of different classes. 


Summary

  • an object is a chunk of information with a crisp conceptual boundary and a well-defined structure.
  • an object-oriented language facilitates abstractions of important chunks of information from a problem domain.  It let us identify those features that distinguish each chunk from all other chunks in the domain and define each chunk's crisp conceptual boundary. 
  • a class describes the structure that is common to a subset of objects.  An object is a single instance of its class.
  • Encapsulation refers to the hiding of the implementation details within a class - the internal data and internal logic are invisible to the client application, which uses objects of that class. 
  • Inheritance refers to the sharing of structure and logic amongst classes within the same hierarchy. 
  • Polymorphism refers to the selection of most appropriate operation based on the class to which an object belongs. 

Exercises




Previous Reading  Previous: Welcome to Object-Oriented Next: Modular Programming   Next Reading


  Designed by Chris Szalwinski   Copying From This Site   
Logo
Creative Commons License