Why do you need to understand object oriented programming ?
If you don’t have a grasp of OOP, or at least a three thousand foot view of this subject. Then you will be at a disadvantage communicating with software developers.
Also the projects that you write yourself will be less effective and more difficult to write than they need be.
If you aim to write some code in any modern macro or programming language, then without a basic understanding of these principles, it will seem way more complicated than it need be.
If you do understand the principles covered here, and in some future articles, you will begin to see the commonalities between different programming languages and understand their essential principles quicker.
You will get a good high-level grasp of the process of building software, and, If you need to write your own code, then gaining an understanding of what i’m about to cover will make the learning process a whole lot easier.
Object oriented programming is essentially just a coding methodology (when supported by your programming language).
In object orientation the code is grouped into modules or virtual modules that emulate real-world objects.
This is in contrast to procedural or functional programming which is essentially the code reading and executing a list of commands.
So what does this mean in practice?
Well what’s going on here is that object orientation is attempting to mimic the real world so that you can solve real problems.
Everything in the world is a “thing” and having a programming language that allows you to emulate this fact (a thing being an object), makes it a lot easier to build great software.
Where Is object oriented programming used?
Computer games come to mind as software that I personally believe would be almost impossible and certainly much more expensive to build without using OO techniques.
If you think of a strategy game where you have aircraft flying and tanks moving around.
Combine this with the existence of friendly and enemy forces.
All with their own artificial intelligence, you just could not do this without an object oriented programming methodology.
Air Traffic Control Systems, Traffic Control Systems, Contact Management Systems, Stock Market and Dealer Trading Systems all come to mind as sound candidates for OO Programming.
Just about all business programming situations are excellent candidates for OOP, as in these cases you are modelling customer, competitor, cost, production or market behaviours.
In essence you are modelling or recording or gathering data on and attempting to understand some real world behaviour
Why Should You Use It?
Object oriented programming (OOP) makes use of the single most important trait of success that has fuelled human evolution, that is the ability to delegate or specialise.
What this means is that the software developer can break a problem, down into bite size chunks, and make each of these chunks a mini program that is responsible for its own existence.
Each of these mini programs have interfaces that enable them to talk to one another.
You could say OOP mimics nature. Take human beings for example.
In general we all have parents some of us have children and grandchildren and cousins.
We also interact with people that we are not related to in various contexts (business and social).
When we build objects in code, if that object has a parent and a grandparent in the real world then we would also model that in code. More on this later.
How Do You Use It?
Most, but not all, programming languages that are object-based use dot notation.
Dot notation is how you express your commands in these languages.
It is arguably an elegant way of expressing the relationship of objects to one another.
If you wished to write a program to model some behaviours of the universe you might come up with an object model such as the following.
Universe.LocalSuperCluster.LocalGroup.Galaxy.Star.Planet.Continent.Country.City.Town.Street.HouseNumber
The code above is a non-language specific example of a hierarchical class structure.
I remember when I first heard about object-oriented programming, it took me a little while to get a handle on the difference between an object and a class.
An object is an implementation of a class just like the Empire State building is the object implementation of the architects class which was the architectural blueprint.
Conceptually a class is the diagram of an object, you only have one class from which one to many object instances can be derived.
In an Equity trading system a Class might be Equity which has properties such as name, price, high, low and volume.
From this class objects are derived which have names such as Microsoft, Oracle, Apple, Chrysler etc.
Or the millions of humans on this planet are all objects derived from the class human.
Every chair in your office is an object implementation of the single class chair.
There’s a heck of a lot more to object orientation and I will aim to cover a lot of it in future articles however I want to first solidify this article in your mind
So using the universe as an example lets look at some code which is not specific to any programming language, but just like Latin is the basis of many spoken languages this code is similar to the majority of OO languages out there.
01 function buildOutSubUniverse(){ 02 int iGalaxyCount; // Variable of numeric type integer 03 // Create a new object of the universe specification and instantiate 04 // it immediately as a "universe" variable. 05 Universe oUniverse = new Universe() 06 Galaxy oMilkyWay; 07 Galaxy oAndromeda; 08 09 oMilkyWay = oUniverse.SuperClusters("virgo").LocalGroup.Galaxies("MilkyWay"); 10 oAndromeda = oMilkyWay.parent.Galaxies("Andromeda"); 11 oMilkyWay.Name = "Milkey Way"; 12 oAndromeda.Name ="Andormeda"; 13 14 // for the purposes of this code, The galaxy count below would be two as the 15 // universe here has initiated two new galaxies. (Milky Way and Andromeda) 16 iGalaxyCount = oUniverse.Galaxys.Count; 17 18 Alert("This model has created " + iGalaxyCount + " Galaxys"); 19 20 oMilkyWay.ExpansionRate = .01; 21 oMilkyWay.Expand(); 22 oAndromeda.ExpansionRate = .04; 23 oAndromeda.Expand(); 24 }
Lets go through the above code
Line 1 and 24 define the boundaries of a function which is the container for the code.
This would be called from other code like this.
buildOutSubUniverse();
Lines 3, 4, 14 and 15 are comment lines which in most “C” style languages are defined either by starting each line with // or for comments on multiple lines starting with /* and ending with */
Line 5 declares a new universe object (oUniverse) from the universe class (Universe oUniverse) and instructs the code compiler to create it right now (= new Universe() )
Lines 6 and 7 declare two new galaxy objects, but have not yet created them.
They are created on lines 9 and 10 where we ask the universe object to give us a code reference to two galaxy object on line 9 and again on line 10 and assign these Galaxy objects to the previously created oMilkWay and oAndromeda objects respectively.
Notice that on line 10, we are getting to the same result as line 9. That is a reference to an existing Galaxy.
However we are leveraging the work done on line 9 by saying, Give me the Milky Way galaxy, now get me it’s parent (“which is the local group”), now get me the sub galaxy of the local group called “Andromeda”.
This short cut saves us from having to navigate all the way from the top level universe object once we have a reference to a “nearby” object.
Now to be clear the names oMilkyWay and oAndromeda are just the “variable” names () of the objects, you could just as easily and correctly from a CODE point of view called them X and Y or Elvis and GaGa (would not make much sense but the code would work fine just the same).
The point i’m making is that we created two objects of type (class) Galaxy, but the objects do not know what Galaxies they represent.
If the code got modified in the future by the same or a different programmer, oMilkyWay could quite easily represent something totally unrelated to the milky way galaxy (and the code would still be functional, Not great engineering, but if it works it works.). So then…
This is why we must explicitly set properties of the object, such as the name of the Galaxy and other properties.
09 oMilkyWay = oUniverse.SuperClusters("virgo").LocalGroup.Galaxies("MilkyWay"); 11 oMilkyWay.Name = "Milkey Way"; 20 oMilkyWay.ExpansionRate = .01;
Classes and their object derivatives have three main ways of interacting with their environment – properties, methods and events.
Events will be discussed in a future article.
Properties are the descriptive elements of the class such as colour, age, name, version, Height etc and can be represented by public variables of the class.
Methods on the other hand are the verbs or Action Oriented part of the class interface.
Such as if you built a submarine object in your code… methods could be Dive(), Ascend(), RunSilent(), StartEngines() and StopEngines().
Methods can be implemented as public function of the class.
This is shown in the code on lines 20 and 21 for example, where we first set the Expansion rate property of the object to .01, (for the rate of Galaxy Expansion) and then tell the oMilkWay Galaxy object to start running it’s expansion process by calling it’s Expand() method. (public Expand() function in the class)
20 oMilkyWay.ExpansionRate = .01; 21 oMilkyWay.Expand();
Wrap Up
This article has covered coding with pre-existing objects, which is a very common form of object oriented coding, which can be found for example in Microsoft Word, Excel and Access vba macro code for example.
I have covered objects from a conceptual point of view and how to use and manipulate existing objects in code
The next stage is creating your own objects by defining and writing classes which represent real world problems and implementing their solution in code.
Leave a Reply