Class-First Coding
© 21 Oct 2013 Luther Tychonievich
Licensed under Creative Commons: CC BY-NC-ND 3.0
other posts

Learning to Program

Lesson 1: Classes and Fields


Conceptual Overview

A fundamental goal in most programming is to create, inside the computer, a model of the real world. One of the most popular ways to do this is with “‍objects‍”

An object has state and behaviors. The state is the details of the object that we care about; the behaviors are the activities we associate with the object.

Each actual object belongs to a class of objects. A class describes what the state and behaviors are and can be thought of as a blueprint. An instance of a class has particular values for the state and you can have many of them. For example, I might describe a class “‍car‍” as having (among other things) a speed as part of its state. Each instance of the car class actually has some speed: this one is stationary, that one is moving 18 meters per second, etc. All instances of a single class have the same methods (all cars can accelerate, for example) but these methods operate on the state of the particular car in question (when one car accelerates, only its speed changes).

In programming vocabulary, the state of an object is specified as a set of fields. Each field has a type, which is usually the name of some other class. The behaviors of an object are specified as a set of methods. Methods have several pieces; more one these later.

Coding Classes and Fields

In most C-derived object-oriented languages, including C++, Java, C#, and D, a class is specified by writing

class NameOfThisClass {
fields and methods are described here

The fields and methods may be put in any order. Each field is specified as TypeOfField nameOfField; the semi-colon is required in most of these languages. The indentation between matched braces is so common as to be almost universal, but is not required by the language.

The particular fields you select are a matter of design and choice. For example, this model:

class Car {
Point location;
Number mph;
Degrees heading;

and this model:

class Car {
Engine engine;
Wheel frontLeft;
Wheel frontRight;
Wheel rearLeft;
Wheel rearRight;

suggest that the class Car will be used in very different ways. In the first, a Car is just something that moves. In the second, a Car is an engine with four wheels, and any motion would have to be part of the state of the wheels. Perhaps we might define the Wheel class as follows:

class Wheel {
Inches diameter;
PSI pressure;
RPM speed;

There are certainly other ways we could model wheels: maybe we care about tread, or width, or age, or steering orientation. We also don’t have to use as many types as we have above; many people would actually write the above class as

class Wheel {
Number diameter;
Number pressure;
Number speed;

The choices of what fields to include and what types to use for those fields are just two of the many decisions that are made in programming. Because there is no need to manufacture instances of a program or do similar repetitive tasks, almost all of the work of programming is some kind of design.


As we design classes, we usually have fields whose types are other classes. But if that was true of all fields, we’d never be done. Eventually this infinite regress of classes must stop, and in programming it stops at the primitive types.

The exact set of primitives varies somewhat between languages, but almost all of them include the following:

Every type that is not a primitive type has to be defined as a class. Usually a large set of these classes are defined in advance by the language designers.

Arrays and Pointers

Most languages add an extra almost-primitive type to store variable numbers of things of a single type. The details of how this is done vary from language to language; we’ll get into it later on.

Looking for comments…

Loading user comment form…