If you like this post then please subscribe to my full feed RSS. You can also subscribe by Email. Not sure how this works?

Remember when we first introduced the objects, we used the example of a car, which is an object. But the car belongs to a class of objects, the vehicles. This is what happens to the programming objects as well.

You should think of classes as general blueprints for the respective objects. In other words, the classes contain types of objects, in a very wide sense of the word. A class of objects has all the properties and all the methods of the object, but a class is not an object. You should think of the class as a sort of definition of the actual object.

After spending so much time to define objects and their properties and methods, it may seem a waste of time to think about classes as well. But the very first time you'll have to deal with a really big project, you'll be thankful that these classes were invented. Keep in mind that you won't see classes at work, they are just blueprints, you use the objects defined, not the class itself.

For defining a new class, you use the class statement:

class ExampleClass

When you need an instance of the class, you have to use the new operator:

var x: ExampleClass = new ExampleClass

Classes have fields, which are a lot like properties, methods and constructors. A constructor is a method with the same name as the class, which runs when an instance of the class is created, with the new operator.

By default, all the members of the class are publicly accessible, but you can also make certain fields protected or private. Private fields can only be accessed by the other members of the same class, while protected fields can only be accessed by other members of the same class or members of the derived classes. This is called encapsulation, and it is one of the most powerful features of the object-oriented programming. By using it, a piece of code can read and use variables from your class, but it can't modify them, and this may be vital in certain instances. Also, you can use encapsulation to constrain the class members to do something, or to prevent them from doing something. If you define the class correctly from the very beginning, you can save a lot of time later on, and considerably reduce the chance of errors and mistakes.

One other major advantage of using classes is that it allows breaking down the script into modules. In this way, several programmers or several teams of programmers can work on the same script, without having to coordinate themselves perfectly. This is also useful for smaller scripts - if you want to ad another module later on, or if the original programmer left and somebody else needs to take over.

Last but not least, the classes have the so-called "inheritance" characteristic. This means that you can create a class based on another one, using the keyword extends, like this:

class ExampleClass extends DerivedClass

Now, our original class, ExampleClass, is now called base class, while the new one is called the derived class, and it takes its functionality from the definition of the base class. This means that you use a derived class when you want it to have all the properties and methods of its base class, plus something else. Once again, this little trick helps you avoid writing long lines of code over and over again, when you need something to perform basically the same function, with minor adjustments.

Inheritance is a really powerful tool. You should think of it as a relation of a child who "inherits" features from his parents, but is not the identical copy of the parent. Also, you should consider that you can use the inheritance feature from one "generation" to another as many times you need, to create derived classes from ones that originally derived, and so on.

If, at this point, you find classes and inheritance a bit confusing, you should spend some time reading and deciphering some examples of scripts already made. You will need quite a lot of practice to use them properly, but keep in mind that this is the core structure of any object-oriented scripting, very popular and very much in demand these days. So, both in web programming and in classic programming you are sure to run into objects and classes again.

Did You Enjoy This Page? These icons link to social bookmarking sites where readers can share and discover new web pages.
  • del.icio.us
  • MisterWong
  • BlinkList
  • Furl
  • Netscape
  • Spurl
  • StumbleUpon
If you like this post then please subscribe to my full feed RSS. You can also subscribe by Email. Not sure how this works?

Leave a Reply