Introduction to object-oriented programming (OOP) in Kotlin. Classes and objects

The object-oriented programming (abbreviated OOP) is an approach to developing software that aims to develop the software code using Classes and objects to structure and organize. One Class serves as this a kind of blueprint or template for creating objects. It specifies the properties (known as attributes) and behavior (in the form of functions or otherwise known as methods) that an object of its kind will possess. One created based on a class Object is called Instance of this class designated. Each instance has access to the attributes and methods defined in the class, but can also set its own values ​​for these attributes.

To better understand the principle of object-oriented programming, let's look at an example from everyday life. Let's take a car. It has a make, model name, color, engine displacement and so on. In addition, a driver can control the car through various commands: drive forward, reverse, brake, turn right, turn left, etc. In general, one can say that there is a class of cars that have common characteristics (all cars have a brand , a model name, a color, etc. and all can receive commands).

A pair of specific car, which stands on the street, is a specimen of this class or, in other words, a Object of this class. All objects in this class have properties such as the make, model name, color, engine displacement and methods such as drive forward, reverse, brake, turn right, turn left. In other words, the The blueprint is great, according to which cars are manufactured in the automobile factory. The Object however, is the car itself, which was made according to these plans. The following image clearly shows this example:

Object-oriented programming (OOP) in Kotlin

The class Vehicle forms a template with corresponding attributes, on the basis of which three different vehicles (objects) were produced.

Declaration of a class

Now let's look at how to implement the above example in Kotlin code. First we create a class called Vehicle with appropriate attributes and methods.

To declare a class in Kotlin, the keyword class used, followed by the name of the class. 

The content of a class is defined after its name within curly brackets.

Properties (attributes) of a class

First, we determine the characteristics of each car produced according to this design. To do this, we define the attributes within the class as variables for brand, model, color and engine displacement. So that we can change these attributes later, we create them as mutable variables. There Attributes within a class in Kotlin always have to be assigned values, these initially receive the initial values “Undefined” or 0.00.

Functions (methods) of a class

The methods of a class describe the behavior of objects and are defined in the form of functions. So that our future cars can drive, we define the methods that describe different commands/actions: drive forward, drive backward, brake. 

Now we have the blueprint (a class called Vehicle) created for our future vehicles. Now it's time to build a specific car based on it at an automobile plant, that is, to create an object of this class.

Creation of an object of a class

To create an object of a class, a variable that references the class must first be initialized. The object is stored in this variable. Then the Constructor of the class called:

The constructor call is after the assignment operator =. This has the same name as the class and its task is to create the new one Initialize object and assign initial values ​​to it

Access to attributes and functions of an object

After we created a car and put it in the variable vehicle1 saved, we can view the result by printing the attributes and methods of this object in the console. The syntax for accessing attributes and functions of an object generally looks like this:

You use the name of the variable that represents the object, and after a dot (also called the dot operator) you specify the name of the attribute or method. When a method is called, the parentheses also follow. 

In our specific case we implement this in the code as follows:

Changing attributes of an object

Run the code above and see the output. As you can see, our first car has taken the initial values ​​from the class. 

But we want to create a specific vehicle with its own unique characteristics. So we modify our car by overriding the object's attributes. To change the value of an attribute, we use the syntax we already know: first we write the variable name, followed by a dot operator and then the name of the attribute. Additionally after the assignment operator = let's define the new value:

Now the complete code with the final result:

Leave a Comment

Your e-mail address will not be published. Required fields are marked with * marked