Object-Oriented Programming (OOP) is a computer programming paradigm that represents reality with objects.
It is easy for all of us to think in objects. Anything we are talking about is an “object”. For example, when we leave home, we see the door, using the key, getting into the car. In those actions, we were using three objects: Door, Key, and Car. Each object has attributes or characteristics and behavior. For example, the Car has attributes like color, model, etc. Likewise, the car has the behavior like start the engine, moving, shut down the engine, etc.
Therefore, in Object-Oriented Programming, an object has:
This characteristic is different from Procedural Programming. In OOP, the object has its attributes and behavior included, but these are generally on different Procedural Programming places.
The data stored in an object in their attributes represents the object's state, but an object is more than a data structure. An object has methods that are used to done actions over the data. Also, it is possible to limit the access to the object members, it means limiting the access to the object attributes and their methods (behavior).
Let’s review the main concepts used in Object-Oriented Programming.
A Class is a blueprint for object creation. It is the abstraction of all common things of an object. Each created object from a specific class is called an instance of that class.
The classes have:
Encapsulation is characteristic of Object-Oriented Programming when an object has to expose only the information (interface) that other objects need to interact with. The attributes of an object and the implementation details of its methods will keep hidden.
It is a class relation type. The composition implies that a class can be composed of other classes. It means when an object of that class has been created, it will have some other object instances as part of its attributes.
Inheritance is a very important characteristic of the OOP. It allows a class to inherit the attributes and methods of another class. In this manner, in a single class, we can have the common attributes and behavior that other classes have, enabling their reusing.
The class with common attributes and behavior is denominated Super-Class, and the classes that inherited the common characteristics are called Sub-Classes. The programming languages like Java and .NET only support having single inheritance, which means that one class can only inherit a single class, other languages like C++ support multiple inheritances.
It means isolating or grouping all the relevant information that belongs to a concept in an Object-Oriented Model. The goal of apply Abstraction is to reduce the complexity of a model by isolating the common or relevant information in a class. Then we could create classes from the abstracted class that inherits it and customize it to have a specialized behavior.
It is related to class inheritance. In a class hierarchy, all the subclasses inherit the supper-class attributes and methods; however, each sub-class can re-define its behavior, overwriting some inherited methods. In this way, each sub-class could have different behavior which could be more appropriate to their needs.
When we create an object, by default, the execution runtime calls the constructor. The constructor's main characteristic is that it has the same name that the class, and it does not return any value. The constructor has to have all the needed to create an object with a stable and safe state, which means that the object attributes have the required values to perform their behavior.
If we do not create a constructor, the programming languages add one by default, but it is a best practice to add a constructor, so this does not do any action. It is possible to create more than one constructor; this is useful when different object creation is needed.
We could have more than one method with the same name, as long as they have different input parameters when the method signature is different. This is named method overloading; it depends on the programming language. Each programming language defines what a different method signature is; some consider the returned values as part of the method signature, others not.
It is better to use class inheritance when an object behaves like another one. We should not use inheritance when the relation between those objects is only an IS-A relationship. That is closely related to the Liskov Substitution Principle (SOLID principles), which defines that a class subtype has to have the capability to replace the parent class.