mentals of OOP and learn how adopting good OOP style now can help you to
develop better, more versatile web applications down the road. This section will
provide a basic OOP foundation angled towards the web developer. In particular,
well cover the following concepts:
objects
properties
methods
classes
scope
events
inheritance
In the pages that follow, well discuss these concepts briefly, and from Chapter 4
onwards, youll see some practical examples of OOP in action.
Objects and Classes
So what does the term object oriented programming mean? Basically, as the
name suggests, its an approach to development that puts objects at the center
of the programming model. The object is probably the most important concept
in the world of OOP; an object is a self-contained entity that has state and beha-
vior, just like a real-world object.
In programming, an object's state is described by its fields and properties, while
its behavior is defined by its methods and events. An important part of OOPs
strength comes from the natural way it allows programmers to conceive and
design their applications.
We often use objects in our programs to describe real-world objectswe can have
objects that represent a car, a customer, a document, or a person. Each object
has its own state and behavior.
Its very important to have a clear understanding of the difference between a
class and an object. A class acts like a blueprint for the object, while an object
represents an instance of the class. I just said that you could have objects of type
77
Objects and Classes
Car, for example. If you did, Car would be the class, or the type, and we could
create as many Car objects as we wanted, calling them myCar, johnsCar, davesCar,
and so on.
The class defines the behavior of all objects of that type. So all objects of type
Car will have the same behaviorfor example, the ability to change gear. However,
each individual Car object may be in a different gear at any particular time; thus,
each object has its own particular state.
Lets take another example: think of Integer (or int) as a class, and age and
height as objects of type Integer. The class defines the behavior of the ob-
jectstheyre numeric, and we can perform mathematical operations on
themand the instances of objects (age and height) have their behavior defined
by the class to which they belong, but they also hold state (so age could be 20).
Take a look at the following code:
Visual Basic
Dim age As Integer
Dim name As String
Dim myCar as Car
Dim myOtherCar as Car
C#
int age;
string name;
Car myCar;
Car myOtherCar;
As you can see, the syntax for declaring an object is the same as that for declaring
a simple integer or string variable. In C#, we first mention the type of the object,
then we name that particular instance. In VB, we use the Dim keyword.
Object oriented programming sounds like an advanced topic, but getting started
with it is actually very easy, because OOP offers us a natural way to conceive and
design programs. Instead of writing long functions of code to perform specific
tasks, OOP allows us to group pieces of related functionality into classes that we
can reuse over and over, or even extend to incorporate new features. In OOP,
one thinks of programming problems in terms of objects, properties, and methods.
And, as weve seen, the best way to get a handle on these terms is to consider a
real-world object and imagine how it might be represented in an OOP program.
For the examples that follow, well use as our example my dog, an Australian
Shepherd named Rayne.
78
Chapter 3: VB and C# Programming Basics

Get Build Your Own ASP.NET 2.0 Web Site Using C# & VB, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.