OOP Quick Start

In the next several articles, I hope to help you understand the power of Object-Oriented Programming (OOP) in a step by step manner. If you haven’t programmed in an Object-Oriented language yet, don’t worry, I am going to break it down into small pieces all of the ideas and concepts behind OOP development so you can easily understand them. Object-Oriented Programming has been around for quite a while now and is supported in a variety of languages such as C++, Smalltalk, Delphi, Visual FoxPro and others. What language you use will dictate the syntax, but the concepts will remain the same. Once you grasp the basic concepts of OOP development, you will understand its power in helping you develop more robust and complex applications more quickly. It’s a major paradigm shift for those of us that have learned OOP development after developing in procedural languages for so long. In the procedural world, everything was coded line by line to get the job done that you needed the program to perform. For simple applications this worked pretty well such as in the printf("Hello World") example! In an object-oriented world, we design self-contained objects to perform the functions and procedures that we need in the application. In more complex systems upfront design can save a lot of headaches down the road and can be more quickly and more easily modified to accommodate new features as they are requested. OOP is not the savior to all applications, but it can greatly improve an application’s maintainability. However, in a poorly designed system can also hinder the maintainability of the system as well. Therefore, a lot more work needs to be put into the original plans of an application than before.

Let’s get started by talking about objects in Object-Oriented (OO) development. What is an object? Well, it’s defined by it’s Properties, Events and Methods (PEM’s). What does this mean? It means that any object whether it be an invoice or a customer or a form on the screen has these Properties, Events and Methods. One of the examples that I like to use when explaining OO development to a newcomer is that of developing a program to mimic a person. Let’s pretend that you are writing software to model yourself as an object in an OO language. You can be broken down and defined by your Properties, Events and Methods. These PEMs define the way you look and act and what you can do. Below I have specific examples of each.

 

Properties

For example, some of the Properties you possess as a person are HairColor, EyeColor, Height, Weight, Age, ShoeSize and SkinColor. All of these Properties would have values such as HairColor = Brown, EyeColor = Green, Height = 71in. Just think if you could change your hair color by just changing the value of a property such as Myself.HairColor = Black. You can do that with hair coloring, but it’s just not the same. But imagine if you would that by changing that one property, your hair was instantly a different color. That’s the way that you can manipulate objects in object-oriented programming. Some properties are automatically updated for you such as your Age. Every year your Age property is increased by a value of 1. For example, on your birthday you may have a Birthday Event that would occur. Inside the Birthday event would be some code to update the value of your Age property. It would look something like Myself.Age = Myself.Age + 1. I’ll talk more about the dot referencing later. But when I put Myself in front of the property Age separated by a period, I am referencing the Age property associated with the object Myself. Not to get too complex here yet, but I want to mention that some Properties can be defined as Protected and therefore cannot be changed by other objects. A good example of this might be your Height property. Your hair stylist can easily change your HairColor property, but changing your height would be impossible. It’s protected by the object itself and can only be updated by itself.

Events

Now let’s take a look at some of the Events that you may have. Some of the inherent events that you have are itch, pain, touched and so on. For example let’s look at the Touched event. Imagine that your sitting at your desk working away and someone comes up behind you and taps you on the shoulder. Well guess what event fired? It was your Touched event. Now if you were modeling yourself in an OO world you would want to be able to respond to that event. So how would you accomplish this? Well you would add some code into your Touched event. Typically you would want to find out who tapped you on the shoulder. So, you would need to add some code that would tell yourself to turn around to see who it was. Now from here you could add more code to do all kinds of things like chasing the person down if they took off or speaking to the person to see what they wanted. You can let your imagination run a number of different scenarios.

Methods

In an OO world you can add new methods to your objects all the time. New methods are the same as adding new functionality. For example, you could add a new method to yourself that allows you to throw a Frisbee. When you were originally defined, you didn’t have this capability. So let’s add it to our object. This method is actually very complex in the sense that it would have to tell a lot of different parts of your body how to move in order to throw the Frisbee. It would have to tell all of your fingers how to grip the disc and then where to move your arm to as it began the throwing motion as well as how to accelerate your arm as the throw begins. You can see how this very simple task that almost everyone knows how to do is very complex if it was to be defined in a program. It’s very impressive as to how our brains are able to do all of this. It’s also very easy to see why it would take billions and billions of lines of code to design an android like Data on "Star Trek".

This first look at object-oriented programming hopefully makes things a little clearer as to what it means. First you, define the object that you want to create and then decide what Properties, Events and Methods that the object must have in order to perform its functions. A lot of objects will have some inherent behavior designed into them and that behavior can then be enhanced as its requirements change by adding new Methods and Properties. Our goal in OO is to design objects that are totally self-contained, which is known as encapsulation. We’ll cover more of these terms in the next article.

 

Rich Simpson is president of Mind’s Eye, Inc., a software development and IT consulting firm. He has a degree in aerospace engineering and has been designing and developing custom and commercial database applications since 1986. For more information or to download software demos visit their web site at http://www.mindseyeinc.com or send e-mail to rsimpson@mindseyeinc.com or call 636-282-2102.

 

 

Copyright © 1999-2008 Mind's Eye Inc.
Last modified: March 02, 2009