What is polymorphism in general?

Giving a definition as simple as it theoretically possible, polymorphism is an ability to treat objects of different types as if they are of a same type. There are several types of polymorphism and different methods of achieving polymorphic behavior.

Academic definition

According to Bjarne Stroustrup, father of C++ language,

polymorphism — providing a single interface to entities of different types. virtual functions provide dynamic (run-time) polymorphism through an interface provided by a base class. Overloaded functions and templates provide static (compile-time) polymorphism.

Breaking down the Stroustrup’s definition, polymorphism can be divided in two categories:


Definition

Inheritance is one of the fundamental OOP principles. According to it, class can use variables and methods of another class as its own.

Class that inherits data is called subclass, derived class, or child. Class from which data or methods are inherited is called super class, base class, or parent. Terms “parent” and “child” are extremely helpful in understanding of inheritance. Just as child gets characteristics of his or her parents, derived class gets methods and variables from base class.

Inheritance might be highly useful because it allows to reuse previously written code, which in turn can significantly speed up…


Definition

Encapsulation is a set of tools which can restrict access to data or methods that can manipulate it. Detailed definition of encapsulation can be found in my previous Medium post here. This article is focused on examples of encapsulation in C++ and C programming languages.

Encapsulation in C++

By default, data and methods in class are private — visible and modifiable only by object/class. Access level can be set to other values using appropriate key words provided by C++.

In C++, several access level specifiers are available, and they make data:


Definition

The definition of encapsulation is vague, and it differs dramatically from source to source. It is generally agreed that encapsulation is one of the fundamental OOP principles, however, several definitions of them omit encapsulation completely. For example, John C. Mitchell in his Concepts in programming languages states that main OOP language concepts are dynamic lookup, subtyping, inheritance, and abstraction, which is close to encapsulation by meaning, but is generally seen as more broad and high-level concept. On the other hand, Robert C. …


Typing

All programming languages can be divided in two major groups: statically or dynamically typed. In spite of some exceptions, the simple rule can be followed:

  • Interpreted languages are mostly dynamically typed languages. Their variable’s type can be freely changed.

Examples of static and dynamic typing

C++ (static):

#include <iostream>
#include <typeinfo>
using namespace std;
int main()
{
int a = 10; // type of 'a' = int
a = 'A'; // type of 'a' = int
cout << "Type of 'a' is: " << typeid(a).name() << endl; return 0;
}

Output:
Type…

Kateryna Bondarenko

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store