Article

ASP.NET 2.0: A Getting Started Guide

Page: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Next

Object Oriented Programming Concepts

VB and C# are modern programming languages that give you the tools to write structured, extensible, and maintainable code. The code can be separated into modules, each of which defines classes that can be imported and used in other modules. Both languages are relatively simple to get started with, yet they offer sophisticated features for writing complex, large-scale enterprise applications.

One of the reasons why these languages are so powerful is that they facilitate object oriented programming (OOP). In this section, we'll explain the fundamentals 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, we'll cover the following concepts:

  • objects
  • properties
  • methods
  • classes
  • scope
  • events
  • inheritance

In the pages that follow, we'll discuss these concepts briefly, and from Chapter 4 onwards, you'll 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, it's 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 behavior, 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 OOP's 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 objects?we can have objects that represent a car, a customer, a document, or a person. Each object has its own state and behavior.

It's 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 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 behavior--for 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.

Let's 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 objects--they're numeric, and we can perform mathematical operations on them--and 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:

Dim age As Integer          
Dim name As String          
Dim myCar as Car          
Dim myOtherCar as Car          
         
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 we've 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, we'll use as our example my dog, an Australian Shepherd named Rayne.

Rayne is your average great big, friendly, loving, playful mutt. You might describe him in terms of his physical properties: he's gray, white, brown, and black, stands roughly one-and-a-half feet high, and is about three feet long. You might also describe some methods to make him do things: he sits when he hears the command "Sit," lies down when he hears the command "Lie down," and comes when his name is called.

So, if we were to represent Rayne in an OOP program, we'd probably start by creating a class called Dog. A class describes how certain types of objects look from a programming point of view. When we define a class, we must define the following two items:

  • Properties - Properties hold specific information relevant to that class of object. You can think of properties as characteristics of the objects that they represent. Our Dog class might have properties such as Color, Height, and Length.
  • Methods - Methods are actions that objects of the class can be told to perform. Methods are subroutines (if they don't return a value) or functions (if they do) that are specific to a given class. So the Dog class could have methods such as Sit and LieDown.

Once we've defined a class, we can write code that creates objects of that class, using the class a little like a template. This means that objects of a particular class expose (or make available) the methods and properties defined by that class. So, we might create an instance of our Dog class called rayne, set its properties accordingly, and use the methods defined by the class to interact with Rayne, as shown in Figure 3.6.

1556_fig-rayne
Figure 3.6. An instance of Dog

This is just a simple example to help you visualize what OOP is all about. In the next few sections, we'll cover properties and methods in greater detail, and talk about classes and class instances, scope, events, and inheritance.

Properties

As we've seen, properties are characteristics shared by all objects of a particular class. In the case of our example, the following properties might be used to describe any given dog:

  • color
  • height
  • length

In the same way, the more useful ASP.NET Button class exposes properties including:

  • Width
  • Height
  • ID
  • Text
  • ForeColor
  • BackColor

Unfortunately for me, if I get sick of Rayne's color, I can't change it in real life. However, if Rayne was a .NET object, we could change any of his properties in the same way that we set variables (although a property can be read-only or write-only). For instance, we could make him brown very easily:

rayne.Color = "Brown"          
         
rayne.Color = "Brown";

In this example, we're using an instance of our Dog class called rayne. We use the dot operator (.) to access the property Color that the object exposes and set it to the string "Brown".

Methods

With our dog example, we can make a particular dog do things by calling commands. If I want Rayne to sit, I tell him to sit. If I want Rayne to lie down, I tell him to lie down. In object oriented terms, I tell him what I want him to do by calling a predefined command or method, and an action results. For example, to make Rayne sit, we would use the following code to call his Sit method:

rayne.Sit()          
         
rayne.Sit();

Given that rayne is an instance of our Dog class, we say that the Sit method is exposed by the Dog class.

Classes

You can think of a class as a template for building as many objects of a particular type as you like. When you create an instance of a class, you are creating an object of that class, and the new object has all the characteristics and behaviors (properties and methods) defined by the class.

In our dog example, rayne was an instance of the Dog class, as Figure 3.6 illustrated. In our code, we'd create a new instance of the Dog class called rayne, as shown below:

Dim rayne As New Dog()          
         
Dog rayne = new Dog();

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links