Monday, 10 November 2008

My First 1200(ish) words...

OK - Here is my first 1200(ish) words. Please ignore the graphic (figure 1), it is obviously wrong. I am waiting on my graphic designer friend/colleague to draw me something here.


Please feel free to be critical of the content.


------------------------------------------------------------------------------------


An Introduction to Object Oriented Programming (OOP)

What is OOP?

Traditional programming languages, such as Pascal, COBOL, C and Visual Basic (amongst others) are procedural languages. In a procedural language you develop the code in a linear format, like a shopping list for example. You define your function and procedures in terms of (essentially) a list of instructions, building libraries of functions and procedures that perform specific tasks which allows you (the programmer) to modularise your code. Thus providing the facilities to create libraries of functions for reuse.

OOP uses a different paradigm and introduces a whole new plethora of terminology and we will only cover a few of these terms here. The three main terms we will become familiar with are class, method and message. However, we will also discuss properties, instances and inheritance. A few other terms will popup but will only be discussed in very basic detail.

OOP uses the concept of objects that describe “real world” things such as a person, an animal or a shape such as a square. It still employs procedures and functions but these are now called methods (more on this later). In procedural programming you have variables which allow for such things as counting, in OOP these are now termed properties (again more on this later). When a function or procedure was called you would issue a command such as foo(some argument [,maybe some more arguments]). In OOP you achieve similar goals by issuing messages to the object.

When an object is created from a class (more shortly) you can pass messages to it, the object can perform calculations and pass messages back which can be processed by other objects. A properly programmed class will know (also known as anthropomorphism) about itself. For instance, a class created to store the details of a person may have the facility to answer how tall it is; i.e. it knows its own height!

To interact with the object and call its functions and access it properties you use the objects interface. The interface defines all publicly accessible methods and properties of the class. As with procedural languages where you can create libraries of functions and procedures, you may also create libraries of classes that can be used in different programming projects.

Why would we use classes? Once a class is developed and tested it can be used in many different projects. OOP allows inheritance so if additional requirements are required the class can be extended and new functionality added without modification to the original class. What does this achieve? Less bugs and a high reusability factor!

The purpose of this short tutorial is to explain various basic concepts of classes with no reference to actual code, be it Java or otherwise.

What is a Class?

When programming in OOP you begin by creating a description of the object, this is known as a class and this can be likened to a blueprint that describes everything about the object to be created and contains all the necessary functioning components (i.e. methods and properties) to make the object work. However, classes cannot be directly worked with and instances of the class (discussed later) need to be created.

The class definition can also be used to define and enforce business rules, or rules in general that apply. For instance, if you had a class defining a person, then a rule could be that the persons age must be greater than zero or their gender must be male or female.

As previously mentioned a class has an interface. The interface describes how the class’s instances and method’s may be accessed. For instance (excuse the pun!), when creating the Person class you may well define a property which describes the date of birth of the person, dateofbirth, in addition you would define methods (described shortly) that allow the users of the class to both get and set the value of this property, for instance setDob and getDob. The setDob and getDob methods are part of the interface of the class.

Figure 1 represents the relationship between a class and an object.

Figure 1: Class defining a person


The “blueprint” image to the left represents the class, which encapsulates all the properties and methods (the interface) to class, and the image to the right represents an instantiation of the class, i.e. an object created from the class.

What is a Method?

Methods are the means by which a class can perform tasks. A method is essentially the same as a function or procedure in a procedural language. However, depending on how the method is defined within the class may or may not be a visible part of the interface (which is beyond the scope of this basic tutorial).

For instance, given the above somewhat simplistic class defining a person, we have a setDob and getDob method which allows the date of birth of the person to be defined and accessed. But what about a method allowing you to access the person to age instead of simply retrieving their date of birth?

We could of course call the getDob method and calculate it manually, but what about a method to return the person’s age? We could create a method within the class that calculates and returns the person’s age. Perhaps call it getage(). When this method is called it simply calculates the age based on the system clock and the date of birth stored within the object.

One of the important advantages to remember about objects created from a class is that the methods are also created. They are not shared amongst all the objects; each object has its own copy. So for instance if two objects of type people are created and the getage method is invoked it will return the value relevant to the object it is called upon.

There are exceptions to this rule but that is beyond the scope of this tutorial.

What is a Message?

We have methods that are defined in our class and instantiated as part of an object. But how do we use these methods?

As stated previously methods are essentially the same as functions and procedures in procedural programming. However, whereas in procedural programming you would simply call the function/procedure with arguments (if any), in OOP you pass the object a message which may or may not contain arguments (obviously dependant on the definition of the method).

Essentially this is the same as procedural programming. Generally you would use the dot notation to pass the message; i.e. if you have a class named person, from which you have created an object named teamLeader, and a method called setDob which takes an argument of the form date then the calling message would be something like:

teamLeader.setDob(“27/10/1970”)

Objects can also return data to the call object. Using the teamLeader example if we wanted to return the teamLeader’s age we would use a message of the form:

age = teamLeader.getAge()

Obviously the actual syntax involved would be language specific and this is only a very general description.

Having now covered three of the fundamentals of OOP, class, method and message, we will now move on to cover several other topics, namely properties, instances and inheritance.

No comments:

Post a Comment