ThePlace

Home ] Search ] Resources ] Site Map ] Contact Me ]
Dave's Information Technology Resource

Up ]

Computers & Programming ] Operating Systems ] Programming Environments ] Windows-Based Applications ] [ Objects and Programming ] Data and Variables ] Operational Expressions ] Functions and Procedures ] Programming Control ] Algorithms ] Files, Graphics & Databases ] Software Development ] 10 Things... ]

--- Objects and Programming ---

Background

Object Oriented Programming (OOP) is based on the packaging together the behavior (methods and events) of an entity with its identity and state information (properties).  The package is called an object.  

Here are some things to know about objects and object-oriented programming:

bulletClasses: the basic blueprint of an object that defines the data and features.  Objects are the basic entities of a object-oriented programming system.
bulletObjects typically have name (identity) and state.
bulletObjects have 2 parts:
  1. Fields (also referred to as "state" and "data") - things that describe an object - including the object name.  Note that the terms variables and properties are often used interchangeably with fields.
  2. Methods (or behaviors) that describe how an object can be used or what it can do.
bulletObject data and methods can be:
  1. Private: can only be seen within the object.
  2. Public: visible to the outside world.
bulletEncapsulation: means that you "contain" all of the properties and behaviors  that characterize an object.  It enables you to protect the object from outside tampering by only exposing the things needed to control behavior or properties.  It manages errors internally through validation of the exposed interface.  Finally, it frees the user from having to know how the object works or the specific details of how it is implemented.
bulletAbstraction: this is the "real" basis of objects, you can "expose" essential features while hiding the background details and explanation of how things work.
bulletPolymorphism:  the ability to treat different objects as if they were the same (i.e., use the same code).  Another way to consider this is that objects can often have multiple methods that are determined by the arguments they are given.  Bottom line, the object is treated as a black-box that has an interface and the objects or code do not have to know how the object works.  
bulletOverloading: closely related to polymorphism is the concept of overloading in which objects take on more than one meaning or use.  This may include the ability to deal with multiple data types with a single operator.
bulletInheritance: Allows objects to share their interfaces in a hierarchical fashion.  In "child" objects, they may carry some or all of the behaviors and properties and add new features as needed. 

Let's look at an example of how objects can be used to describe a "real-world" object:

Class the basic description (blueprint) of an object Automobile
Object
an instance of something derived from the class Trailblazer Automobile
Fields (data)
also: properties and variables
features that describe the object
bulletname = "TrailBlazer"
bulletmanufacturer = "Chevrolet"
bulletlength = 191.8 (inches)
bulletcolor = "white"
Methods
(aka behaviors)
things the object does (behaviors)
bulletstartup
bullettow
bulletcruise-control (speed)
bulletturn (direction)
Encapsulation the packaging of the object to completely define it's structure and operation All of the fields that describe the vehicle physically AND all of the methods that describe what a vehicle can do.
Abstraction exposes essential features, while hiding underlying structure and operation
bulletexposed: fuel remaining
bullethidden: how fuel is consumed based on temperature, speed, road grade, etc.

 

Inheritance data/methods that are granted by parent that can be modified or overridden Automobiles have length, weight, color, driving behavior etc. which is the parent of the Trailblazer (actually an intermediary called "SUV" might be the direct parent of the "TrailBlazer").

The TrailBlazer actually inherits Automobile data and methods and adds the off-road method.

Polymorphism the object can take on multiple roles based on the arguments it is given  The "accelerate" method will behave differently depending on initial speed (from stopped to say 90 mph) and amount of acceleration desired.  In addition, you may change how specific properties or methods work.

Okay, so I can describe a Chevy TrailBlazer for a computer - now what?  The description of the "TrailBlazer" object can be used to...

bulletImplement a sales application (e.g., keep information about various vehicles and what they are capable of).
bulletCreate a vehicle simulation program to test engineering modifications.
bulletCreate a driving simulator program (e.g., an arcade game) with various vehicles.

Object oriented technology is the foundation of programming tools such as Visual Basic (VB).  VB depend on the implementation of windows, buttons, and other objects as "objects" to define and build a user interface.  In addition, developers can create new objects to extend the environment and build complex applications.

Now that we have covered the requisite techno-babble of object technology, let's look at how it is used in a programming environment such as Visual Basic.

 

Objects and Programming

The windows environment itself is a key aspect of object-oriented programming systems.  Windows-based programs operate by displaying objects such as windows, buttons, and text boxes on the screen for collecting and presenting data and information.  These objects are the heart of programming development and delivery systems such as Visual Basic and JavaScript.

Object-orientated design and development using tools such as C++, Java, and even Visual Basic has become the de facto standard approach for developing and delivering windows applications (not too mention enterprise and web applications as well).  It is based on developing software programs using and manipulating objects that often represent "real-world" objects.  For example, a double-column bookkeeping ledger can be presented as a grid in a program (a spreadsheet), which can contain data.  The data, in turn, can be manipulated in much the same way as the paper ledger itself.

Advantages of object-oriented programs are:

bullet

Objects, via class descriptions, can be used to model real-world things such as pad of paper, a double column bookkeeping ledger, or other real world objects that do work.

bullet

Modeling can include various aspects of behavior, including the ability to store data and information.

bullet

Objects can be used to build other objects.

bullet

Objects can share behavior (how they work).

bullet

Detailed aspects of object operation can be hidden so that the developer can focus on building complex applications.

Object oriented software development is a complex and exciting aspect of computer science.  It is much more than the brief discussion we are providing here, however, there are some aspects you need to understand if you want to develop windows-based software applications using tools such as Visual Basic and JavaScript.

 

Programming with Objects (Object-Based Programming)

Please note: much of the following discussion is oriented towards Visual Basic.

Object based programming is technically an "application" of object oriented programming.  Programming tools such as Visual Basic and JavaScript take advantage of "windows" program objects that support the window interface paradigm (forms, buttons, text boxes, etc.).  

These languages may also go further by allowing the creation and use of new objects in object-oriented programs.

Program objects are components that can be used to build an application.  They include:

bullet

 Windows/Forms

bullet

Buttons

bullet

Text boxes

bullet

List and drop-down combo boxes  

bullet

Picture and image containers  

The following form is from Visual Basic and illustrates some of the objects that can be used to create an application...

 

 

Just about everything on a windows-based screen is an object.  Some objects, such as windows or forms, contain other objects.  Objects are usually identified by unique names.  For example:

bullet

"myform"—for a window

bullet

"gobutton"—for a button in a window

When referring to objects within objects, a dot-notation is used.  For example: "myform.gobutton" refers to the button in the "myform" window.

There are some basic features of window-based programming objects that are important to understand:

bullet

Properties (data/fields) - what they are

bullet

Methods - what they can do

bullet

Events - what can happen to them

 

Properties

Objects are described with "properties" (data) which tell you what they look like, where they go, how they will act, and what they contain.  Properties often describe physical properties of the object (color or size), but they can also describe behavior, and the data contained by an object.

Examples of properties include:

bullet

Appearance—color

bullet

Behavior—dragging, visibility

bullet

Position—top, left, width, height

bullet

Content—text, graphics

bullet

Font—name, size, bold, italic

bullet

Value—data stored by the object

  The following example shows the property list for the "lastname" text box...

 

Property descriptions such as "height" or "value" are usually referenced by dot-notation.  For example:

bullet

gobutton.height = 25    indicates that the height of a button is 25 pixels

bullet

textbox.text = "Hello World"   indicates that the string "Hello World" is stored in the property value of a text box.

 

Events

The secret to how most windows-based programs lies in behaviors called "events".  Events are triggered when windows are "loaded," buttons are "clicked," or text boxes are "changed."  These events drive the program.

Objects can respond to stimuli or things that are done to them.  For example:

bullet

Click, Double Click

bullet

Change

bullet

Mouse Down, Over, Up

bullet

Drag

The following example from Visual Basic shows the event list for the push button... 

 

In many programming systems such as Visual Basic and JavaScript, objects are associated with behavior by appending a descriptive behavioral term to the name of the object.  For example: "gobutton_click" indicating that the button has been clicked.  In some cases, certain events such as "onclick" are explicitly associated with the object, a button.

From a programming perspective, events are where the work is done.  Code is attached to events to drive a program.  For example:

Sub command1_click ()

  Text1.text = "Hello World"

End Sub

In this example:

bullet

There are, at least, 2 objects (button and text box) and the window.

bullet

The event is triggered when the user clicks the button,

bullet

Clicking the button (an event) triggers the command1_click subroutine,

bullet

Which contains code for updating the text box.

Code written for events may be used to call other procedures, change properties, or invoke methods. 

Methods

Objects usually have many things that they can do or that can be done to them.  These are predefined procedures that often modify properties of the object such as its location.  For example:

bullet

Move—change the location of an object

bullet

Drag–allow it to be dragged with by the mouse

bullet

Paint—display or redisplay the object

bullet

Draw (Point, Line)—use the object to draw (e.g., use a mouse) or draw with the object (e.g., a picture object)

In many cases, methods can apply to different objects and perform approximately the same function on all of them.

In the following example, the text1.move will cause the text box to move and be resized when the button is clicked...


In many cases, values are passed to methods that change property values.  For example, the "move" method in Visual Basic is given values for the left and top position as well as height and width. The properties for left, top, width, and height are changed by the "move" method. 

Methods are usually referenced by the following notation: "mybox.move 10,10,200,200" which invokes the move method for "mybox" to the left and top positions of 10 and a width and height of 200.

Collections

Another feature of some objects is the ability to contain other objects.  For example, a database table (an object) consists of a collection of records (also objects) or a form includes a collection of subordinate objects.

Just remember:

bulletThe elements of the collection usually have unique names/identifiers.
bulletThe elements can be referred to as individuals (children) or from the parent perspective.

 

Referencing Objects in Applications

Every object has a unique name, which can be linked to properties and methods.  For example:

bullet

A button object has a name: cmdbutton

bullet

A property of the button is width, you would reference it as: cmdbutton.width

bullet

 A button can be moved with the "move method," follows: cmdbutton.move 10,20 (move cmdbutton to position left-10, from top-20)

In some cases, objects may be contained within other objects.  For example, a form, named myform, contains cmdbutton.  If cmdbutton is referenced from outside of myform, it is referred to as myform.cmdbutton.property.  This strategy is used extensively in JavaScript to refer to objects on web forms, for example:

document.webform.tbox.value

This name refers to the value found in the text box in a form in a browser document.

Events, in the Visual Basic environment, are linked to objects with the "_".  For example, in Visual Basic, the cmdbutton click event would look like this: cmdbutton_click.  This technique will vary in other environments such as JavaScript where the "onclick" event is explicitly described when the object is described.  For example:

<input type="button" VALUE="Click to add =" NAME="button1" OnClick="addnum()">

The button has an "OnClick" event that invokes the "addnum" function.

 

Why Is Object-based Programming So Powerful

The primary advantage of object-based programming systems is the speed and ease in which you can design and build an application.  The benefit of windows-based programming is the consistency that is found in how programs look, feel, and behave. 

In some cases, a technique called rapid prototyping is applied in which programs are constructed very quickly to understand what needs to be done.  These programs are usually discarded when the final program is constructed.

In fact, you can quickly design an application by "drawing" it.  This allows you to think about the program before you actually sit down to create it.  There are various other techniques for describing how a program works such as flow charts that you may already have heard of.

The user interface is built by placing objects on the screen that the end user will interact with to accomplish the task of the program.  These might include buttons, text boxes, and list boxes.  Once the user interface is built, code is attached to the objects to enable the program to perform its work.

As the programmer, you focus on what the program needs to do and not on how buttons or boxes are drawn on the screen during program execution.  The programming tool and the delivery environment (operating system) take care of how the buttons work during program execution (when you click them).

 

Key Things to Remember about Objects and Windows Programming

Note: the following comments refer directly to tools such as Visual Basic and are not intended as a comprehensive discussion on object oriented programming.

bullet

Objects are the basis of windows-based programming—they are forms (often called windows), buttons, text boxes, and other things that make up the physical or visual presentation of the program.

bullet

Objects have properties that are used to describe physical qualities, behavioral characteristics, and content.

bullet

Objects have associated behaviors called events to get things done.  These events include clicking, dragging, and other actions.

bullet

Events contain code, which, when triggered, may call procedures, change properties, or invoke methods.

bullet

Methods are things that can be done to or with an object.

 

 

Home ] Up ] Computer Architecture ] Database Bootcamp ] Visual BasicS ] Web Basics ] Web Multimedia ] Web Programming ] Advanced Web Topics ] Developing Web Sites ] XML Technology ] Web Glossary ]

Copyright © 1999 - 2005 
ThePlace - Written and Sponsored by Dave Hillman