As is well known almost all programming languages in the world today are object oriented languages. But what does it mean? What is an object? What object do they mean? And to orient on it?
Going back to the previous topic (which you can read here), it is recalled that HTML and CSS are not programming languages. These languages are only responsible for the layout and style. Languages that are working with the logic of pages, such as javascript, C #, jquery, C ++, java, php etc are precisely the object-oriented languages.
What is the meaning of the notion object-oriented? According to Wikipedia: Object-oriented programming (OOP) – programming paradigm, in which the basic concepts are the concepts of objects and classes.
Before turning to the explanation of these basic concepts must be understood that even though languages are object-oriented, all of them are divided into groups. At the heart of of any language is the concept of the object. Object – an entity that can send messages and which can respond to them using their data. But there is a group of languages (which includes our friend javascript) called a prototyping languages. In this group of language objects exist by themselves and each new object IS new! It could be with unique properties, and it can repeat properties from other objects. Usually the object – is an instance of the class. The data of the object is hidden from the rest of the program. The class – it’s like a kind of example to indicate like what should be its objects.
For a better understanding let’s turn to my favorite allegories. Imagine what we are doing cookies – gingerbread men. Our entire program logic – is the stretched almost ready dough. Now we need to select the objects, that is to make each individual cookie. Everyone knows that the ginger man should have 2 feet, two hands, eyes, smile, and buttons. In prototyping languages the logic is that each programmer sculpts a gingerbread man itself. And he may himself each time for each new gingerbread man make his own design. Adding buttons or not? To make a man having two eyes, or make it like Cyclops? J
Let us turn to a more concrete examples. That is, for example in the java script code to make a gingerbread man would look like this:
And for his friend – Cyclops like this:
In general, each time you create a new object it is not necessarily follow the specified earlier style. You can add or remove properties or methods. In the second example of the code you will notice that Cyclops has no buttons, and he does not speak words of welcome.
In ordinary programming there is a more clear-cut. That is, every time you create an object it is necessary to create it as an example shown in the class. It is necessary to use a form for each person, that is, he definitely must have legs, arms, buttons etc. If provided in the “form” – i.e. the class of the man will speak words of welcome. That is, in fact there is no choice. If we want to make Cyclops without buttons and a welcome speech in C # for example, then we will have to create a new class – the “Cyclops” and in it – our object – cyclops Pretty.
This is the code in C # to create a class Gingerbread man:
This is the code to create an object gingerbread man named Happy and indications of its working function when he greets you:
This is the code to create the class Cyclops:
This is the code to create an object cyclops called Pretty:
As you can see, to create a new object in C # only 1 line of code is needed. This is the main advantage of ordinary object-oriented languages. That means that you can create a huge number of identical objects with less code.
I hope this view is understandable. If not, I’m ready to explain my vision in the comments.