This is a free tutorial to help beginning programmers get up to speed using Microsoft Visual Basic 2010 Express. To get the most from this tutorial, you might want to start at the beginning. A complete index to the course is also in the front of the first lesson.
Part 1 - A "From the Ground Up" Tutorial - An introduction to the course.
In part 5 of the tutorial, I introduced the idea of software objects and the Hello World program was upgraded to object status. But objects are so important that all of this part will be devoted to them. We'll look at the concept of an object, what an object is in VB.NET, and show you the initial version of the Signature Block program. The object we code here won't be perfect, but I'll explain the problems and we'll code a better one in a later segment.
Before we look at a software object, let's look at objects in the real world. The idea of software objects has such a natural parallel in everyday life that thinking about the ways that real objects and software objects are the same helps to understand the software.
"Software objects" and "objects" in everyday life are remarkably similar. In fact, the biggest difference is that you don't have to write detailed and correct program statements to use objects in everyday life.
Take any object - your computer, a book, your dog, the clothes you're wearing, a TV ... anything ... and you can describe it in terms of:
- what it is
- what it will do
Computer scientists call this:
- state
- behavior
In Visual Basic .NET and other languages it's called:
- properties
- methods
We'll use the last set of terms - the ones for VB.NET - because that's what we're studying here. The overall structure of objects in VB.NET is shown in the illustration below.
--------
Click Here to display the illustration
Click the Back button on your browser to return
--------
Real World Objects and Software Objects
Consider a TV as an object. Here are some properties a TV has:
- screen size
- flat panel or tube
- NTSC, SECAM, or PAL
(broadcast standards)
Here are some methods:
- turn on/off
- mute the sound
- change the channel
In the same way, a software object, like a Timer, can have:
- A property: The timing Interval
- A method: Start or Stop
What makes code OOP?
In part 5, these characteristics of OOP software were introduced:
- Inheritance
- Polymorphism
- Encapsulation
Let's see how they apply to physical objects.
The idea of inheritance is a natural for TV's. Factories manufacture TV's with very specific features by model. In VB.NET, software objects are programmed as a Class and you can think of the manufacturer's model specifications as being a like a Class. A Class tells the program how to create an instance of the software object and model spec's tell the factory how to manufacture exact copies of TV's. So each actual TV is an instance of that model.
You can design a new model TV by "inheriting" most of the specifications from an earlier one and then changing a few features such as a larger screen. (And a larger price!). Because the manufacturer needs to keep track of these things, the new model would get it's own model number even though it's mostly the same as a previous one. In the same way, you can usually "inherit" most of a software object and then change a few things to create a new object.
Polymorphism is easy to demonstrate for TV's too. Polymorphism means that the object will do different things depending on what kinds of inputs are received. When the signal being received by the TV is the wireless remote, then the channel, volume, or something like that will change. When the signal being received is from the cable, then the TV turns it into sound and pictures on the screen. Different inputs create different results. With software, if you send an object a number followed by text, it will do different things than when you send it text followed by a number. (If those inputs are defined for that object. The parallel here is that your TV won't respond to your garage door opener either.)
The TV also demonstrates encapsulation very well. This is the "black box" effect. In words, it means that you don't care how the TV turns DVD's and broadcasts into shows for you to watch. You only care that it does the job reliably and you can control the process. A lot of people have no idea how a TV works and they don't care. That's encapsulation.
The benefits of OOP are hard for beginners to appreciate because they become really valuable only when systems become more complex. With very simple programs, they often seem like they're more trouble than they're worth. But it's fair to say that the amazing sophistication of software wonders like the .NET Framework are only possible because OOP was used to program them.
On the next page, we look at just some of the major reasons why OOP is better.
