What is Object-Oriented Programming? Essential Knowledge About OOP

What is Object-Oriented Programming?

Object-Oriented Programming (OOP) is a programming technique that allows programmers to create objects in the code. These objects are abstracted from real-life entities.

What is Object-Oriented Programming? Essential Knowledge About OOP

Basic Concepts in Object-Oriented Programming

1: Object (Object)

An object represents things or events with similar characteristics, attributes, and actions, such as humans, phones, computers, etc.

Common characteristics include two main components:

Attribute (Attribute): These are the information and characteristics of the object.

For example, a computer's attributes include color, size, memory, etc.; or a person may have features such as eyes, nose, hair, ears, age, preferences, etc.

Method (Method): These are the actions that an object can perform.
For example, a computer's methods include turning off, turning on, scanning for viruses, etc.; human methods include actions like eating, talking, walking, etc.

2: Class (Class)

Objects with similar characteristics are grouped into a class of objects (class).

A class is understood as a data type, also including attributes and methods.

The Difference Between Objects and Classes

A class is a template, while an object is a specific instance that represents the state & behavior based on that template.

For example:

With common human features like two legs, two arms, eyes, nose, mouth, hair, etc., and actions like walking, standing, sitting, lying down, laughing, etc.,

So if it is a human, it will have the above features. When talking about humans, the class is the template of humans will have those common features and are designed similarly to the template. The object is specifically about girl A, girl B, girl C, etc.

The emergence of the concepts of objects and classes is a characteristic of the object-oriented programming method. This programming form addresses the shortcomings of the previous programming method (structured programming) left behind. These two concepts also help to better represent the real world on computers.

What are the Characteristics of Object-Oriented Programming?

To program and design programs according to the object-oriented programming method, you need to understand clearly about 4 properties: encapsulation, abstraction, inheritance, and polymorphism.

1: Abstraction (Abstraction)

Abstraction simplifies the information in an object. It allows us to communicate with the components of the object without needing to know how those components are built up.

For example: When you ride a scooter, the action of accelerating helps the vehicle to speed up. At that time, the function of accelerating is abstract. Users only need to know to twist it to accelerate without knowing how the acceleration principle of the vehicle works.

When writing a program in an object-oriented way, the design of the object will be distilled into common features. Then, those features will be abstracted into interfaces and designed to see how they will interact with each other.

2: Encapsulation (Encapsulation)

Related data and methods will be encapsulated into classes for convenient use and management. That is, each class will be built to perform a group of its own class's characteristic functions.

Besides, encapsulation allows hiding the information of the object by combining information and methods related to the information for the object.

This property is similar to reality, you cannot know the real attributes of them unless they express it (such as personality, hobbies, other private information, etc.). That person may tell you that they like roses, like to eat fish, are 20 years old, etc., but that may not be their real attribute. Similar to getters not returning the real value of the attribute but giving a different value.

Advantages of encapsulation:

  • Limits invalid access to the object's attributes.
  • Helps keep the state of the object correct.
  • Hides unnecessary information of the object.
  • Allows changes to the internal structure of the class without affecting other classes.

3: Inheritance (Inheritance)

When programming, you will see many cases where many objects share certain attributes and methods.

For example: You write a program to save information for students & teachers. Students need to save name, age, address, test scores; teachers save name, age, address, number of teaching sessions, salary, etc. So there will be duplicate code in the same attributes (specifically like name, age, address or both setter, getter, etc.). This also violates one of the most basic principles of programming, which is DRY – Don’t Repeat Yourself (never repeat code).

Thanks to inheritance, this problem will be solved. Inheritance in object-oriented programming will inherit the attributes and methods of a class. That is, if class A inherits class B, it will have the attributes and methods of class B.

4: Polymorphism (Polymorphism)

Polymorphism is understood as in each situation, each case, objects will play different roles.

For example: A person in the company has the

 role of an employee, shopping is a customer, going to school is a student, etc. The same person but in each situation will have a different role. This is an example of polymorphism in reality.

In programming, an object or method will have more than one form or is polymorphic. Polymorphism will be displayed in 3 forms:

  • Method overloading: For example, adding two integers (1+2), adding two real numbers (2.1+ 2.2) and adding three integers (1+2+3). All are adding integers or adding 2 numbers but 3 results are different, specifically 3, 4.3 and 6.
  • Method overriding: This form is often used to calculate salary for objects. Each object will have a different way of calculating and a different result.

Through polymorphic objects: You can imagine the variables of the parent class as polymorphic, when referring to the object of the child class will also be polymorphic.

What are the Advantages of Object-Oriented Programming?

From the analysis of the characteristics above, you will also see that object-oriented programming has many advantages such as:

Inheritance helps the process of describing eliminate repetitive, redundant programs. It helps expand the usability of classes without needing to be performed again, optimizing and reusing code effectively.

  • Shortens the system construction time, increases productivity.
  • Objects and classes appear to address the shortcomings of the structured programming method, while also helping to better represent the real world on computers.