You get a bonus - 1 coin for daily activity. Now you have 1 coin

Four concepts of OOP. Classes and relationships between them

Lecture



In the last publication, the concept of an object in OOP was considered. Classes will be described in this note. This is a rather complex topic, the consideration of which implies that you are familiar with the concept of an object in object-oriented programming.

The concept of class in OOP

In any system, many objects function. Some of them are “similar” and of the same type. For example, in the banking system there are many objects, accounts and customer objects. Objects of the same type are combined into classes .

All objects of the same class have the same interface and implement this interface in the same way. Two objects of the same class in OOP can differ only in the current state, and it is always theoretically possible to change the state of one object in such a way that it becomes equal to another object.

For example, continuing the examples of the previous article, all account objects belonging to the “Account” class have a number and balance, all of them react to the message “check money availability and withdraw money from the account”. It is important that they react to this message in the same way, i.e. implementation of the method for all objects of the same class is the same.

Individual objects are called class instances , and a class in OOP is a pattern by which objects are built.
Thus, our banking system consists of instances of three classes: account class, ATM class, and customer class. Class names in OOP are written with a capital letter, and object names - with a small one. The following graphic class diagram corresponds to the notation adopted in the Unified Modeling Language UML .

Four concepts of OOP.  Classes and relationships between them

Classes in the educational banking system

In reality, accounts may be different. A term deposit differs from the settlement deposit and from a demand deposit. They have different characteristics, and they implement the same operations in different ways. Therefore, to describe them, we need different classes.

Four concepts of OOP.  Classes and relationships between them

Splitting accounts into different classes

What distinguishes the concept of class in OOP from such concepts as "interface" or "type"?
The interface is the external part of the class. An interface defines how objects of a given class can interact with other objects of this or other classes. However, if the two objects have the same interfaces, this does not mean that they belong to the same class. In addition to the coincidence of interfaces, it is necessary that their implementation and behavior be the same.
Type is the domain of definition of a certain value, i.e. the set of its possible values ​​and the set of applicable operations. The type can be set by class.

Class Inheritance in OOP

The most important feature of classes in OOP and their fundamental difference from abstract data types (embedded in a programming language) is inheritance . Inheritance is a relationship between classes, in which one class shares the structure or behavior of one or more other classes.

The mechanism of class inheritance in OOP allows you to select common parts of different classes. In the example above, different types of accounts in the banking system were highlighted. However, they have a lot in common. Having selected the common part, you can create the class “Account”. Classes "Current Account" and "Deposit" retain all the properties (both methods and attributes) of the "Account" class, complementing and clarifying its behavior. They say that the class "Deposit" inherits the class "Account". Graphically, this is depicted as a hierarchy.

Four concepts of OOP.  Classes and relationships between them

Invoice Class Inheritance Scheme

Class inheritance in OOP can be multi-level. An example of such a multi-level structure of classes of accounts is presented below.

Four concepts of OOP.  Classes and relationships between them

The scheme of multi-level class inheritance

The hierarchy of classes in OOP can be constructed in different ways. In fact, the class hierarchy is a classifier of objects. In this case, when building a class system, the developer tries to take into account the following considerations: “Is the difference between ruble and foreign currency contributions so significant that they should be divided into different classes?”, “Different types of deposits are different characteristics of the same class or different classes? ", etc.

As can be seen from the figure presented above, the difference between the ruble and foreign currency accounts is so significant that they are divided into different classes. Different types of deposits are also represented by different classes. If they decided that the monetary unit in which the amount on the account is expressed is only an additional attribute of the account, and different types of deposits are distinguished by an additional characteristic of the Deposit class, then the class hierarchy would be transformed into the form shown in the figure:

Four concepts of OOP.  Classes and relationships between them

Simplified hierarchy of currency and ruble accounts

It is important to note that in OOP there is a special type of classes - abstract classes . Abstract classes are classes for which there are no instances, they only describe the general characteristics of descendant classes. In our case, the class “Account” can be considered an abstract class, since no instances of this class actually exist. But it is used to implement a common interface for descendant classes.
Specific classes are instances of which may exist (or exist) in the system, unlike abstract classes.

The mechanism of abstract classes in OOP is an extremely powerful concept that is widely used. The purpose of abstract classes in OOP is to determine the common, most characteristic methods and attributes of the classes inherited from them. Most often, abstract classes are used to define a common interface for a hierarchy of specific classes, although attributes and the implementation of some methods may be present in abstract classes.

The examples considered so far have shown how a class in OOP can inherit methods and attributes of a single base class. Such inheritance is called single or simple inheritance . Along with it, there is also multiple inheritance , in which one class has several basic ones.

Multiple inheritance in OOP allows you to combine the characteristics of different classes in one. We considered a class hierarchy for the presentation of accounts in the banking system, which displays the functional characteristics of accounts - the ability to live or withdraw money from an account. On the other hand, in the implementation of this banking system, many objects, including accounts, should be stored in a database. A class system that stores objects in a database can consist of the base class “Permanent Object”, which has methods to save and retrieve to implement writing and reading from the database, and the attributes “table name” and “row number” to describe the location object. In order for any particular account to become possible to be stored in a database, it must be derived from the "Permanent Object" class.

Four concepts of OOP.  Classes and relationships between them

Multiple inheritance

The “Currency Deposit” class inherits the attributes and methods of both its parents.

Polymorphism in OOP

Polymorphism is the ability to interact with an object without knowing which particular class it belongs to. For example, sending a message to any account, we use the polymorphism of all accounts.
In this case, polymorphism is limited. If the message “remove” is sent, for example, to the client, he will not be able to process it, i.e. It does not matter to us which specific account handles the message, but nevertheless it must be an account, an object of one of the classes separated from the base class “Account”.

In most cases, limited polymorphism is used. However, sometimes any system object can process some message. For example, in the Java language , all objects have a toString method that reduces the value of an object to a string form, according to any object, regardless of its class, you can send a message toString .

When using polymorphism in OOP, knowledge of the object's interface is used; however, the behavior of a particular object in response to the message received may be different depending on the specific class of this object. Accordingly, the object sending the message does not know exactly what will happen when the method is called.

Abstraction It is important to remember that when describing the behavior of any object, you need to choose the appropriate level of abstraction for solving a specific task. Objects of the real world can be quite complex to describe all their characteristics, moreover, the solution of specific problems will require only the presence of some of them. Thus, we must abstract away from some specific details of the object. But it is also important that the abstraction is not too generalized and allows to correctly model the behavior of the object.


Next, we consider the 3 main principles on which object-oriented programming is built: Encapsulation; Inheritance; Polymorphism.


Encapsulation is a mechanism that combines attributes and methods (which make up an object) and protects them from external interference. Encapsulation is a protective shell that allows you to access the attributes and methods of a class only within this class or using a specially designed interface.
Attributes or methods of a class can be open (public) or closed (private). Private attributes and methods can be accessible only inside the class in which they are located; they are not accessible to that part of the program code that is outside this class. Open attributes and methods are available, including the program code outside the class. Thus, public methods are used to provide a controlled interface to closed class elements.
For example, imagine that a robot has LEDs on its head that change color by the voice command “Change Color”. We can not otherwise affect the change in the color of the diodes, because this will not allow privacy settings. We can influence the change of color, only with the help of a specific voice command, which in this case is an interface to the LEDs.


Inheritance helps to avoid code duplication in case we need to create an object based on an existing one. In this case, it is stated that the new object (child) inherited the properties of an already existing (parent) one. If the attributes or behavior of an existing object needs to be partially changed, then you can simply override them.
For example, based on an existing Robot object, we can create a new CoffeRobot object that will brew coffee. The new robot will have all the attributes and methods as the previous one, plus contain an additional method “Brew coffee”.

Polymorphism

If we have objects that belong to the same branch of the hierarchy (were inherited), then they can use a single interface that will produce the same type of action for each object, but the result for each object will be different (depending on this particular object).

For example, if using inheritance we create a series of robots of different types (a robot that brews coffee, a robot that washes the floor, a robot that waters the flowers), and then we give the command “work” to each robot, then each robot responds to the same the team itself will do various actions, according to its type. That is, the single interface here is the Robot object with the “work” method, and how exactly it will work depends on its implementation.


Classes and class relationships:
Inheritance (Generalization) - objects of the child class inherit all properties of the parent class.
Association - class objects interact with each other.
Aggregation - objects of one class are included in objects of another.
Composition - the objects of one class are included in the objects of another and depend on each other for a lifetime.
A metaclass class is a relationship in which other classes are instances of one class.


Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Object oriented programming

Terms: Object oriented programming