Skip to content

Object-Oriented Programming

Prelude: It has been said that teaching object-oriented programming (OOP) to those with no programming background is easier than those with experience in (procedural or functional) programming.1 This is because experienced programmers get used to procedural (or functional) thinking and modeling. On the other hand, for non-programmers, the object-oriented way of decomposing a problem is similar to the way they are used to look at real life situations. Indeed, we live in a world made up of interacting objects.

What is an Object?

"An object represents an individual, identifiable item, unit, or entity, either real or abstract, with a well-defined role in the problem domain."2

In a class-based object-oriented programming language3, like Java, any object must be an instance of a class.

What is a Class?

A class defines the attributes, structure and operations that are common to a set of objects, including how the objects are created.

A class is an abstraction or a model.

What is a model?

A model is a partial representation of something else (a concept, a system, a pattern, etc.). It helps us to visualize and understand the original and its role in a problem domain.

When we model something (i.e. create an abstraction of it), we focus on some of its characteristics (that matter to the problem at hand) and ignore others (that are deemed irrelevant for solving the problem). For example, this class models (the idea, concept of) circle in Grade 3 (K5) Geometry:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
public class Circle {
    private double radius;

    public Circle() { radius = 1; }
    public void setRadius (double r) { radius = r; }
    public double getRadius() { return radius; }
    public double getArea() { 
        return Math.PI * Math.pow(radius, 2); 
    }
}

And here we have a few different Circle objects (instances):

1
2
3
4
Circle c1 = new Circle();
Circle c2 = new Circle();
c2.setRadius(3);
Circle c3 = new Circle();

An object has state and behavior.

The state of an object (a.k.a. its properties or attributes) are its essential and distinctive characteristics. A class declares attributes of its objects through its data fields (instance variables). The state of an object is value of those data fields at any point in time. A Circle class, for example, has a data field radius, which is the property that characterizes a circle. A circle object with radius of 1 cm is an instance of the Circle class. If we update the radius of this circle to a new value, we have changed its state.

The behavior of an object is the set of operations, or responsibilities, it must fulfill. This includes the responsibility to provide and modify state information when asked by clients (other objects or services). A behavior of an object is defined by the instance method that implements that behavior. To invoke a method on an object is to ask the object to fulfill a behavior (to perform an action). For example, you may ask a circle object to provide you with its state by invoking the method getRadius() which is declared and implemented in the Circle class.

Encapsulation

A quick detour: an oven is an object! There is a lot going on inside an oven but you interact with it through certain set operations. You, for example, can set the temperature of an oven and thus changing its state (temperature). How an oven exactly operates is not your concern. An oven is, in some sense, a black box to you.

Objects, in software as in real-life, are build in a way to encapsulate their state and behavior in a unit (a bundle, a capsule, a black box, ...) with an interface to allow (and simplify) access for the user.

Class is a unit of encapsulation in OOP; it provides the machinery for bundling state and behavior (data fields and methods that work on that data) within one unit. The state is typically private to the object and not (directly) visible to clients. This concept of denying direct access to the state of an object is called information hiding. In addition to the state, some of the behavior (responsibilities) of an object may also be hidden and only used by the object. The set of visible (public) behavior of an object form its interface and define how it collaborates (acts and reacts) with other objects.

Inheritance and Polymorphism

A superclass/subclass relationship may exist between two classes when one class "is a kind of" the other class. Subclasses inherit the structure and behavior of their superclass. Therefore, common behavior and attributes can be defined once in the superclass rather than repeated in each subclass.

Object Composition

Another effective way to reuse code (but without inheritance) is through object composition which, in a nutshell, is a way to combine objects into more complex ones. (E.g. put a cooktop and an oven together to get a stove!)

Polymorphism (having many forms) is the ability of an entity in an object-oriented model to refer to objects of different classes at different times. For example, a method that accepts an object of certain class will also operate on its subclasses.


  1. Alan Key, for example, discovered that children learned SmallTalk faster than experienced programmers; see Key A., "Microelectronics and Personal Computer." Scientific American 237(3):230-244 (1977) 

  2. Smith, M., and Tockey S. "An Integrated Approach to Software Requirement Definition Using Objects." Boeing Commercial Airplane Support Division, Seatle, WA, (1988). 

  3. There are also prototype-based object-oriented programming languages, like JavaScript.