Object Oriented Programming<!-- --> | <!-- -->Miguel Ccantuta Leon

Object Oriented Programming


eleonora-albasi-oH-3vcgGP5E-unsplash

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:

  • Attributes
  • Behavior
  • Identity

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).

Object-Oriented Programming basic concepts

Let’s review the main concepts used in Object-Oriented Programming.

Class

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:

  • Attributes: Where the data or relevant information of the class is stored. The data represents the state of the object. We can define an access level from outside the class to the attributes (public, private).
  • Methods: Where class behavior is defined, this expected behavior would be called by other objects. When designing the class methods, we need to define their access level, if those methods will be used only by the same object (private access level) or if those methods will be used by any object (public access level). It is a common practice that the attributes of one class are managed only by that class's methods. That class will not allow attribute modifications directly from other objects.
Class

Encapsulation

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.

Composition

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.

Composition

Inheritance

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.

Inheritance

Composition relationship

  • IS-A Relationship: When a class inherits from another one, we could say that they have an IS-A relationship. For example, when we have the Cat class, which inherit from the Mammal class, we could say that a Cat IS-A Mammal.
  • HAVE-A Relationship: When a class has as attribute another class, we could say that they have a HAVE-A relationship. For example, if the Car class has its attribute to the Engine class, we could say that the Car HAS-A the Engine.

Abstraction

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.

Polymorphism

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.

Constructor

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.

Method overloading

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.

Best practices

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.