Object-oriented programming (OOP) is one of the most important programming techniques today. It is applicable to most practical applications built in businesses. Most popular programming languages and programming frameworks like Java, PHP, .NET, ruby support object-oriented programming. Most programmers have learned about object-oriented programming at university, but the basic principles of object-oriented programming are sometimes unclear leading to misuse and improper programming philosophy. object oriented.
In this article, I will summarize the basic principles of object-oriented programming to help you get an overview of OOP and how to apply it.
Class and object
OOP has 3 basic principles we will explore in detail the following:
Encapsulation is the rule that requires the internal state of an object to be protected and avoid access from external code (i.e. external code cannot directly see and change the state of that object). ). Any access to this internal state is required through a public API to ensure the state of the object is always valid because the public APIs are responsible for performing validity checks as well as the update order. Update the status of the object.
In general, the object status is invalid because: it has not been checked for validity, the steps are performed in the wrong order or are omitted, so there is an important rule in OOP to keep in mind. The object’s internal state is private and only accessible through public / protected method / property. When using objects we do not need to know how they work inside, we just need to know what public APIs are and this ensures that what changes the object will be checked by the internal logic rules. , avoid objects being used incorrectly.
The principle of packaging like this everywhere we can see for example the tablet design, we only know it cures this and that and some other key ingredients that are specific to what it is. totally unknown.
When we start building the application we will start designing the classes, usually we will see that there are cases where some classes seem to be related to others, they have quite similar properties. For example: 3 classes AndroidPhone, IPhone, WindowsPhone
Each class represents a different type of smartphone but has the same properties. Instead of copying these properties, it is better to put them in a place that can be used by other classes. This is done by inheritance in OOP: we can define the parent class - base class (in this case Smartphone) and have subclasses derived from it (derived class), creating a relationship. parent / child system.
Now, subclasses can inherit 3 properties from the parent class. If the functions of the superclass are fully defined, the programmer will not have to do anything in the subclass. If a child class wants a function that is different from the definition in its parent class, it can override the function defined on this parent class.
For most programmers, the Inheritance and Package in OOP are quite easy to understand while the Polymorphism when approaching will find it a bit more difficult to understand. However, this is a property that can be said to contain most of the power of object-oriented programming. Put it simply: Polymorphism is a concept that two or more classes have the same methods but can be implemented in different ways:
AndroidPhone stored with Google Drive Iphone stored on iCloud WindowsPhone uses SkyDrive.
Because all are smartphones, if we write a function that uses the Smartphone type as a parameter, when calling the function we can pass an AndroidPhone, Iphone or WindowsPhone object because they are inherited from the Smartphone class should be accepted. (in a nutshell an AndroidPhone, Iphone, WindowsPhone is also a Smartphone). Besides, this function does not even need to care which smartphone is passed in because it only needs to know that the object that is handling here is Smartphone with the public method / property defined. If the child classes do not redefine (overrides) the CloudStore () method, the CloudStore () method on the parent class (Smartphone) will be called. If the subclass overrides the parent’s CloudStore () method as in the image above, the CloudStore () method on the subclass will be called even though the code in the function is manipulating a Smartphone-like object.
Such polymorphism is a very powerful property because it gives the code a high degree of generalization. We do not need to create methods for each type that inherits from the Smartphone parent class, but simply receives a Smartphone type variable and can work with any class that inherits from it. The only thing not done here is using methods that are only declared on subclasses. For example, if we have a method on the IPhone class called OpenSiri () but is not declared on the Smartphone class, then wanting to call it will be forced to cast from Smartphone to IPhone before calling.
Inheritance-based polymorphism is not always the best option. It is clear that the 3 lower layers (Iphone, Laptop, FingerprintScanner) are all things that are accessible by fingerprint but they perform in different ways. These classes share a common action called biometricAuth (). If we try to combine all three layers into a common layer, it is not good because it is difficult to find their common ground in addition to being accessible by fingerprint.
So instead of using Inheritance here, we can use another technique, Interface. An interface is simply a contract indicating that your code will execute and support a specific public API. However, how these public APIs are implemented is not shown on the interface, but rather on the interface implementation class. Basically the contract is a list of public methods / properties that will certainly be implemented in your class.
Applying the above example, we can create an interface called IBiometricAuth with a method called BiometricAuth (). Next for the Iphone, Laptop, FingerprintScanner classes implement this IBiometricAuth interface as shown below
Because each of the above classes implements the IBiometricAuth interface, we can guarantee that they all have the BiometricAuth () method, and the method declaration will be exactly the same as the one defined on the IBiometricAuth interface. Similar to Polymorphic Inheritance, using Interface allows us to declare methods that accept parameters of type IBiometricAuth but accept any objects passed that its type implements this interface IBiometricAuth. The IBiometricAuth implementation class does not need to have the same parent class except the IBiometricAuth interface. In the above method, we can call any method defined on the interface IBiometricAuth of the object that is passed in, regardless of the actual type: No matter whether it is an Iphone, a Laptop, or FingerprintScanner, as long as it supports the IBiometricAuth interface, can call the BiometricAuth () method, which is extremely flexible and flexible.