OOP classes. Object oriented programming

The terms "object" and "class" are familiar to every person. However, for computer scientists, they have their own subtext. These are the basic concepts in object-oriented programming. Classes - a data type defined by the developer, which is characterized by the way they are transmitted and stored, the usage profile and the set of actions that can be performed with them. They differ in that they can be implemented as an interface.

What is OOP (Object Oriented Programming)

Experienced developers know the COBOL and C languages ​​well. The programs written on them were a sequence of step-by-step instructions. They used procedures and functions to make the program modular. This paradigm focused on logic, not data, and on methods for combining them.

Object Oriented Programming Classes




Modern programming languages ​​Delphi, Java, C # and others follow an object-oriented approach. At the same time, importance is given to data, and not just to writing instructions to complete a task. An object is a thing or idea that you want to model. It can be anything, for example, an employee, a bank account, a car, various furnishings and so on.





The concept of object-oriented programming (OOP) is inextricably linked with the following criteria:

  • Abstraction.
  • Encapsulation.
  • Inheritance.
  • Polymorphism.

Let's consider each of them in more detail.

Abstraction

This criterion allows you to focus on what the object does, but not on how these actions are implemented in programming. OOP implies that abstraction is the knowledge of an object with the maximum amount of data. It helps in creating independent modules that can interact with each other in some ways.

We try to selectively focus only on those things that are important for us (in life) or for our module (in programming). Changing one independent module does not affect the others. The only thing you need to know is what he gives us. The person who uses this module does not have to worry about how the task is solved, what exactly happens in the background.

Class C Constructor




Everyday objects that we use have abstractions applied at different levels. One example of object-oriented programming is the application of braking in a car. This system is abstract: it is enough for the motorist to press the pedal so that the vehicle slows down and stops. Making changes to the acceleration system does not affect the braking system, since they are independent. The driver does not need to understand the internal operation of the brakes. It is only required of him to press the pedal on time. In this case, the brake (both disk and drum) will work, and the car will slow down the speed.





Encapsulation

This concept is closely related to abstraction. Encapsulation is the disclosure of a solution to a problem that does not require the user to fully understand its subject area. It binds data and behavior into a single whole and does not allow the client or user of the module to learn about the internal representation in which the abstraction behavior is implemented.

Object Oriented Programming System




Data is not available directly. Access to them is carried out through certain functions. Hiding the internal elements of an object protects its integrity by preventing users from translating the internal data of the component into an invalid or incompatible state.

Inheritance

This is a code reuse mechanism that can help reduce duplication. This concept is a powerful feature of object-oriented programming languages. It helps organize classes into hierarchies, allowing them to inherit attributes and behavior from the components above.

An example of inheritance: a parrot is a bird, the Russian ruble is a type of currency. However, the phrase "bank is a bank account" is incorrect. This connection is obvious when it is required to describe some entity in a given statement of the problem. Using inheritance, you can determine the overall implementation of OOP and its behavior, and then for specialized classes, override or change these indicators to something more specific. Inheritance does not work backwards. The source (the so-called parent) will not have the properties of the derived (child class).

It is important to note that when trying to model a solution, you should not add several levels of inheritance. You need to try to define common attributes and behavior in the objects that are modeled. Further, based on this, you can continue to refactor the code that defines the appropriate parent class. The overall implementation can be moved into it.

Polymorphism

This concept allows you to expand computer systems by creating new specialized objects. At the same time, it enables the current version to interact with the new one, without paying attention to its specific properties.

For example, if the task is to write a message on a piece of paper, you can use a pen, pencil, marker or pen. It is enough that the tool could fit in the hand and had the opportunity to leave a mark on contact with the paper. It turns out that certain human actions make an inscription on the sheet, and what tool is used in this case, this is not so important for the transfer of information.

Another example of polymorphism in an object-oriented programming system is an airplane and a space shuttle, which can be called flying objects. How exactly do they move in space? Of course, there is a big difference in their work. That is, the ways to implement their movement are not the same. However, from the viewer's point of view, both objects fly.

OOP implementation




Inheritance is one way to achieve polymorphism when behavior defined in an inherited class can be overridden by writing a custom implementation of the method. This is called overriding (compile-time polymorphism).

There is another form of polymorphism called overload, in which inheritance is not taken into account. The method name will be the same, but the arguments in the method are different.

Features of the concepts of "class" and "object"

To start working with object-oriented programming, we need to figure out what the OOP class and object are. It is important to understand the difference between the two. A class is a plan for creating an object. It defines attributes and behavior. It is like an engineering drawing of a house. An object is an instance of a class. That’s the difference between them. The following example shows how the "TForml" class and the "Forml" variable are declared in the Delphi programming language:

type TForml = class(TForm) Buttonl: TButton; procedure ButtonlClick(Sender: TObject); end; var Forml: TForml;
      
      



If we want to model, for example, a car in our program, we must determine its attributes: model, fuel, brand, color, its behavior, as well as the so-called methods: starting the engine, braking, acceleration, and so on. It is clearly seen that these indicators are characteristic not only for one make or model of a vehicle.

With an object-oriented approach, we try to generalize our object (machine), arguing that the one we are going to model in our program will have a certain number of attributes and methods. There may be other indicators and characteristics of the vehicle, but those listed are enough for us to understand how the class works in OOP.

When we use this data, we create a car with specific parameters. By programming the same object (machine), we can take different characteristics, as shown in the table below:

Object 1Object 2
Model: VAZ 2107Model: VAZ 2109
fuel: gasolinefuel: diesel
color: redgreen color
engine start method: Start ()engine start method: Start ()
braking method: Break ()braking method: Break ()
Acceleration Method: Acceleration ()Acceleration Method: Acceleration ()

Thus, object-oriented programming makes it easy to model the behavior of a complex real-world system. With OOP, data and functions (attributes and methods) are combined in an object. This prevents the need for any shared or global data with OOP. This approach is the main difference between object-oriented and procedural approaches.

What is a class in OOP




OOP classes consist of elements of various types:

  1. Data Fields: Store the state of a class using variables and structures.
  2. Methods: routines for manipulating the specified data.
  3. Some languages ​​allow a third type — properties. This is a cross between the first two.

Methods

The behavior of a class or its instances is determined using methods. These are routines with the ability to operate on objects. These operations can change the state of an object or simply provide ways to access it.

There are many methods. Their support is language dependent. Some are created and called by the programmer’s code, others (special ones, such as constructors, destructors and conversion operators) are created and called by the code generated by the compiler. The language may allow the programmer to define these special methods.

Interface

This is the definition of a group of abstract actions. He finds out what behavior a particular object should exhibit without specifying how it should be implemented.

An object can have several roles, and users can use it from different points of view. For example, an object of type "person" may have roles:

  • Soldier (with the behavior of "shoot at the enemy").
  • Husband (with the behavior "love your wife").
  • Taxpayer (with the behavior "pay taxes") and so on.

However, each object implements its own behavior in its own way: Misha pays taxes on time, Andrei is late, and Peter does not do this at all. The same can be said about each object and other roles.

What is OOP




The question is why the base class of all objects is not an interface. The reason is that in this case, each class will have to implement a small but very important group of methods, which will take an unnecessary amount of time. It turns out that not all classes need a specific implementation - a common default is sufficient in most cases. There is no need to redefine any methods, but if the situation requires it, then it is possible to override them.

A good example are the buttons on the front of the TV. We can say that they are the interface between the user and the wiring on the other side of the device. A man presses the power button to turn the appliance on and off. In this example, a specific TV is an instance, each method is represented by a button, and together they make up the interface. In its most common form, it is a specification of a group of related methods without their implementation.

Constructor

This criterion is responsible for preparing an object for action, for example, for setting initial values ​​for all its data and their elements. Although it plays a special role, the constructor is just another function with which you can pass information through a list of arguments. They can be used to initialize it. The name of the constructor function and the class are the same.

The following example explains the concept of a class constructor in C ++ (a common programming language):

 #include <iostream> using namespace std; class Line { public: void setLength( double len ); double getLength( void ); Line(); //   private: double length; }; //  ,   Line::Line(void) { cout << " " << endl; } void Line::setLength( double len ) { length = len; } double Line::getLength( void ) { return length; } //   int main() { Line line; //   line.setLength(6.0); cout << "Length of line : " << line.getLength() <<endl; return 0; }
      
      



When the above code is compiled and executed, it gives the following result:

Object created

Length of line: 6

Destructor

This is a special class function that destroys an object as soon as its scope expires. The destructor is automatically called by the compiler when the object goes out of scope.

The syntax for the destructor is the same as for the constructor, however, the class name is used in this case for it with the tilde sign "~" as a prefix.

The following C ++ example explains the concept of a destructor:

 #include <iostream> using namespace std; class Line { public: void setLength( double len ); double getLength( void ); Line(); //   ~Line(); //   private: double length; } //  ,   Line::Line(void) { cout << " " << endl; } Line::~Line(void) { cout << " " << endl; } void Line::setLength( double len ) { length = len; } double Line::getLength( void ) { return length; } //   int main() { Line line; //   line.setLength(6.0); cout << "Length of line : " << line.getLength() <<endl; return 0; }
      
      



When the above code is compiled and executed, it will give the following result:





Length of line: 6







What are the advantages of classes

The benefits of organizing software into object classes are divided into three categories:

  • Fast development.
  • Ease of maintenance.
  • Reuse of code and design.
Basic concepts of object-oriented programming




Classes and OOP in general contribute to rapid development, as they reduce the semantic gap between code and users. This is appreciated by many programmers. Thanks to this system, analysts can communicate with both developers and users using the same dictionary, talking about accounts, customers, accounts and so on.

Object classes often contribute to rapid development, since most object-oriented environments have powerful debugging and testing tools. Class instances can be checked at runtime to ensure that the system is working properly. In addition, instead of receiving core memory dumps, most object-oriented environments interpret debugging capabilities. As a result, developers can accurately analyze where an error occurred in the program and see which methods, arguments, and values ​​were used.




All Articles