Basic programming, .NET technology.

Lập trình hướng đối tượng – OOP (2)

In my previous article, I shared some of its basic concepts: what is an object? What are classes? How is the constructor defined and used?

Next, in this article, I will present the basic features of object-oriented programming. Everyone knows when it comes to object-oriented programming, any coder, from amateur to professional, must master these characteristics. It's like the inner gong of a sect. Referring to that type of martial arts, one must immediately remember its inner gong and know how to use it. So what is the inner work of the mind of OPP? These are Abstraction, Inheritance, Encapsulation, Polymorphism.

Here we go.

First, Abstraction: It is a feature, an object-oriented principle, used to describe and display the necessary features of an object for other objects. This feature allows hiding the details of the object. It focuses on what the object does rather than how the object does it.

You can picture it like this: We have a dog, it is an object. It has action: go and eat. Dogs also have a way of walking, a dog's way of eating is different from a cat's. Abstraction, describing to the surrounding, the dog has the act of eating and walking. But how to eat, how to walk it is the dog's business, not to be described to the surroundings.
See the following code for more clarity.


Second, Encapsulation.
This means that the details of an object are restricted from being accessed by other objects, or hidden from view by other objects.
Speaking of closures, we need to know a concept called access modifier. It controls access to member data in a class. We have access modifiers: private, public, internal, protected, protected internal.

- Private: only members of the same class can access it.
- Public: unlimited access.
- Protected: only members of the class and the derived class have access.
- Internal: only members in the same assembly.
- Protected internal: members in the same assembly or inherited class are accessible.




Next, let's learn the third, Inheritance.
Defining inheritance in the object-oriented direction is the same as in social inheritance. Everyone must know who inherits, inherits from whom, and inherits what characteristics?
In object-oriented, the class to be inherited is called “superclass” or “base class”, in this class we define common features that will be inherited from subclasses. Subclasses that inherit from the parent class are called "derived classes".
The benefit of inheritance is that it can reuse code, increase system maintainability, as well as reduce the cost of system development. C# does not support multiple inheritances, to do that, we can use an interface.
An example of inheritance: We have a superclass animal: has a name property, a constructor, a move and eat method. Subclasses: dog, cat inherit from an animal class.



Implement base class



Implement derived class: Dog



Use derived class:

                



Finally Polymorphism.
Polymorphism means many forms. When a problem is handled in different ways, we call it polymorphism. We have two types of polymorphism: Compile polymorphism, or overloading, and run-time polymorphism, or overriding.
-  Overloading: a method has many different forms based on its own parameters.



- Overriding by using the keyword: virtual and override
-        



Above, you and I have gone through the 4 main slogans in object-oriented programming. Hopefully, we will memorize and use it in the best way in the Kung-fu code.

Related posts:
Share:

0 nhận xét:

Đăng nhận xét

Featured Posts

Data type 3 - string type