A-level Computing 2009/AQA/Problem Solving, Programming, Operating Systems, Databases and Networking/Programming Concepts/Object-oriented programming (OOP)

Object oriented programming is a type of programming paradigm based around programming classes and instances of classes called objects. These can be objects that appear on the screen (e.g., pictures, textboxes, etc.) or are part of the programming (e.g. actors, connections, particles, etc.).

Classes
Structures are very similar to Classes in that they collect data together. However, classes extend this idea and are made from two different things: Let's take a look at the following example: You can see that the class is called  and it has: Remember this is a class and therefore only a template, we need to 'create' it using an object
 * Attributes - things that the object stores data in, generally variables.
 * Methods - Functions and Procedures attached to an Object and allowing the object to perform actions
 * two attributes:
 * five methods
 * three procedures:
 * two functions:

Attributes
These store information about the object. In the example above we store the fuel and maxSpeed. The attributes are attached to the classes, and if there are several instances (objects) of the classes then each will store its own version of these variables. Note that instead of the usual dim, there is the word private or public, we'll cover that in the encapsulation section.

Methods
Unlike structures, OOP allows you to attach functions and procedures to your code. This means that not only can you store details about you car (the attributes), you can also allow for sub routines such as  and , which are attached to each class.

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

OO - PIIE
When talking about OOP you must remember the following: Where:
 * 00 = Object Orientation and
 * PIIE = Polymorphism / Inheritance / Instantiation / Encapsulation

Instantiation
As we have seen a class is a template for something, you can't actually execute a class, you must instantiate it, that is create an instance of an class in the form of an object. The code above creates an object called polo and escort, both of class type car (which we declared earlier). We can now use all the public attributes and methods: This would output the following:

pumping gas!

pumping gas!

pumping gas!

polo: 89

escort: 29

Write your own instantiation for a beetle car:

What would the following output:

pumping gas!

pumping gas!

pumping gas!

ka: 20

montego: 37

Encapsulation
You noticed that we didn't have to use the dim statement for the attributes and we used the word  instead. What this means is that these attributes are not directly accessible once you have instantiated the class. Let's take our polo class as an example: In the example we access the fuel attribute of the polo class and give the car 100 units of fuel because fuel is declared as public, there are no restrictions in accessing it. However, when we try the following we run into trouble: The reason that this wouldn't work is because we have declared the maxSpeed attribute as private. If something is declared as private you can't access it externally, but how do you access it? The only way to access a private method or attribute is to use an interface, or public method. In the car code example we have: Because this method is public we can call it through:. And because setSpeed is declared inside the car object, it can have access to all the private attributes and methods.

We also need to find out the speed of a car to display to the user, we can do this by creating a get routine: Because this method is public we can call it through:. And because getSpeed is declared inside the car object, it can have access to all the private attributes and methods.

In general attributes should always be declared as private and only accessible through interfaces, using a setX command to give the private variable X a new value, and using the getX command to return the value of X. You should never directly access X!

Declare a colour attribute for the car that can only be accessed through an interface

Write an interface to set the colour

Write an interface to return the colour

Create an actor class with the following:
 * attributes: health, dexterity, x, y, strength
 * methods: walkforward, eat, gethit, displaystats, setHealth(p), setLocation(px,py)

For the actor class declared above instantiate:
 * Wizard called Barry with 100 Health starting at 4,5
 * Orc called Herbert with 35 Health starting at 20,2, then report on his status

Encapsulation is such a common thing that some languages have short cuts for making get and set routines. In VB.NET this involves using a : As we will never call  directly it doesn't matter that it has an underscore at the beginning. Using this code we can perform the following methods without having the fuel attribute as public:

Inheritance
Building on the car example above, what would happen if we wanted to declare an electric car? Well we'd probably want to store some information on the number of batteries that it has: This seems like a very long and tedious task rewriting all the same code again. You're right! It would be far better if we only had to declare all the new stuff we wanted to add. OOP allows for inheritance, where a new class can inherit the attributes and methods of a parent class: This means that everything that car declared is now accessible from electricCar, as well as the new numBatteries attribute and methods. Let's instantiate this example and see what's possible By using inheritance it makes creating new classes very quick and easy. It also allows for a modular approach to creating classes, where you might never use the base class at all, but only as a means of creating other child classes.

Rather than having to rewrite the same functionality for similar objects, OOP allows you to reuse attributes and methods from parent classes.

Declare a new class called limo that has attributes numSeats and colourSeats; and the ability to interface with them

What are the benefits of using inheritance? Creating new classes from parent classes is very quick and easy. It allows for a modular approach to creating classes, where you might never use the base class at all.

Inheritence diagrams


What is the parent class of car in the above diagram? vehicle

Draw an inheritance diagram for the following classes:
 * motor bikes, bikes, vehicles, pedal bikes, trikes.



Draw an inheritance diagram for the following classes:
 * Monster, Character, Dragon, Hero, Orc



Polymorphism
When you were young you might have watched the Mighty Morphin' Power Rangers. These guys could morph from regular people into their power suits. Sadly polymorphism isn't that exciting, but it does allow you to change the function of attributes and methods when you are inheriting from a parent class. Consider our car example again. When we created the  object we inherited from   and added the   attribute and methods. But what happens when we try and refuel, let's take a look at the code: Well this just won't do. We are creating an electric car and we don't want to say that we are pumping gas; what would our sandal-socked yoghurt eating friends say! So for the  object we want to inherit everything from , but we want to morph(change) the   method. To do that we are going to use something called override:

In polymorphism what is the keyword we use to redefine a subroutine:

overrides

Write a definition of a limo class that uses polymorphism that makes sure that the maxSpeed never goes beyond 100:

Write a definition of musclecar that inherits car, but uses 30 units of fuel per drive and displays "vroom vroom!" every time it drives. It should also store details on leatherSeating, allowing you to interface with this.

Describe polymorphism

Polymorphism allows you to inherit attributes from a parent class, but redefine some of the methods or attributes