📜 ⬆️ ⬇️

OOP with examples (part 1)

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, presented below, is devoted to classes, objects, and interfaces.
The second part illustrates encapsulation, polymorphism and inheritance.
')


Basic OOP Concepts


Class

Imagine that you are designing a car. You know that a car should contain an engine, a suspension, two headlights, 4 wheels, etc. You also know that your car should be able to pick up and slow down, make a turn and move in reverse. And, most importantly, you know exactly how the engine and wheels interact, according to what laws the camshaft and crankshaft move, and how the differentials are made. You are confident in your knowledge and start designing.

You describe all the parts that make up your car, as well as how these parts interact with each other. In addition, you describe what the user must do in order for the car to brake or for the main beam headlights to turn on. The result of your work will be some sketch. You have just developed what the OOP class is called.

A class is a way of describing an entity, defining a state and behavior depending on this state, as well as rules for interacting with a given entity (contract).

From the point of view of programming, a class can be considered as a set of data (fields, attributes, class members) and functions for working with them (methods).

In terms of program structure, a class is a complex data type.

In our case, the class will display the entity - the car. Class attributes will be the engine, suspension, body, four wheels, etc. Class methods will be “open the door”, “push the gas pedal”, and also “pump a portion of gasoline from the gas tank into the engine”. The first two methods are available for execution by other classes (in particular, the “Driver” class). The latter describes the interactions inside the class and is not available to the user.

In the future, despite the fact that the word "user" is associated with solitaire "Solitaire" and "Microsoft Word", we will call the users of those programmers who use your class, including yourself. The person who is the author of the class, we will call the developer.

An object

You did a great job and the machines designed according to your drawings go off the assembly line. Here they are, standing in even rows in the factory yard. Each of them exactly repeats your drawings. All systems interact exactly as you designed. But each car is unique. They all have a body and engine number, but all these numbers are different, cars differ in color, and some even have a cast instead of stamped discs. These cars, in fact, are objects of your class.

An object (instance) is a separate representative of a class that has a specific state and behavior that is fully defined by the class.

In simple terms, an object has specific attribute values ​​and methods that work with these values ​​based on the rules specified in the class. In this example, if a class is some abstract car from the “world of ideas”, then the object is a particular car that stands under your windows.

Interface

When we come to the coffee machine or we get behind the wheel, we start interacting with them. Usually, the interaction takes place with the help of a certain set of elements: a slot for accepting coins, a button for choosing a drink and a compartment for dispensing a glass in a coffee machine; steering wheel, pedals, gear lever in the car. There is always some limited set of controls with which we can interact.

An interface is a set of class methods available for use by other classes.

Obviously, the class interface will be a set of all its public methods in conjunction with a set of public attributes. In fact, the interface specifies the class, clearly defining all possible actions on it.
A good example of the interface can serve as a car dashboard, which allows you to call methods such as an increase in speed, braking, turning, shifting gears, turning on headlights, etc. That is, all actions that can be carried out by another class (in our case, the driver) when interacting with the car.

When describing a class interface, it is very important to strike a balance between flexibility and simplicity. A class with a simple interface will be easy to use, but there will be tasks that will not be able to solve with it. At the same time, if the interface is flexible, then most likely it will consist of quite complex methods with a large number of parameters that will allow you to do a lot, but using it will be more difficult and risky to make a mistake, something mixed up.

An example of a simple interface can be a boxed machine. Any blonde who has completed two-week driving courses can master her management very quickly. On the other hand, in order to master the management of a modern passenger aircraft, it takes several months, or even years of hard training. I would not want to be on board the Boeing, which is managed by a man who has a two-week flight experience. On the other hand, you will never force a car to rise into the air and fly from Moscow to Washington.

Source: https://habr.com/ru/post/87119/


All Articles