By the will of fate, I have to read a special course on design patterns in high school. The special course is obligatory, therefore, students get to me the most different. Of course, there are among them practicing programmers. But, unfortunately, the majority have difficulty even with understanding the basic terms of the PLO.
For this, I tried to explain more or less vivid examples of basic concepts of OOP (class, object, interface, abstraction, encapsulation, inheritance, and polymorphism).
The first part is devoted to classes, objects and interfaces.
The second part, shown below, illustrates encapsulation, polymorphism, and inheritance.
')
Encapsulation
Imagine for a moment that we were at the end of the nineteenth century, when Henry Ford had not yet invented a conveyor belt, and the first attempts to create a car were criticized by the authorities that these smoky monsters pollute the air and frighten horses. Imagine that in order to control the first steam car, it was necessary to know how the steam boiler is arranged, to constantly flip coal, monitor the temperature and water level. At the same time to rotate the wheels use two levers, each of which turns one wheel separately. I think we can agree that driving a car of that time was very inconvenient and difficult.
Now back to the modern wonders of the automotive industry with automatic transmission. In fact, in fact, nothing has changed. The gasoline pump still delivers gasoline to the engine, the differentials ensure that the wheels turn to different angles, the crankshaft turns the forward movement of the piston into rotational motion of the wheels. Progress in another. Now all these actions are hidden from the user and allow him to turn the steering wheel and press the gas pedal without thinking about what happens at this time with the injector, throttle and camshaft. It is the concealment of the internal processes taking place in the car, which makes it possible to use it effectively even to those who are not a professional auto mechanic with twenty years of experience. This concealment in OOP is called encapsulation.
Encapsulation is a property of the system that allows you to combine data and methods that work with them in a class and hide details
implementations from the user.
Encapsulation is inextricably linked with the concept of class interface. In fact, everything that is not included in the interface is encapsulated in a class.
Abstraction
Imagine that a driver is driving in a car through a busy section of traffic. It is clear that at this moment he will not think about the chemical composition of the car paint, the peculiarities of the interaction of gears in the gearbox or the influence of body shape on speed (except that the car is in a deaf traffic jam and the driver has absolutely nothing to do). However, it will regularly use the steering wheel, pedals, direction indicator (and, possibly, an ashtray).
Abstracting is a way to isolate a set of significant characteristics of an object, excluding non-significant ones from consideration. Accordingly,
abstraction is a collection of all such characteristics.
If for modeling the behavior of a car, we had to take into account the chemical composition of the body paint and the specific heat of the room light bulb, we would never know what NFS is.
Polymorphism
Any driving instruction would not make sense if a person who learned to drive, say, a VAZ 2106 could not then drive a VAZ 2110 or BMW X3. On the other hand, it is difficult to imagine a person who could normally drive a car in which the gas pedal is located to the left of the brake pedal, and instead of the steering wheel - a joystick.
The thing is that the main controls of the car have the same design and principle of operation. The driver knows for sure that in order to turn left, he must turn the steering wheel, regardless of whether there is power steering or not.
If a person needs to get from work to home, he will sit behind the wheel of a car and will perform the same actions, no matter what type of car he uses. In fact, we can say that all cars have the same interface, and the driver, abstracting from the essence of the car, works with this interface. If the driver has to go on the German autobahn, he will probably choose a fast low-slung car, and if he has to return from a remote maral in Gorny Altai after the rain, the UAZ will most likely be chosen with army bridges. But, regardless of how the movement will be implemented and the internal functioning of the machine, the interface will remain the same.
Polymorphism is a property of the system to use objects with the same interface without information about the type and internal structure of the object.
For example, if you are reading data from a file, then obviously, in a class that implements a file stream, there will be a method similar to the following:
byte [] readBytes (int n);Suppose now that you need to read the same data from the socket. The class that implements the socket will also have a
readBytes method. It is enough to replace an object of one class with an object of another class in your system, and the result will be achieved.
In this case, the system logic can be implemented regardless of whether the data will be read from the file or received over the network. Thus, we abstract away from a specific data acquisition specialization and work at the interface level. The only requirement is that each object used has a
readBytes method.
Inheritance
Imagine, for a moment, the engineers of a car factory. Our task is to develop a modern car. We already have a previous model that has proven itself over many years of use. Everything is good, but the times and technologies are changing, and our modern plant should strive to improve the convenience and comfort of products and meet modern standards.
We need to release a whole range of cars: sedan, station wagon and subcompact hatch back. Obviously, we are not going to design a new car from scratch, but based on the previous generation, we will make a number of constructive changes. For example, add power steering and reduce the gaps between the wings and the bonnet, put fog lights. In addition, in each model will change the shape of the body.
It is obvious that all three modifications will have most of the properties of the previous model (the good old engine of 1970, impenetrable running gear, which proved to be excellent on domestic roads, gearbox, etc.). In addition, each of the models will implement some new functionality or design feature. In this case, we are dealing with inheritance.
Inheritance is a property of the system that allows a new class to be described on the basis of an existing one with partially or fully borrowed functionality. The class from which inheritance is made is called base or parent. New class - descendant, heir or derived class.
It should be noted that the derived class fully complies with the parent's specification, however, it may have additional functionality. From the interface point of view, each derived class fully implements the parent class interface. The reverse is not true.
Indeed, in our example, we could perform the same actions with the old cars as with the old ones: increase or decrease the speed, turn, turn on the turn signal. However, in addition we would have the opportunity, for example, to turn on the fog lights.
The lack of backward compatibility means that we should not expect from the old model the correct response to such actions as the inclusion of protivotumanok (which simply do not exist in this model).