What is Object-Oriented Programming? What are Objects?

Jose Salvatierra
School of Code
Published in
3 min readFeb 9, 2016

Imagine we are making a program for a veterinary clinic which deals with cats and dogs. We will therefore be storing data related to cats and dogs.

An option for our application would be to just use a database, with no program written. Every time one of the cats or dogs in the clinic has some food, we would go into the database and update their record with the time they last ate.

This means that the doctors in the clinic would need to have access to the database, and would need to know how to update the database with the new data. We can’t really expect that, so we agree that we need a program written that has some sort of user interface. In this user interface we could load one of the dogs or cats, which would bring up some information about him/her, including the time they last had food.

While we develop our program, remember that each one of these animals is just a row with some data in a database somewhere.

Lets say both dogs and cats have the same properties (for now, lets say they have a name, a colour, an age, and a time when they were last fed).

In order to make our program understand that the data it is dealing with has these properties, we create a Class which has these properties. It could look something like this:

public class Animal {
public String name;
public String color;
public int age;
public long timeSinceLastFed;
}

Notice this isn’t one specific cat or dog, like “Hudson” or “Gabby”, but this is us just telling our program that this is the thing it is going to be working with.

Now, lets load the information for “Hudson”, one of the cats, into our program:

... // Some part of the program above
Animal cat_one = new Animal();
... // The program continues

We are telling our program “hey, this thing you understand called cat_one is an Animal! You know it has these four properties: name, color, age, timeSinceLastFed”.

And then our program says “cool I got it.”

But notice that at no point did we tell our cat_one that his name is “Hudson”, or how old he is, or when he was last fed, or anything. The program knows it has the properties, but it does not know the value for the properties.

That’s what a Class does: define that the properties exist.

Our object, cat_one, now should define the values for this specific cat. We can do this with a constructor, which in Java is called as soon as we create an object using the new keyword.

public class Animal {
public String name;
public String color;
public int age;
public long timeSinceLastFed;
public Animal(n, c, a, time) {
name = n;
color = c;
age = a;
timeSinceLastFed = time;
}
}

So now we’ve got this constructor, we can tell our program “we’re going to tell you that this cat exists, and also give you its properties so you can use them”.

... // Some part of the program above
Animal cat_one = new Animal("Hudson", "black", 3, 3600);
... // The program continues

Now the program knows cat_one.name is “Hudson”. But we can create many objects, not just one! They all have the same properties, but can have different values for them.

... // Some part of the program above
Animal cat_one = new Animal("Hudson", "black", 3, 3600);
Animal cat_two = new Animal("Gabby", "brown", 5, 7300);
Animal dog_one = new Animal("Fred", "white", 2, 6700);
Animal dog_two = new Animal("Samuel", "black", 1, 3000);
... // The program continues

So there we’ve got four objects, they all have different values, but we only had to define what their properties are (note: not their values) once, when we defined the Class.

Our program now has the objects, and we’ve understood what objects are in programming.

Next, we’ll be looking at what ‘static’ is and how we can apply it in this example!

--

--