title=Programming Concepts: Object-oriented programming (OOP)
Key elements of OOP
There are four key elements when designing or writing a program using OOP:
In OOP, an Object is compound data: it combines other things together into a single bundle. As noted above, what makes the Object unique is that it combines data (e.g. integers, strings, references) with code (i.e. procedures).
In most languages, Objects do not exist in source code: you cannot directly "write down" an object. Instead you must create an Object piece-by-piece, filling-in the individual bits of data and code.
It is important to remember that not only does the Object contain data, but it is also data itself. Once the program starts running, you can freely change the value of any piece of data inside the Object, and in most languages you can also change the elements (data, code) of the Object itself.
Typical Objects can be things that appear on the screen (e.g., pictures, textboxes, etc.) or things are part of the programming (e.g. actors, connections, particles, etc.).
To use any value (the number 7, the word Apple) we need a data-type. Numbers have a data-type e.g. "integer", and text has a data-type e.g. "string".
To define Objects and work with them in source-code, we need a data-type. Classes are the data-type for Objects. Each Class defines the specific set of data and the specific procedures that will make a particular Object.
Classes as templates
Because Objects are complex, with many different variables and procedures inside them, Classes are more complex than normal data-types. Most OOP languages give additional features to Classes to help us use them and create them.
Not only does a Class define a data-type for an Object, but it usually defines default values for new Objects. This allows us to use Classes like a template, or biscuit-cutter: a single Class can stamp-out many identical Objects. The Class can even make small changes to each Object as it is created, as we'll see later.
Since Objects are themselves data, each Object can be separately modified after it's created, even though they start out similar or the same.
Let's take a look at the following example:
class car private maxSpeed as integer public fuel as integer public sub setSpeed(byVal s as integer) maxSpeed = s end sub public function getSpeed() as integer return maxSpeed end function public sub refuel(byVal x as integer) console.writeline("pumping gas!") fuel = fuel + x end sub public function getFuel() as integer return fuel end function public sub drive() fuel = fuel - 1 end sub end class
You can see that the class is called
Remember this is a class and therefore only a template, we need to 'create' it using an object
Procedures (and Functions) can be created anywhere, and used by any code at any time. In OOP, we want to restrict some Procedures (and Functions) so that they can only act upon the data inside the Object they are attached to. These restricted versions have a special name: Methods.
Every Method is a Procedure, with two special features:
- We can restrict which source-code is allowed to access the Method, based on the Object the Method is attached to, and the Class that created that Object
- Methods have access to a special variable that is the Object and/or Class they are attached to. Depending on language, this variable is usually named "self", "this" or "Me" ("self" is used in Delphi/Pascal, Ruby, and (conventionally) in Python, "this" is from Java and C#, "Me" is from VB.Net).
Data can be created anywhere, and used by any code at any time. In OOP, we want to restrict some data so that it can only be acted-upon by the Methods in the same Object. These restricted variables have a special name: Attributes.
Every Attribute is a Variable, with three special features:
- The Class that creates the Object usually provides a default or starting value for each Attribute
- Attributes can optionally be hidden from other code
- Attributes can optionally be made visible to other code, but made uneditable (only the code (Methods) in the Attribute's Object can edit the Attribute)
NB: Attributes are further split into two types: Fields and Properties. All Fields and all Properties are Attributes, but with extra specialization. See the section on Encapsulation for the precise differences.
In the example above we store the fuel and maxSpeed.
Putting it together
methods and attributes
structures don't have methods