OOP – Object Oriented Programming

Tram Ho

Today I will share a topic that is very familiar to most programmers. That is object-oriented programming, the content will include the following parts: definition, 4 oop properties, demo code. Hope to discuss with you.

No longer waiting, let’s get started!

1. Definition

OOP stands for Object-Oriented Programming which translates to object-oriented programming. Object-oriented programming is a technique and programming method that uses objects to build applications. Most languages such as: C#, Java, PHP, Ruby… support object-oriented programming.

Note:  Object-oriented programming (OOP) is a technique and programming method and each programmer will have a different implementation and application depending on each project, depending on experience. In addition, it is not required to apply OOP to the project, can use Structured Function-Oriented Programming (POP) or other methods.

2. Benefits

As introduced in part 1, in addition to OOP, there are other programming methods. So why is OOP so popular and popular in programming?

OOP has the following benefits:

  • The OOP approach is very practical. Easy to model problems, objects outside of life into code. For example, in real life we have a developer entity, which takes actions to get requirements, write code, and deploy products. From there we can create Developer class with methods: AnalyzeRequirement, WriteCode, Deploy respectively.
  • Easy to maintain, easy to extend, and cleaner looking code. About this advantage, after going through the properties of OOP in the next section, you will understand better.

Note:  Whether the application of OOP helps to clean code, easy to reuse, easy to scale depends on each person’s experience and thinking in applying OOP techniques. The same technique will have different good and bad ways.

3. Class and Object

In OOP,  Class is a data type, a template that helps to model actual objects. The class will have properties and methods corresponding to the actual external properties and actions.

Eg:

If we consider Class to be a template, then Object is an instance created from that template.

For example, the Developer Class will have instances of Objects such as: BackendDeveloper, FrontendDeveloper, FullStackDeveloper.

Next we will learn together the 4 properties of object-oriented programming (OOP).

4. Encapsulation

Encapsulation, also known as ** hiding information**, helps to group properties, methods and many other components into an object or unit.

Encapsulation is implemented using the access modifier: public, private, protected, internal.

  • public: Accessible from anywhere
  • private: Accessible only inside the class
  • protected: Can only be accessed within the class and classes that inherit from that class
  • internal: Same as public but limited to 1 assembly.

Intenal explanation: Simply put, we have an application U that calls an external library L. In this library L has a class C that declares internal. Other classes in this L library can access this C class, but the U application cannot because of the different assembly.

Eg:

5. Inheritance

Inheritance allows creating a subclass from an existing class and extending that class. Subclasses can inherit the properties and methods of the parent class, and may not need to redefine methods or override or add their own methods. my class. This feature helps to reuse, make use of existing source code.

Eg:

6. Polymorphism

Polymorphism allows an action to be performed in different ways.

There are two ways to use polymorphism:

  • Method overloading (compile time polymorphism): In a class methods have the same name but different return types and parameters passed (number, type) Example:

  • Method overriding (runtime polymorphism): Methods are implemented in subclasses that inherit from the parent class (base class). The implementation inside each layer varies depending on the business logic. Only at runtime will we know which method the object will use. Eg:

7. Abstraction

Compared with the above 3 properties, the abstraction to explain is a bit difficult to understand because it is very abstract.

Abstraction allows generalization of an object. That is, hiding the detailed information inside, only showing the outside information. And looking at that external information we can understand what that object does.

This property is demonstrated through the use of interface or abstract class.

Note: Abstraction spell – Hide details, show overview.

For example, when performing a checkout action there will be multiple steps and transactions within the action. By breaking down the business into each method and using the interface, we can easily understand the overview of the steps and actions when paying. Without going into detail what each action does.

Summary: Through this article, we have learned the basics of object-oriented programming, its properties and demo code examples. OOP programming theory is the same, but depending on the experience and thinking, each programmer will have a different understanding and implementation. Hope this article will help those who are new to OOP object-oriented programming.

If you have any questions or issues that you would like to discuss, please leave a comment below the article. Hope to discuss with you.

If you find this article interesting and useful, please give me a thumbs up. Thanks and have a great weekend guys!

Share the news now