In this article we are going to see what is a CLASS in programming, what are its components, class diagrams and of course we are going to see examples in C# using Unity.
If you already know about this topic I invite you to see the practical part of this article in which I pose a problem to create a class structure, make diagrams and then program the solution in Unity.
The concept of Class is linked to object-oriented programming.
When we program in C# using Unity, when we create a new Script we automatically create a class that is called the same as the Script.
The terminology we use when referring to a class probably changes depending on the bibliography or the teachers, but if we can grasp the basic idea we can use this knowledge to create solutions for our projects.
What is a class in programming?
A class is a tool we have to model programming objects, make them behave as we want and make as many copies of them as we need.
For now let’s just think of it as code that is encapsulated in a programming script.
How to define a programming CLASS
Depending on what we are doing, creating a class will be done one way or another. In general what we will do is create an instruction file with the extension of the programming language we are using and whose name will coincide with the name we give to the class in its declaration.
In general, development environments simplify the process of creating a class. For example in Unity from the project folder we can right-click and use the menus to create a new C# Script, as shown in figure 1.
To show some examples of classes in Unity, I am going to create the three Scripts that are observed in figure 2.
Example 1, default class
When we create a new script, when we open it we are going to find some code already written.
In figure 3 we can see that in the first three lines are imported some libraries that will allow us to access the functionality of the Unity engine.
Then it is observed that a class called “Class1” is defined, the definition of the class begins in line 5 and ends in line 18 with the closing of the key.
Something we also see is the “MonoBehaviour” to the right of the class name, this means that our “Class1” is going to be an extension of the MonoBehaviour class, which is known as inheritance.
Inheritance is a great topic and we are not going to delve into this article, for the moment think that if a class called Class 1 inherits the class MonoBehaviour, means that Class 1 is itself a MonoBehaviour but with more specific functionality.
It’s like thinking about dogs and cats, these are different species but share certain characteristics such as the number of legs and also have similar behaviors such as walking, running, eating and breathing. This means that we can group them for example in a class called SerVivo, Animal or Quadruped (no accents are used in programming).
MonoBehaviour is like the basic class of GameObjects in Unity and allows these to be automatically initialized through the Start method and also automatically runs the Update methods (also defined automatically when creating a class, lines 8 and 14 of figure 3) and FixedUpdate for example. For more details about the MonoBehaviour class you can take a look at the Unity API.
Example 2, Object subclass
In the example in figure 4 the inheritance of MonoBehaviour has been removed, this implies that Class2 will inherit implicitly from the “Object” class, which is probably the mother of all the classes we are going to use in Unity.
By removing the MonoBehaviour the green comments in lines 7 and 13 of figure 4 are no longer true. These Start and Update methods will not be executed automatically.
It is not normally used in Unity this type of classes that derive directly from Object.
Example 3, subclass of another class
This example is similar to example 1, we can make classes that derive from other classes (inheritance of classes), not necessarily MonoBehaviour but any UnityEngine class or that we have created.
In figure 5 we see that Class 3 inherits Class 2.
Example 4, Java classes
Usando NetBeans IDE hice un par de clases en Java para mostrar, aunque Unity no usa Java, es interesante ver las similitudes y diferencias.
Las siguientes clases serán análogas a las creadas en los ejemplos 2 y 3.
La Figura 6 muestra una clase llamada Clase1 derivada de Objeto mientras que la Figura 7 muestra una clase llamada Clase2 derivada de Clase1.
Components or members of a class
The classes in programming are used to model objects that we want to fulfill a function.
It is the data that the class has, they can be variables or even instances of other classes.
For example, a class called “Auto” might have a boolean variable to indicate whether it is on or off. A float variable to indicate the speed and another for the remaining fuel.
We could use a three-dimensional vector (“Vector3” class in Unity) to determine the speed and direction of motion.
Also the class “Auto” could have between its fields the instance of another class called “Motor”, which in turn will have its own members defined.
Methods are those that provide functionality to a class, allowing it to perform tasks, manipulate its fields and interact with other classes.
Returning to the example of the “Auto” class that has among its fields the “Motor” class.
Suppose Auto has a method called “Accelerate” that progressively increases the speed of the vehicle.
A useful tool when it comes to reflecting the structure of classes that we will make to propose a solution is the class diagram, in which we will show the different classes, the relationship that exists between them and we will show the inheritance of classes that may exist.
In figure 8 we see an example of a class diagram based on the practical exercise of classes in programming.
Once we have defined the classes that will be present we think about their possible fields and methods, in this part it is good to reflect on the nature of the object we want to model, what are its characteristics, what information it handles and what can be its behavior.
We can represent the fields and methods of a class using “Object diagrams” that in general are drawn with a box for each class, indicating fields and methods using the names that we are going to give them in the script and separated by a line (in my case to the methods I add the symbols “( )” to indicate that they are methods). For the fields we also indicate the type of data.
Another thing we can indicate is the visibility of fields and methods, using a “+” sign for fields and public methods and the “-” sign for fields and private methods.
Figure 9 shows Object diagrams for the 4 classes of the class diagram in Figure 8.
In this article we have seen what a class is in programming, it is like a template that will allow us to create instances of objects that will have the attributes and methods that we have defined in the class.
Classes have defined within them fields and methods that together will define their nature and behavior.
The class diagram helps us to represent the relationship that exists between our classes.
The object diagram allows you to represent the fields and methods of a class along with their visibility.