Last Updated:

Fundamentals of Object-Oriented Programming

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. The use of OOP minimizes redundant data, it improves the 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 such principles as:

  • encapsulation;
  • inheritance;
  • polymorphism.

Some of the principles that were originally laid down in the first OOJ have undergone significant changes.

Examples of object-oriented languages:

  1. Pascal. With the release of Delphi 7 at the official level became known as Delphi. The main area of use of Object Pascal is writing application software.
  2. 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.
  3. 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.
  4. 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.
  5. Objective-C is built on the basis of the C language, and the C code itself is understandable to the Objective-C compiler.
  6. Perl is a high-level interpreted, general-purpose dynamic language. It has rich opportunities for working with text, originally designed specifically for manipulating text. Now it is used in system administration, development, network programming, bioinformatics, etc.
  7. PHP. The abbreviation translates as a hypertext preprocessor. It is used for the development of web applications, in particular the server part. With it, you can create gui-applications using php-GTK, PHP-Qt, WinBinder packages.
  8. Python is a general-purpose language focused on improving developer productivity and code readability. The Cython project was developed, with the help of which programs written in Python are translated into C code.

Abstraction

 

Any book of the kind "Object-oriented programming for dummies" highlights one of the main principles - abstraction. The idea is to divide 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 presentation without specifying the 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. ATD is independent of other code fragments.

A famous aphorism by David Wheeler says: All problems in computer science can be solved at another level of abstraction.

Inheritance

Object-oriented languages are heritable – this is one of the most important principles.

 
Inheritance java

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 originates is called the ancestor, basic, or superclass. The descendant-heir relationship generates a special hierarchy.

There are several types of inheritance:

  • simple;
  • Multiple.

With multiple inheritance, there may be several children from one ancestor, while with a simple one- 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 can see that class Cow inherited all methods from class Animal. Now, if you execute Cow.eat(), we get "the animal is eating", respectively, the draw() method has been changed. Cow.draw() will return "something that looks like a cow" and Animal.draw() will return "just animal".

 

Encapsulation

Encapsulation restricts components' access to others, and binds data to methods for processing. The private access specifier is used for encapsulation.

 

Usually, the concepts of encapsulation and concealment are identified, but some languages distinguish between these concepts. In other words, work-critical properties 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 specified 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 problems.

Polymorphism java

The example above is a table. We see class CardDesk and class GraphicalObject. Both have a function called draw(). It performs different actions, although it has the same name.

Ad hoc polymorphism or special polymorphism uses:

  • overloading functions, methods;
  • Typecasting.

Overloading involves using multiple functions with the same name when the appropriate ones are selected at compile time.

Type conversion refers to converting a value of one type to a value of another type. There is an explicit transformation - a function is applied that takes one type and returns another, implicit - is performed by the compiler or interpreter.

"One Interface, Many Implementations" by Björn Stroustrup.

Class

 

A class is a data type that consists of a single set of fields and methods.

Has internal and external interfaces for content management. Copying through assignment copies the interface, but not the data. Different species interact with each other by:

  • 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 is part of another, it is called aggregation. But when they are still dependent on each other for lifetime, it is a composition.

One of the main characteristics is the scope. The concept is defined differently by different YPs.

Object Pascal describes it as follows:

ClassName = class(SuperClass)

private

{ the use of elements is limited only to the limits of the module }

{ fields } are specified here

strict private

{ the access specifier became available with the release of Delphi 2007, stands for the same as private }

protected

{ elements can be used inside ClassName or when inheriting }

public

{ }

published

{ items are available to everyone, they are displayed in the Object Inspector }

end;

Here, SuperClass is the ancestor from which the inheritance is derived.

For C++, the creation looks like this:

class MyClass: public Parent

{

public:

MyClass(); designer

~MyClass(); destructor

protected:

private:

};

In this example, Parent is the ancestor, if any. The specifiers private, public, protected mean the same thing as in the previous Pascal example. We also see the constructor, the destructor, available for any part of the program. In C++, all elements are private by default, so you can choose not to specify this.

Implementation Features

At the center of object-oriented languages is the object, it is part of the class. It consists of:

  • Fields
  • method.

A data field describes the parameters of an object. They represent a certain value that belongs to a class, describe its state, properties. They are private by default, and data modification occurs through the use of various methods.

A method is a set of functions that define all possible actions that can be performed 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;
  • Prototyping programming;
  • Class-oriented programming.

Component-oriented programming relies on the concept of a component - such a component of the program that is intended for reuse. It is implemented as a set of constructions with a common feature, rules, restrictions. The approach is used in the object-oriented language Java, where component orientation is implemented through "JavaBeans" written according to the same rules.

In prototyping, there is no concept of class - inheritance is made by cloning an existing prototype. It is the basis of the object-oriented languages javascript and other dialects of ecmascript, as well as lua or lo. Main features:

  • descendants should not retain the structural similarity of the prototype (in relation to class - instance, this is how it happens);
  • when copying a prototype, all methods are inherited one by one.

Class-oriented programming focuses on the concept of class and instance. A class defines a common structure, behavior, for the instances that adopt them.

Object-oriented languages

 

All OJS fully comply with the principles of OOP - elements are objects that have properties. At the same time, there may be additional funds.


The OJ necessarily contains a set of the following elements:

  • declaring classes with fields, methods;
  • Expansion by inheriting functions
  • polymorphic behavior.

In addition to the above list, additional tools can be added:

  • constructor, destructor, finalizers;
  • properties;
  • indexers;
  • Access modifiers.

Some OJs meet all the basic elements, others partially. Still others are hybrid, that is, they are combined with subsystems of other paradigms. Generally, OOP principles can be applied to a non-object-oriented language too. However, the use of OOJ does not make the code object-oriented.

JPs support more than one paradigm. For example, PHP or JavaScript support 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-paradigmatics. It supports the same approaches as Java, and a reflective paradigm is added to this list. A JP like Oz is designed to bring together all the concepts traditionally associated with different software paradigms.