Object-oriented programming

Object Oriented Programming (OOP) is an object-based paradigm of computer programming. The main idea is to decompose tasks into objects, where object is combination of data and methods that manipulate this data.

Main principles of OOP:

  1. encapsulation - union data and logic to one unit and hide data from external access. As a result, we have reducing couplings between software components and have more readable and stable code.
  2. inheritance - the ability to define a new classes as extensions of an existing class. As a result, we reuse existing code and can use child classes wherever the parent class can be used.
  3. composition - the ability to include other objects as part of object. As a result, we
    1. reuse existing code
    2. can change the functionality of the object at runtime
    3. can hide some functionality of included objects from user
  4. polymorphism - the ability to override parent's method in child classes. As a result, we can create specialized classes that respond differently to the same message.
  5. abstraction - the ability to expose the main functionality and leave the implementation of the details to the child classes. As a result, we can change the implementation without affecting the existing code.


There is question how properly to decompose program into objects.

In a simple case, object can reflect an ordinary data structure or process like function. In the latter case, data fields play the role of arguments, result, or store the current state of the process.

There are many patterns that solve common problems:

  • design patterns that provide low-level solutions
  • architectural patterns that provide a general solutions to a commonly occurring problem in software architecture within a given context

You can use design principles for testing quality of your classes.

prototype and class

There are two basic ways how to create new objects.

The first way is to create copy from the prototype object. For example, JavaScript is prototype-based language.

The second way is to describe objects as a class and then create concrete instances of that class. For example C++ and Java are class-based languages. Class itself can be just blueprint as in C++, or be special object as in Java.


A method is procedure or function associated with a class or object.

A field is a particular piece of data encapsulated within a class or object. It is also known as attribute/property. Sometimes property means field with associated get()/set() methods.

Both, methods and fields are members of class.

There are common access rights to the fields and methods:

  • public - everybody can access
  • protected - objects of this class and derived classes have access
  • private - only objects of this class have access

special members

Constructors are special methods that are used to create and initialize an object. Constructor without arguments is called empty or default constructor. A constructor with an argument of the same class is called a copy constructor.

Destructor is a special method used when the lifetime of an object ends. For example, finalize() method in Java can be considered as destructor.

Languages can support a special field that refers to the current object. In Java, this is done with the this keyword.

Languages can support a special field that refers to the parent object. In Java, this is done with the super keyword. This is useful when a child class overrides a parent method but wants to retain the functionality of the parent method.

static members

Static data are fields that are shared between all instances of class. In other words only one variable is created in the memory even if there are many objects of that class.

Static methods are part of the class definition, but not part of the objects it creates. Thus, static methods can use static data and cannot use instance data.

Good practice is avoid using public mutable static data.


Inheritance is the definition of new classes as extensions of an existing class. The new classes are called child or derived classes. The existing classes are called parent classes.

Multiple inheritance is a feature to define a new class as extensions of several existing classes. For example, C++ support this feature, and Java does not.

There is the diamond problem that arises when two classes B and C inherit from A, and class D inherits from both B and C. If there is a method in A that B and C have overridden, and D does not override it, then which version of the method does D inherit: that of B, or that of C? Solving of this problem depends from language, for example C++ support virtual inheritance, and Java throw a compile error.

protocol, mixin, trait

Protocol aka interface describes the behavior of objects or the way of interaction, without implementation.

Mixin is a class that adds new functionality to other classes without being a parent class. How those other classes gain access to the mixin's methods depends on the language. Mixins are sometimes described as being "included" rather than "inherited". They are useful:

  • to provide a lot of optional features for a class
  • to use one particular feature in a lot of different classes

Trait is a set of methods that will be included to other classes. Unlike interfaces functionality already implemented. Unlike mixin trait usually does not have associated states (datas).

Technically, the difference may not be visible. For example, Java 8 supports interfaces with a default implementation. And there may be no data in the mixin. Thus, a Java interface with default implemented methods can be thought as a trait or mixin.


Reflection is the ability of a process to examine, introspect, and modify its own structure and behavior. In other words, reflection allows inspection of classes, interfaces, fields and methods at runtime without knowing the names of the interfaces, fields, methods at compile time. It also allows instantiation of new objects and invocation of methods.