Why do we need objects in programming

Object-oriented programming I

In this article on object-oriented programming, we will explain to you in a first attempt how to clearly describe objects using programming.

  • Repeat: what is an object?
    in the text
  • Methods - essential for running a program
    in the text
  • Object Oriented Programming - Illustration
    in the text
  • in the text

Repeat: what is an object?

To do this, we first repeat the term "object". One object could be a tennis player. But the tennis ball or the racket can also be called an object.

It is interesting, however, that there are also objects in programming that are not immediately recognized as such. For example, if you write object-oriented software, you will find that it is often difficult to determine exactly what you are packaging as objects. But that's not so important in programming. Because an object only needs an identity, a state and a behavior.

The tennis player, for example, has his own identity. That he is athletic could be described as his condition. And his current behavior is that he plays tennis.

In the end, everything that fulfills these three properties can be described in a software object - even if in reality it would not be described as an object. But this is no longer something real, but something virtual. The only real thing about the software object is the memory it occupies in the computer. Processors, bus systems, video cards and so on exist only to work with this memory and to display its contents.

You can see the mapping of an object as follows: You describe an object using variables and methods. Variables are available as status information. Methods work as a guide to the behavior of the object. For example, the variables could be “color”, “position” and “size”. The methods for this are called "move ()" and "colorAendern ()". With the help of the status information you can influence the object.

Methods - essential for running a program

After the written program has been compiled for the object, it is generated in the main memory during runtime. After it has been processed by the processor, it begins using your methods to serve its intended purpose.

As you have probably noticed, methods are very important to programs. Because they ensure that these exist and do something at all. The same goes for objects, by the way, since they mostly consist of methods. The more diverse and complex the methods, the more real the object looks.

Without methods, there wouldn't even be a program. Because the first method that also starts the program is the “main ()” method. She alone is responsible for what the program ultimately does. This means that it can carry out all the necessary instructions if it is a small application. In a larger application, the main method creates objects so that it can use their methods.

So if we want to describe an object, we first have to write a class for it. An object of this class is only created when the “new” operator is used together with the class name. Unlike declaration and initialization, which relates to variables, the creation of an object is generally called "instantiation".

But be careful! Just because a variable refers to an object does not automatically mean that that object also exists. For example, the variable can only refer to the address of an object that does not yet exist.

What does that mean? Imagine you are declaring a variable that designates an object.

However, this object does not yet exist. It is just a placeholder that does not yet refer to an object.

The object only exists through the new operator and the variable can be used.

Object Oriented Programming - Illustration

If you are already familiar with objects, you probably know that classes make Java programming a lot easier. As a developer it is often a bit more difficult. Because at the beginning you always have to make a plan of how the class, and thus the object, should look.

You can imagine it like this: You are just about to bake muffins. The dough is your memory, the baking pan is your class and the muffins are your objects. Makes sense right? This means that when an object is created, the class takes as much memory from main memory as it needs to form the desired object.

Now you could say that it would be an unnecessary repetition to form several muffins with the same baking pan. But are the muffins really all the same? Every muffin, i.e. every object, has its own piece of memory, i.e. its own identity. Different sugar decorations can distinguish the muffin from the others in the same baking pan. Translated, this means that different values ​​of the variables change the state of the object and thus differ from the others. This ultimately also influences the behavior of the object.

If a class is represented as a baking pan, doesn't it exist? Yes she does. Just as there are muffins, there is also the baking pan. But both are different from each other. Because while the baking pan is made of steel and has sharp edges, the muffins are rather soft and made of batter. A baking pan has features that cannot be shared with the muffins.

Static methods

In Java, characteristics of the class definition, i.e. the content of a particular class that is not shared by its objects, are called “static”. There is only one definition for a particular class. So if a method of a class is static, then this method applies globally to all objects that are created. Static methods can be called via the class itself. Here is an example:

As you can see in the 3rd and 5th lines, the variable and the method were described as static. This allows us to initialize the integer "var" in the 14th line of the main method for each object in general. To do this, we use a method that takes as a parameter the value that var should take on in the class.

We test that everything went well by creating two objects from the 16th line onwards and executing both the output method. The output method has a simple system out print line statement that prints the value of var.

The output of both objects is the same because we are passing a static value to the class and thus all objects created have the same value.

That was a lot of theory for now. In the second post we explain more about object-oriented programming.