Classes

Classes is a cornerstone in object oriented programming, and is often made into something difficult to understand. That is sad, because if you get a hold o the concept it is a lot easier to get your ideas onto paper and finally realized in a program. Classes er often a noun. "Hey, that is just what you said variables was?!" you may think. That is right, variables and and classes are both (for the most part) described in a noun. Variables are the nouns that represent simple units of data. Classes are the nouns that relates to objects.

I am a person, and you are a person. We are surrounded by animals, buildings, plants and so on. If that is all we have to deal with, variables are good containers of that information. Here you can treat person as a simple unit, that relates to the other units on the same "level". That could produce something like (in processing):

Person karsten = new Person();
Animal jackie = new Animal();
Tree elm = new Tree();

This will be sufficient when trying to differentiate karsten from jackie and place both in a larger context. If you have to focus on person as an object, you will be able to dig out a second level of variables, like eye color, age, name etc. that characterize the person. In addition to that, there may be actions you want this person to perform like sleep, greet, eat, dance, jump etc. When a noun have this extra layer of complexibility and is thought of as an object ... there you have - a class.

Now, that you can dig down and extract classes from your thoughts in a process like this: "I want to make a game with a mole that shoots dirt at rabbits. What have we here. as objects? A mole, a piece of dirt and a rabbit!" There may be more rabbits in the game, and the single pieces of dirt may be piled up, but the originates from the same fundamental class, describing the functionality of the generated object. Let's look into that.

What is a class?

A class is a self containing piece of code, that describes an object in two major ways: What it has, and what it does. If we take the person as an example, it has an age, an eye color, a name, a language etc. These are the properties of the class. Properties are passive descriptions that can characterize en instance of it. If the person class can have a property called age, it can be assigned different values for each instance of it. "Hey, THAT is just like a variable?", you may think ... again. And you are absolutely right. Properties in a class is just variables placed in a context. They are declared the same way and treated the same way. There is some spefict rules and possibilities when it comes to the scope of properties, but in general they are alike.

If we look at the person again, we can teach it things it should do. It could be greet, eat and sleep. All are actions the the object can perform. The more we teach it, the more complex the object gets. These are known as the methods of the class. They encapsulate several statements into one single call. They can take arguments and deliver a result back to the caller, based on it's internal process. "Hey, that is just like a function?!", you may think ... again - again. Yep, methods are functions placed in the context of a class.

Class Members

That sums up to the fact that a class property is a variable defined inside a class, and a class method is a function defined inside a class. They are referred to as class members, and is breathing life into the objects. When you design your program, you will have to articulate what the individual objects responsibility is, and how they relate to other objects. If you can do that, it is actually just a matter of transforming that description into code, and you are well under way.

Access modifiers

When describing an object in a class, there may be properties and methods you wan't to keep away from the public, so you just have the relevant information exposed to the outer world. If you make a vehicle class. You may have a method that puts gas in the car exposed to other, but not the method that calculates and subtracts a specific amount of gas, based on speed and weight. You may have a method to tell it to turn right, but not the inner methods to rotate wheels and angle of the car. There are private methods and public methods. The following is the available access modifiers:

  • Public
  • Protected
  • Internal
  • Private

To understand these access modifiers you should know that classes can work with other classes in a grid of interconnected hierarchies. Classes can be a subclass of another class. Classes can be in a package with other classes, and these packages can be nested inside other packages.

  • Writing public means that the class or class member is available to every one instantiating it.
  • Writing protected means that the class or class member is available to other classes in the same package or other packages nested inside the package of the class.
  • Writing internal (or nothing) means that the class and its members only can be accessed from other classes in the same package.
  • Writing private makes it private. The members and it's class cannot be accessed from the outside.

As you can see, the access goes from everyone to just the class itself. It may be tempting to just go public and let everyone access everything, but it also exposes the inner workings and the following problems arise. Other classes can tamper the data and enter data in the other class in an uncontrolled way. This problem it tackled with a concept called getters and setters, that you will look at shortly. The other problem is, that the functionality of the class is cluttered with unnecessary methods and properties, making it hard to figure out, what the responsibility of the class is. When the right set of class members is exposed to the outside world, they are often referred to as it's interace or the class API (Application Programming Interface).

The static modifier

We have to discuss one more modifier, the static modifier. When writing static to a class member, makes the member relate to the class itself instead of any instance of it. When a class has a static member, you access it via the class name. The Math class is one of the more popular classes with static methods. Math.random() returns a random number between 0 and 1, and Math.PI returns the value of PI. Neither of these refers to a specific instance of the class.

Is this for everyone?

Classes are widely known by programming languages. Not all languages known every aspect of classes, but the fundamental thought behind classes and object oriented programming can be applied to most languages. There is one exception and that is JavaScript. This language wasn't build to cope with these more advanced programming design patterns, so they don't have classes, packages and all the modifiers. Instead it uses a set of tricks to emulate the same concepts, if they are needed.

It is important to remember, that you may not need to utilize classes at all. Some languages demands that everything is based on classes, but languages like Processing, JavaScript and ActionScript may be well served with a set of variables and functions, if the complexity isn't overwhelming.