Object-oriented programming is based on the representation of a program as a set of objects. Each object belongs to a class, which, in turn, takes its place in the inherited hierarchy. Using OOP minimizes redundant data, it improves manageability, understanding of the program.
What is OOP
It arose as a result of the development of procedural programming. The basis of object-oriented languages ​​are principles such as:
- encapsulation;
- inheritance;
- polymorphism.
Some of the principles that were originally laid down in the first OOI have undergone a significant change.
Examples of object oriented languages:
- Pascal. With the release of Delphi 7 at the official level, it became known as Delphi. The main area of ​​use for Object Pascal is writing application software.
- C ++ is widely used for software development, is one of the most popular languages. It is used to create OS, application programs, device drivers, applications, servers, games.
- Java - translated into bytecode, processed by the Java virtual machine. The advantage of this method of execution is independence from the operating system and hardware. Existing families: Standard Edition, Enterprise Edition, Micro Edition, Card.
- JavaScript is used as a scripting language for web pages. The syntax is much like C and Java. It is an implementation of Ecmascript. Ecmascript itself is used as the basis for building other scripting languages, such as JScript, ActionScript.
- Objective-C is based on the C language, and the C code itself is understandable to the Objective-C compiler.
- Perl is a general-purpose high-level interpreted dynamic language. It has rich capabilities for working with text, originally designed specifically for manipulating text. Now it is used in system administration, development, network programming, bioinformatics, etc.
- PHP The abbreviation is translated as a hypertext preprocessor. It is used to develop web applications, in particular the server side. With it, you can create gui-applications using the packages PHP-GTK, PHP-Qt, WinBinder.
- Python is a general-purpose language that focuses on improving developer productivity and code readability. The Cython project was developed, with the help of which the translation of programs written in Python into C code is carried out.
Abstraction
Any book of the kind “Object-Oriented Programming for Dummies” highlights one of the main principles - abstraction. The idea is to separate the details or characteristics of the implementation of the program into important and unimportant. It is necessary for large projects, allows you to work at different levels of the system, without specifying details.
An abstract data type is represented as an interface or structure. Allows you not to think about the level of detail of the implementation. ADT is independent of other sections of code.
David Wheeler's famous aphorism says: All problems in computer science can be solved at a different level of abstraction.
Inheritance
Object-oriented languages ​​are inherited - this is one of the most important principles.
Indicates that some type of functionality can be reused. A class that inherits the properties of another is called a derived, descendant, or subclass. The one from which the inheritance is derived is called the ancestor, base or superclass. The descendant-heir relationship generates a special hierarchy.
There are several types of inheritance:
With multiple inheritance, there can be several children from one ancestor, when with simple inheritance, only one. This is the main difference between the types.
Inheritance looks like this:
class Animal {
function draw () {
return "just animal";
}
function eat () {
return "the animal is eating";
}
}
class Cow extends Animal {
function draw () {
Return "something that looks like a cow";
}
}
We see that class Cow inherited all the methods from class Animal. Now, if we execute Cow.eat (), we get "the animal is eating", respectively, the draw () method is changed. Cow.draw () will return “something that looks like a cow”, and Animal.draw () will return “just animal”.
Encapsulation
Encapsulation restricts component access to others, associates data with processing methods. For encapsulation, the private access specifier is used.
Typically, the concepts of encapsulation and concealment are identified, but some languages ​​distinguish between these concepts. In other words, properties critical for operation are protected, and their change becomes impossible.
class Animal {
private $ name;
function __construct ($ name) {
$ this-> name = $ name;
}
function getName () {
return $ this-> name;
}
}
Name is accepted as constructor arguments. When the constructor is used in other parts of the code, nothing can change the name element. As you can see, it is indicated inside, for other parts of the code it is not available.
Polymorphism
Polymorphism allows you to use the same name to solve similar, but technically different tasks.
In the example above is a table. We see class CardDesk and class GraphicalObject. Both have a function called draw (). She performs different actions, although she has one name.
Ad hoc polymorphism or special polymorphism uses:
- overload of functions, methods;
- cast.
Overloading involves the use of several functions with the same name, when the selection of suitable occurs at the compilation stage.
Type conversion means converting a value of one type to a value of another type. There is an explicit conversion — a function is applied that takes one type and returns another, implicit — is performed by the compiler or interpreter.
“One interface - many implementations” Björn Straustrup.
Class
A class is a data type that consists of a single set of fields and methods.
It has internal and external interfaces for managing content. When copying through assignment, the interface is copied, but not the data. Different species interact with each other through:
- inheritance;
- associations;
- aggregation.
When inheriting, a child class inherits all the properties of the parent; association implies the interaction of objects. When an object of one class enters another, this is called aggregation. But when they still depend on each other for their lifetime, this is a composition.
One of the main characteristics is the scope. The concept is defined differently by different PLs.
Object Pascal is described as follows:
ClassName = class (SuperClass)
private
{use of elements is limited only by module limits}
{fields are indicated here}
strict private
{access specifier became available with the release of Delphi 2007, means the same as private}
protected
{elements can be used inside ClassName or when inheriting}
public
{}
published
{elements are accessible to everyone, they are displayed in the Object Inspector'e}
end;
Here SuperClass is the ancestor from which the inheritance comes from.
For C ++, the creation looks like this:
class MyClass: public Parent
{
public:
MyClass (); // constructor
~ MyClass (); // destructor
protected:
private:
};
In this example, Parent is the ancestor, if any. The private, public, protected qualifiers denote the same as in the previous example in Pascal. We also see the constructor, destructor, available for any part of the program. In C ++, all elements are private by default; accordingly, this can be omitted.
Implementation Features
At the center of object-oriented languages ​​is an object, it is part of a class. It consists of:
The data field describes the parameters of the object. They represent a certain value that belongs to any class, describe its state, properties. They are closed by default, and data is modified through the use of various methods.
Method - a set of functions that determine all the possible actions available to perform on an object. All objects interact by calling each other's methods. They can be external or internal, which is specified by access modifiers.
OOP methodologies
There are such methodologies:
- Component oriented programming;
- Prototype programming;
- Class-oriented programming.
Component-oriented programming relies on the concept of a component - such a component of a program that is intended for reuse. It is realized as a set of constructions with a common attribute, rules, restrictions. The approach is used in the object-oriented Java language, where component orientation is implemented using “JavaBeans”, written according to one rule.
There is no class concept in prototype programming — inheritance is done by cloning an existing prototype. This is the basis of object-oriented javascript languages ​​and other ecmascript dialects, as well as lua or lo. Key Features:
- descendants should not preserve the structural similarity of the prototype (in relation to the class - instance, this happens just like that);
- when copying a prototype, all methods are inherited one to one.
Class-oriented programming focuses on the concepts of class and instance. A class defines the general structure, behavior for instances that adopt them.
Object Oriented Languages
All OOI fully comply with the principles of OOP - the elements are objects that have properties. In this case, there may be additional funds.
OOYA necessarily contains a set of the following elements:
- declaration of classes with fields, methods;
- expansion through inheritance of functions;
- polymorphic behavior.
In addition to the above list, additional tools can be added:
- constructor, destructor, finalizers;
- properties;
- indexers
- access modifiers.
Some OOIs meet all the basic elements, others partially. Still others are hybrid, that is, they are combined with subsystems of other paradigms. Typically, OOP principles can be applied to an object-oriented language too. However, the use of OOI does not make the code object-oriented.
PLs support more than one paradigm. For example, PHP or JavaScript supports functional, procedural, object-oriented programming. Java works with five paradigms: object-oriented, generalized, procedural, aspect-oriented, competitive. C # is considered one of the most successful examples of multi-paradigm. It supports the same approaches as Java, and a reflexive paradigm is added to this list. Such a language tool as Oz is designed to combine all the concepts traditionally associated with various software paradigms.