411 REFERENCE · Wikibooks
A-level Computing/AQA/Paper 1/Fundamentals of programming/Elements of Object-Oriented Programming
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
Object-oriented programming
Features of Object-Oriented Programming
Key elements of OOP
thumb|upright=2|right
There are four key elements when designing or writing a program using OOP:
Object
Class
Method
Attribute
Object
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.
Example: Objects
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.).
Class
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.
left|thumb|upright=2
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.
Example: Classes and Objects
right|thumb|You can use the same class as a template to make (instantiate) many objects
Example: Classes and Objects
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 car and it has:
two attributes: maxSpeed, fuel
five methods
three procedures: setSpeed, refuel, drive
two functions: getSpeed, getFuel
center|300px
Remember this is a class and therefore only a template, we need to 'create' it using an object
Method
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).
Attribute
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.
Example: Attributes
In the example above we store the fuel and maxSpeed.
Putting it together
Exercise: Object Orientation Intro
What is the difference between a class and an object?
A class is a template which cannot be executed
An object is an instance of a class which can be executed
one class can be used to make many objects
What are the main components of a class?
methods and attributes
What is the difference between a structure and a class
structures don't have methods