Monday, December 20, 2010

Object Oriented Programming (OOP) 1 - some resources

Object Oriented Programming is a way of bundling up large amounts of code into something that is easier to understand, and hopefully use. The code is called "classes" and when you create an "instance" of that class, it becomes an "object".

The examples I used in our class work at college, I referred to classes as "cookie cutters" that give us the "shape". Each time we press the cookie cutter into the cookie dough, we create a cookie "object" which we can decorate with sprinkles and candy.

In it's simplest form, classes are the "templates" of our objects. Classes can contain "properties" (the values we can read and/or change), "methods" (actions than can be performed), and in VisualBasic.NET there are also "events" that can be raised with event handlers (but to be honest, this is really only useful for User Controls).

Here's an example.

Say we have a "Car" class. We can create an "instance" (or a single car) from this class. Like this, and do things with it:

Dim myCar as Car
myCar = new Car()
myCar.Brand = "Toyota"
myCar.Model = "Camry"
Console.WriteLine(myCar.Wheels()) 'COMMENT: this displays "4"
myCar.Drive(10) 'COMMENT: the speed is "10"
myCar.ChangeDirection("left", 45) 'COMMENT: turn left at 45 degrees
myCar.Stop()

Lets look at that more closely:

Dim myCar as Car

just tells VB.NET to reserve enough RAM (memory) to put a "Car" object inside it. NOTE: this does not (yet) create the object. This also gives a name within the code to call our car ("myCar").

myCar = new Car()

This creates the object - the instance of our class. The word "new" is important because this is the "constructor" - the special Function or Sub that will be run to start our object. It is the perfect place to set values to create our object (like read a database to get values). NOTE: you don't have to create your own Sub called NEW - VB.NET will create an empty one for you and use that instead. This is called an "implied constructor". If you create your own "New()" sub that is an "Explicit constructor".

REMEMBER: a constructor is simply a function or sub that is run when you create your new object for the first time.

You can also write those two lines of code as one line like this:

Dim myCar as Car = new Car()

The next two lines are setting some property values:

myCar.Brand = "Toyota"
myCar.Model = "Camry"

This sets the "Brand" property to "Toyota" and the "Model" property to "Camry".

You can also read property values:

Console.WriteLine(myCar.Wheels()) 'COMMENT: this displays "4"

where "Wheels()" is a property and in this case is used to get how many wheels the car has and displays it to the screen.

REMEMBER: Properties are just values for our objects that we can "set" (write) and "get" (read). For a "Person" class, some properties could be "FirstName" or "LastName".

The next three lines are some of our classes "methods"

myCar.Drive(10) 'COMMENT: the speed is "10"
myCar.ChangeDirection("left", 45) 'COMMENT: turn left at 45 degrees
myCar.Stop()

Methods are just Functions or Subs that are run when used in code like this. Just like normal functions and Subs you can pass in values for them to use. Here there are three Subs within our class called:

Drive(speed) which takes in "speed" as an integer to use as a parameter (argument)
ChangeDirection(direction, angle) which takes in two parameters (arguments) to successfully run this sub ("direction" as a string and "angle" as an integer)
Stop() which takes no arguments to run

REMEMBER: Methods are simply Subs that allow our object to do things. Another example could be a "Person" class which has a method called "Login(username, password) which takes two values ("username" and "password") to correctly work.

What Now?

This is just how to use Objects within our code. I will explain how to write classes later but meanwhile have a read of this:


Start here at Chapter 6 and work through until the first part of Chapter 7 where it talks about inheritance. I do not expect students to be experts at using Classes and Objects yet, but you should be able to understand what classes, objects, methods and properties are, and be able to write the "code stubs" (the starting code) when given a specification.

I will write another blog entry about inheritance later but to get you thinking, imagine our "Car" class being derived from a "Vehicle" base class. A "Motorcycle" class could also be derived from the "Vehicle" class, as could a "Truck".

A "base class" is simply a starting point that you can use to create more complex classes later (you "inherit" the base class and add to it). Not all classes need to come from base classes, and inheritance is not something you do all the time, but there are important programming ideas here that you need to understand.

REMEMBER: the whole point about Object Oriented Programming (OOP) and creating classes is to write code once and test it to make sure it is working the way you want, then you can put it aside and forget about how it works. Then you can write really simple code that uses your classes.

Be lazy: write code once and test it well: don't repeat yourself (in other words, don't rewrite your code). Try to keep your code simple in small bite-sized pieces that is easy to understand ... and use.