Object-oriented approach in programming: concept, application and development

The first programming languages ​​were imperative languages, or machine instructions (codes). They consisted of binary code that varied from machine to machine. The set of possible commands for such low-level “languages” was small, since each machine instruction performed a certain action (addition, copying a machine word in the register, moving to the next section of code). Of course, for convenience, programmers developed a set of alphabetic analogs for these machine instructions, and this way of communicating with machine "hardware" was called assembly language.

Punch cards first




Assembler

Assembly language is a low level language. Its implementation and features vary from machine to machine, from processor to processor, that is, it is platform dependent. But the essence of assembler on any machine is the same: assembler commands directly correspond to similar machine instructions or their sequences. The difficulty in learning assembly language is that the programmer is forced to study the purpose of the language directives individually for each machine, which increases the entry threshold when changing the processor.

Using an Object Oriented Approach




The most famous assembler implementations are:





  • Borland Turbo Assembler (TASM).
  • Microsoft Macro Assembler (MASM).
  • Watcom Assembler (WASM).
  • A86.

High level languages

With the development of processors, a need arose for more universal and widely applicable tools for interacting with a computer. Over time, machine codes became very long and inconvenient to understand, so it was decided to operate with commands that are intuitive to a person reading and writing code. The first high-level computer language to use this approach was Fortran. Machine commands turned into readable ones: OPEN, CLOSE, PRINT, the block IF statement and such constructions as IF THEN - ELSE first appeared here.

Fortran language




Moreover, it is worth noting that Fortran, after the punch cards left for the dustbin of history, began to support structural programming.

Structural programming

In the 1960s and early 1970s, the development of the next programming paradigm began - structural programming, another step towards an object-oriented approach in software design. After Edgar Dijkstra's work “On the dangers of the goto operator”, developers of that time come to understand that the work of any program can be described using only three control structures:









  • sequence;
  • branching
  • cycle.

The goto statement has since been deemed redundant. This is an operator that allows you to go to any block of the program. And for beginning programmers sometimes it seems that there is nothing easier than using the goto operator in certain sections of the code so as not to invent the next branches and loops. But in fact, the use of this operator sooner or later leads to the fact that the program turns into a “spaghetti code”. Such code cannot be maintained, painlessly modified, and worst of all, it is difficult for other developers to come to replace the author of the code. This is especially dangerous in business development, where large databases are designed for thousands of lines of code. There is always fluid, and dealing with bad code is difficult, especially for new programmers who have arrived at the company.

object oriented programming approach




The c programming language

The heyday of structural programming is inextricably linked to the C programming language. This language was written in assembly language by Dennis Ritchie and Ken Thompson and became the source language when developing the UNIX operating system. It has become the basis for many modern operating systems, such as GNU / Linux, FreeBSD, MacOS, Posix and many others.

Language C




Due to the fact that the C language design is close to machine commands, it has gained widespread use, mainly in various application software for a variety of devices, from video cards and operating systems to rockets and supercomputers. And its syntax has forever become the basis for many modern programming languages ​​(C ++, C #, Java, Objective-C).

Object Oriented Programming (OOP)

Programs continued to become more complex, and the imperative paradigm is being replaced by an understanding of the need for an object-oriented approach to information technology. Instead of the usual work with a computer using the console, graphical applications appear. The computer is no longer a highly specialized apparatus for scientific and military calculations, but a tool whose capabilities range from business automation to communicating with friends.

The main structural unit in the development of an object-oriented approach is the class. A class is an abstract data type created by a programmer. This is a scheme, or contract, describing the fields and methods of objects that will be created using the patterns of this class.

For example, a person, a machine, a department is an abstraction, which means that it can be described as a class. Ivan Ivanovich, the white Skoda with numbers nn123, the operations department are specific representatives of these abstractions, that is, speaking the language of an object-oriented approach to programming, these are objects of these classes. The task of the developer is to describe abstract and concrete objects of the real world in the language of OOP. The class description is implemented in the description of its fields and methods.

the need for an object-oriented approach to information




Fields

Fields are variables, that is, quantities characterizing the operation of a given class. For example, if we write the “Machine” class for a computer game, we can define the following fields for it:

class Car { string brand = "Hunday Solaris"; string colour = "Yellow"; double speed = 0; /*...  ...*/ }
      
      



Encapsulation

Fields can change their values ​​during program execution, if required by the programmer. If the author does not want the fields to be accessible outside the class, and some other program (user) could change their value, then he “encapsulates” the data, that is, makes it inaccessible using the private, protected keywords. If the fields should be accessible throughout the program, then public access is set in front of them.

For example, you can make all fields of a class public:

 class Car { public string brand; public string colour; public double speed; \* ...  ... *\ }
      
      



In this case, access to these fields will not be limited. In the user interface, it will be possible to accidentally or intentionally change important data in such fields, which in the future will incorrectly affect the operation of the entire program:

  class MainClass { public static void Main() { Car car = new Car(); car.colour = "Red"; /* ...  ...*/ } }
      
      



To avoid accidental data changes, the developer encapsulates them. In the case of the color of the machine, instead of public, you must write private. Then changing the color directly will not be possible.

object oriented approach classes




Methods

Methods are functions that allow you to operate on class fields or some other data. Like any functions in procedural programming languages, they accept data, and can either return the result of calculations or not return (for example, output something to the console). For instance:

 class Car { public string brand = "Hunday Solaris"; public string colour = "Yellow"; public double speed = 0; /*   «Drive»,      whatIsTrafficLight (   false -  ,  true -  ,    )*/ public void Drive(bool whatIsTrafficLight) { if (whatIsTrafficLight == true) { speed = speed + 40; } else { speed = 0; } } }
      
      



As a result, using the Drive method, we change the speed of the “Machine” class.

Polymorphism

The second “pillar” of development by an object-oriented approach is polymorphism. Björn Straustrup, creator of the C ++ language, formulated the definition of polymorphism as follows: "One interface - many implementations." In short, polymorphism is the ability to create an abstract class that describes the general structure of a structure, and derived classes that implement the missing mechanisms are already being created. For example, when creating a character in a computer game, from the point of view of an object-oriented approach, it would be logical to first implement the abstract class Person, and from it already create specific classes: Archer, Healer, Warrior, and so on.

Or another example with a car. If we operate on one machine, with several fields and methods, it costs us nothing to manually change several values ​​in the code. But how many cars can there be? Or, for example, users on a social network? Everyone has a name, surname, marital status, photo album, a huge number of records, links to other pages, to other users and so on. And if the developers of the social network decide to make a redesign, and change or remove some user parameters, then with this approach a lot of work will arise. The object-oriented approach solves this problem. Classes are not created for each specific object, but at the beginning an abstract class is designed, and from it classes-heirs are created. Like encapsulation, polymorphism is the second most important OOP rule.

Inheritance

Inheritance is another rule when using the object-oriented approach. It lies in the ability of the descendant class to use the capabilities of the parent class. For example, if we want to have a motorcycle in our fleet, it’s not necessary to write repeating properties for a new class. Instead, we can say that the motorcycle is the successor class from the car. Then it becomes possible to use similar fields and methods of the car in a motorcycle class, for example, make, color, speed. In the code, inheritance is indicated as follows:

 class Motocycle : Car { /*…  ...*/ }
      
      



The fields and methods of the Car parent class are now available for use in the Motorcycle child class.

In short, inheritance is a mechanism for reusing code, and is aimed at a convenient and logically competent extension of the program. Inheritance also helps to follow the principle of DRY (Do not Repeat Yourself). According to this principle, the code should not have duplicate sections, because this creates an extra load when compiling and executing the program. If the code has duplicate sections, it means that it needs to be optimized - to make repetitions in separate methods, and call them as needed; apply inheritance to logically similar objects that have identical fields and methods.

Summary

The concept of an object-oriented approach to programming has been around for more than forty years, and now it is the most popular development method (except for specific areas, for example, software development for controllers, where the C language dominates). The most important OOP paradigms are:

  1. Inheritance.
  2. Polymorphism.
  3. Encapsulation.
object oriented design approach




Having well studied these powerful tools, a modern developer will be able to write fast, supported, visually pleasant and modifiable code that will support the needs of the business for many years, bring joy to gamers, solve social problems of people or provide communication in all corners of the world.




All Articles