Introduction to Object Oriented Programming

Author
By Darío Rivera
Posted On in POO

Object-oriented programming as a programming paradigm aims to model real-world objects to solve problems that can be represented in a computer program.

Each real-world object has inherent properties (state), for example, a basketball has properties such as radius, weight, and color. It is also easy to see in each object a group of actions that can be performed (behavior); following the same example of our ball, it can bounce, roll, and spin; note that the actions performed by an object are verbs. The aforementioned properties are usually called attributes, the actions methods, and this set of characteristics.

caracteristicas_objetos.png
Characteristics of an object

In addition to representing the characteristics of each object, we can define a general model to represent objects of the same type, we will call this model a class. Just as a basketball belongs to the "basketballs" class we can identify any object with a class. Classes observed from this point of view are factories of objects, which help us abstract the concept of the object to be able to have an informatics representation of the real world. With classes, we can efficiently create objects by selecting the desired attributes.

fabrica_objetos.png
Class as a factory of objects

Within the object-oriented programming paradigm, we find fundamental concepts such as abstraction, inheritance, polymorphism, modularity and encapsulation. Next, we will see what each one means.

Abstraction

Abstraction is taking the most important characteristics of an object to form a model. This concept of object-oriented programming is closely related to the concept of class, as it is precisely the classes that allow us to represent objects with those characteristics that we have defined and that interest us. For example, we can abstract the concept of a laptop, as an object that has properties such as brand, model, memory, and O.S, and actions such as turn on, run programs, and turn off.

diagrama_de_clase_laptop.png
UML notation for Laptop class

For now, it is enough to know that there is a standard notation for object-oriented modeling called UML (Unified Modeling Language). The above image represents the class diagram for the Laptop class.

Inheritance

An object inherits by default the attributes and methods of the class that created it. However, a class can take these same characteristics from another class already defined, this is what we call inheritance. For example, let's imagine the Appliances class that encompasses another class called Washing Machines. The Appliances class will be called the superclass since it has attributes that are much more general than the Washing Machines class, in turn, it is said that Washing Machines is a subclass of Appliances. An object of the Washing Machines class will inherit all the characteristics of its class and those of its parent class, in this case Appliances. Note that one or more classes can inherit from the Appliances class, for example, a TVs class.

Class diagram inheritance
Representation of inheritance in UML

Polymorphism

Several objects can execute similar actions in different ways. Let's imagine several balls from different sports that perform the bounce action. It is evident that a basketball will bounce differently than a soccer ball, or a tennis ball. This shows us that different objects can have the same methods but implemented differently, this is known as polymorphism.

Encapsulation

Let's think about some real-world objects like a television or a cell phone. Both electronic devices implement their functionality internally in complex electronic circuits, but is it really necessary for the user to understand how all this works? The answer is NO! Both a mobile device and a television use electronic principles to create user-friendly interfaces and quickly display information, the how this is done inside the device is of interest only to the developer, it is then said that these objects have hidden characteristics and this is called encapsulation (hiding information). However, many other characteristics must be present for the user, such as, how to turn on the device? for this, the user must have switches, volume controls, among others, we will call this interfaces.

Modularity

Modularity refers to dividing an application into smaller parts called modules. Each module can be understood as a part of the program that performs a function perfectly differentiated from the others. For example, in a software that controls the finances of a small business, there will surely be a billing module, one for customers, inventory, among others.


Acerca de Darío Rivera

Author

Application Architect at Elentra Corp . Quality developer and passionate learner with 10+ years of experience in web technologies. Creator of EasyHttp , an standard way to consume HTTP Clients.

LinkedIn Twitter Instagram

Sólo aquellos que han alcanzado el éxito saben que siempre estuvo a un paso del momento en que pensaron renunciar.