Object Oriented Programming
This tutorial gives a quick introduction to OO programming and the various concepts of such a programming paradigm and how it is different to other paradigms such as procedural programming. It is especially useful if you plan on learning an Object Oriented programming language such as Java or C++.
OOP vs. procedural programming
Using procedural programming, for example C or Fortran, a program is executed as a series of linear steps and procedures. However some real disadvantages were encountered when programs grew bigger and more complex:
- It was hard to divide the code into independent modules.
- The code was difficult to be reused and extended.
To deal with complexity, object oriented programming provides a different approach that sees a program not arranged around the procedures necessary for certain situations, but around abstract units called objects, and the interfaces used communicate with them. OOP facilitates modularity and permits an easy way for the code to be reused and extended.
Software objects
If we look around us, we find all kinds of real-objects that have different characteristics and can perform different, often multiple, operations. For example, a mobile phone has colour, shape, a certain number of keys, a ring tone speaker and more. These form the features of the mobile phone. What can we do with the phone? We can use it to call a certain person, to hold the details for a contact (name, mobile number, email address of the person), to set the alarm for the mobile phone and many other things.
We can think of the real-characteristics as the states of an object and we can see the performed actions as its behavior.
In terms of programming, the states of an object can be described using variables. A variable is a data item that it is given a name by which it can be refered to. The behavior of the real-object can be defined by writing some methods for that software object. A method is a function that serves the object.
Let's consider as an example a car object. We can define variables like:
- colour
- speed
- the name of the company who produced it
For this object, we can think of implementing some methods:
- retrieving the speed
- retrieving the colour
- retrieving the company name variables and modifying the speed
Note: The state of the speed variable will change if the modify speed method sets a different value for the speed.
Classes
A class is the basic structure of an OOP language which is used to define the states and behaviour that particular objects can have. The objects that are created from a particular class are referred to as "instances" of that class. You can think of it like a blueprint that can be re-used every time you want to create a new object.
If we look around us, we can see many mobile phones that are different objects, but all of them can be identified as mobile phones. The idea is to group the objects that have similar characteristics into a single class - the mobile phone class, and after that to instantiate it to create objects from it with similar look and feel.
As mentioned above, the variables store the data for an object and the methods implement the behaviour, this is illustrated in Figure 2.

Figure 2: A class structure
Abstraction
Abstraction is one of the basic OOP concepts. It is the mechanism that helps reduce the complexity. For a given object, we think of what it can be used for, we don't think of all the individual components that form it.
For example, the mobile phone has a plastic cover, a circuit board, a keypad, a SIM card, a loudspeaker, a LCD display, a battery, an aerial and a microphone, but all these items are inside the mobile phone. We only take the mobile phone as a well-defined object, without taking in consideration the individual parts that form it.
Complex objects can be easily handled if their semantics are divided into different layers. This way, complex objects are classified hierarchically into separate parts. For the mobile phone example, we have an exterior layer that it is the object itself and an interior layer that it is implemented by all the pieces we have mentioned above.
If we go deeper, we can think of the LCD diplay like an item formed from different components. It is a single element when it is used inside the mobile phone, but its inside can also be explored. Thus, we can find a new layer composed of all the pieces that actually create the LCD display as a whole object.
After this brief discussion, we must remember that:
The OOP principles
All the object-oriented programming languages have three basic principles: encapsulation, inheritance and polymorphism.
Encapsulation
Encapsulation is the ability to bundle together the data of an object with its methods. It provides mechanisms to protect the inner structures of the objects from outside points. A well-written Java object has all its variables private - they are only visible inside that object, and it provides ways to access its variables from outside through the use of public methods. Private methods can also be used, but they can only be called from inside the object. Figure 3 shows how such an object can look like.

Figure 3: Encapsulation
Inheritance
Inheritance is an OOP mechanism which allows code to be reused. If we have a certain class, but we want to add some specific characteristics to it, then we create a new class that has some or all of the properties of the initial class and add the new features to it. The initial class is called the superclass (or the parent) and the new class is named the subclass (or the child) .
For example, let's consider the Animal class to be the superclass. For this class we can find some attributes that are common to all the animals like: legs, teeth, size. However, if we think of a certain animal, let's say a cat, we can find other attributes that are typical of it. Thus, the Cat class should be a subclass that inherits all the variables and the methods of the superclass, but adds its own to the code. The Cat class code can modify a variable defined in the superclass, as if it was its own, or can overwrite a method of the parent class.
Note! We can also create a class with specific states and behaviour, but which can never actually be instantiated itself. To do this we put the keyword abstract when we define it. We can still inherit from this class and use its attributes in subclasses, but we can never instantiate the superclass. For example, we might not want to create an Animal object per se, since all animals are likely to be instances of a particular species subclass.
We can think of inheritance to be an "is a" relation between the superclass and the subclass. This way, the subclass is a superclass.

Figure 4: Inheritance
Polymorphism
Polymorphism is the feature which allows multiple methods with the same name to be written within the same program.
To understand this better, think of different geometrical shapes: a circle, a square, a triangle. If we want to calculate the surface areas of these figures, we have different formulae for each of them. Thus, it is necessary to use different code depending on which figure we refer to. The polymorphism ability allows us to use the same name for the methods that calculate the areas of the figures, but one method will calculate the area of a circle, the other one of a square etc.
We can have two types of polymorphism (we will see exactly how they work later):
overridden methods: multiple methods with the same name that have the same parameter lists and the same return type,
overloaded methods: multiple methods with the same name but different lists of parameters.
Note: Polymorphism is possible in Java thanks to something called "late binding of instance methods", i.e. Java chooses the right version of the method at run-time, depending on the type of object created (this is also sometimes called dynamic method lookup).
Naming convention
Names of objects, classes and methods in Java can at first sight seem a bewildering mess of upper and lower case letters. As a rule of thumb:
Classes: have uppercase first letters (e.g. Dog, Cat)
Objects: have lowercase first letters (e.g. fido). Where the object reference consists of more than one word, these are run together with the first in lowercase and the other first letters capitalised (e.g. spotTheDog)
Methods : generally as for objects (e.g. eat, move, getName, wagTail etc)
For more information about naming conventions read Java in a Nutshell (section 7.1).