Day 1

This is the day 1 page for object oriented programming course 102, an introduction to object oriented programming. On day 1 we introduce the concepts of classes and objects.

Below are the topics and course materials we are learning today.

What is object oriented programming

Object oriented programming is an approach to writing software where sections of functionality are placed in individual reusable containers called objects. These objects can work and interface with like anything you image, but generally they are most helpful when they are written in a way to mimic objects in the real world.

A simple example of an object

Image you were to designing a television set. Ask yourself what makes a television a television?

A television has a volume right? Doesn't it also have a channel? How about a size?

These are all properties of any television, and if you were to create a representation of a television in code it would make sense to have something with these properties:
type 
  TTelevision = class
    Volume, Channel, Size: Integer;
  end;
Now ask yourself what can you do with a television?

Can you turn a television on and off? Can you change the channel or volume? Can you change the input source on a television?

These are all methods of television, and if you were to modify our representation of a television it might look like:
type 
  TTelevision = class
    Volume, Channel, Size: Integer;
    procedure PowerOn;
    procedure PowerOff;
    procedure ChangeChannel(Number: Integer);
    procedure VolumeUp;
    procedure VolumeDown;
    procedure ToggleMute;
  end;

Classes and objects

Notice that in the examples above we defined our television as a class and not an object. Consider a television in the real world. You likely have a few in your home, and I likely have a few in mine as well. You could say that each television you own is an instance of a television, or an object, and the model or design of the television is its classification or class for short.

The big Samsung HDTV in my living room is a specific class of television. It might work a bit differently than the television in your home, but they're all televisions.

Polymorphism, encapsulation, and inheritance

When thinking about the television example we've chosen to introduce object oriented programming, there are a few key principles that seem obvious and happen to be the foundation of object oriented programming. These principles are polymorphism, encapsulation, and inheritance.

Polymorphism comes from the prefix poly, meaning many, and suffix morph meaning shapes. When applied to a television it could simply mean that they come in may shapes and sizes. While this is obviously true, it can also mean that a television could take many forms, but they all have the same function. That is, they can all be turned on and off, their volume can be adjusted, and the channel or input changed.

Encapsulation also applies to every television in real life. There are many different models or classes of them, and internally they are often designed with much different circuitry and components. Some use an LED matrix with lenses to disperse their back light, while others use florescent tubes. Others use organic light emitting diodes, but all serve the same purpose, and that is to create an illuminated picture.

Just as the underlying methods to present an illuminated picture can be different between classes of television in the real world, so to can the underlying code in various derived TTelevision classes differ. Encapsulation is the principle where what's behind your television screen is not something you need to think about, and with objects in code it's the same. You can use them both without getting bogged down by the details of exactly how they work.

Inheritance is what allows polymorphism and encapsulation to work. It allows you or someone else to derive from and extend a class. Consider the following.
type
  TProjector = class(TTelevision)
    Brightness, Temperature: Integer;
    procedure Focus(Length: Single);
  end;
A class of projectors could be defined as a type of televisions where they illuminate a picture, power on and off, but also change focus and the bulb brightness can be adjusted to be very bright. This could be done by inheriting from a television class. The manner in which the images is created and lit is encapsulated by the projector class, and if you wanted to use a projector you need not concern yourself with exactly how it works. If you understand how to use a television, the chances are good you can easily learn to use a projector.

Show and tell break: a hierarchy of classes

All object oriented programming languages come with a large assortment of predefined classes that you can freely use or extend for your own purposes. Let's take a look at a section of the class library available to you with Lazarus.

Homework

There is no homework for day 1.

See also