Object Oriented Programming: Getting Started

Lots of people don’t seem to fully understand Object Oriented programming, so I decided to write this series where I would do my best to explain it in a simple fashion that’s suitable for both programming beginners and people who are just getting started with Object Oriented (OO) programming. I’ll assume for these posts that you can at least understand the syntax of Java/C#, but I’ll explain the OO concepts behind things.

What is OO Programming?

OO programming is a paradigm based on the idea of objects. I’ll go on to explain more about objects (and classes), but for now it’s enough to know that objects have two parts: attributes and methods. An attribute is a piece of data about the object, often called a property, and a method is a block of code that does something. I like using examples when I’m explaining things as I think it makes things a lot clearer, so let’s consider we’re wanting to make an object to represent something in real life – say a dog.

For our dog object, we need attributes and we need methods. Think of the attributes as things the dog has and the methods as the things the dog does. In this case, attributes could be things like Colour, Name and Age. The methods for the dog could be eatFood, wagTail and walk.

Let’s talk about Classes

In an OO programming language, like Java, C# or Python, we don’t “write” objects. We write classes. The relationship between objects and classes is that an object is an instance of a class.

An object is an instance of a class.

That’s important, so remember that. But what does it actually mean? Let’s continue our dog example by writing some code for it. The listing below shows the Java code for a Dog class.

So now we have a class, that represents a dog. But it doesn’t represent a specific dog – there’s no values for it’s attributes for example. Think of a class as the blueprint for a dog, and if we want to represent a specific dog, we’ll need to create an instance of this class – we’ll need to create an object.

As simple as that, we’ve created a new dog. It’s sat on the Java heap in memory, and we’ve set some attributes on it as well. That means if we were to call the walk() method on our aNewDog object, it would be Fido that went for a walk. It doesn’t make sense to call the walk() method on the Dog class because that’s not actually a dog – it’s the blueprint for a dog.

Constructors

You might have noticed that if we have lots of dogs to create, then it’s a bit of a tedious process – 4 lines of code to create a new dog and give it a name, colour and age is going to get a bit tiring if we have more than a few dogs. This is where constructors come in handy. If we add a constructor for our Dog class, it’ll look like this:

This means that now, we can create a new Dog, and set all of its attributes, with a single line of code, which is obviously much cleaner.

Dog anotherNewDog = new Dog("Rex", "Black", 12);

If we don’t specify a constructor, then the compiler generates a default no-arguments constructor for us that just creates a new, empty dog. This is what happened when we created the first Dog object.

So now, you should understand the relationship between classes and objects, how to create new objects and how you can use attributes and methods to represent a thing. There’s one final part that I’ll cover, although you can skip this part for now if you’re struggling, it’s handy to know about.

Static Classes and Attributes

For some things, making new instances of the class might not make sense. Maybe you have an API class that’s just methods to fetch data from a web service, so creating instances of it doesn’t make sense to do. In this case, we can make the API a static class. You cant instantiate (create an object of) a static class, you just use it as-is. Let’s take another example. In a static class, all of our attributes and methods have to be marked as static and it obviously doesn’t make sense to have a constructor in a static class, so we can’t.

In order to use the fetchData() method, since we have a static class, we can just call it directly like:

private String data = API.fetchData();

Using the static keyword means that there is only one “copy” of the class or the attribute. For example, we might decide that we want to keep track of the number of dogs that have been created. Let’s add that functionality to the dog class using a static attribute numberOfDogs, and adding functionality to increase that count every time a new Dog is created.

Let’s consider what we need to do for this. We need some way to keep track of the number we’re counting, so let’s add a static attribute to the Dog class. By making it static, we tell the computer that all of the instances of Dog share this same attribute – if we update it for one instance, it is updated for all others because there really is just one copy that all of the Dog instances point to.

public static int numberOfDogs;

Next, we need a way to actually increment this count. We can update the constructor to add 1 to this count every time we use it to make a new Dog:

We’re almost done – but we saw earlier that there’s actually 2 ways to create a new dog. What if we just created an empty Dog with no attributes set using the default, no-arguments constructor? We can override (think “customise” for the moment) this default constructor to add our own behaviour. So to the dog class, we want to add:

public Dog(){
this.numberOfDogs++;
}

So now, regardless of how we create a Dog, it will update the count when a new one is created. We can get the attribute numberOfDogs from any Dog object, regardless of when or how it was created, and we’ll get the up to date count of how many dogs we have. You can write some test code to verify that yourself, play about with static attributes and see how they work.