Shortcut to seniority
Home
Go to main page
Section level: Junior
A journey into the programming realm
Section level: Intermediate
The point of no return
Section level: Senior
Leaping into the unknown
Go to main page
A journey into the programming realm
The point of no return
Leaping into the unknown
What are the most important Object Oriented Programming Concepts, you ask?
But first..what is a class?
A class is a blueprint, a model, a template – which describes some features.
We humans are different from each other, but we have some common features, such as a name and an age, and we can all perform some actions (eating, sleeping).
The class functionality is divided in two: Structure and Behavior.
The structure of a class (properties / fields / data members / attributes) refers to the class data. In our case, it would refer to our name, or our age.
The behavior of a class is defined using methods / functions / subroutines, which have the ability to alter the state of an object, or simply provide ways of accessing it. In our case, it would refer to actions that we can perform, such as eating or sleeping.
Let's see an example of code if we were to create a basic human class.
class Human {
// structure
string name;
int age;
// behavior
void eat();
void sleep();
};
The operation of creating an object from a class is called to instantiate, or to create an instance of the class.
Because each individual has a different name and age, we will use the class name to create an object (instance of a class) and assign some values to these attributes only for that specific instance.
Human individual;
individual.name = "Robert";
individual.age = 28;
individual.eat();
Encapsulation is achieved when each object keeps its state (attributes) private, inside the class. Therefore, other objects do not have direct access to the state, and can modify them only by calling the methods exposed as public.
If encapsulation is not properly made, others will have access to our private fields, and we don’t want that.
Software gets extremely large, and we need to maintain a large code base for years – codebase which changes along the way. Abstraction is a process of hiding irrelevant details from the user (to hide implementation details). We don’t care how things are done – we just want them done. We want a car, but we don’t care how the parts are assembled. Through this, the internal implementation can change, and nobody will know or be affected by it.
Objects can be similar, can share common logic, but not be exactly the same….so we want to reuse the common logic, while still keeping things separate. Therefore, we extract that common logic into a separate class, and we inherit from it, forming a hierarchy.
In the end, we will have a parent class (the common logic) and a child class (the derived one), which can implement its own, unique part.
The etymology of the polymorphism comes from ancient greek, and it refers to the ability to assume different forms or shapes. What if we want to use a collection, which contains a mix of classes that inherit from a common class, and we want to call the same method for each of them?
With polymorphism, we can use a child class exactly like its parent, but they keep their own implementation. To do so, we create specific classes called interfaces. These classes usually contain abstract methods, which are functions with a signature only, and no implementation body. Subclasses that inherit from this class must provide an implementation for the abstract methods. Interfaces can also contain common implementation, for reusability purposes. When the functions are called through the base class, the object type is evaluated and the right method will be called.
Data race occurs when more than two threads access the same memory and one of them is a write.
Polymorphism – “poly” (many) + “morphe” (form)
Binary search: split the range in half and check it from there. Based on the result, you will reduce half of the work by knowing in which half is the problem contained.