Object Oriented Programming (OOP) is one of the most important and widely used programming techniques today. Most of today’s programming languages such as Java, PHP, .NET, Ruby, Python… all support OOP. So what is object-oriented programming? And what are the basic principles in OOP to know?
What is Object Oriented Programming (OOP)?
Object-oriented programming (OOP) is a programming technique that allows programmers to create objects in code that abstracts away objects.
Watching: What is Object Oriented Programming
are things and events that have the same properties, characteristics, and actions that we collect into the same object in real life. When programming OOP, we will define classes to collect (model) actual objects.
An object consists of two pieces of information: properties and methods.
In general, the invalid object state is usually caused by: not being checked for validity, steps taken in the wrong order or being skipped, so in OOP there is an important rule to remember that is to always declare The internal state of the object is private and only accessible via public/protected methods/property.
For example we see a cold medicine. We only know that it cures colds, headaches, and some main ingredients, but what specific active ingredients are inside it is completely unknown.
It allows to construct a new class based on existing class definitions. This means that the parent class can share data and methods with the child classes. Subclasses do not have to be redefined, in addition, they can extend the inherited components and add new components. Reuse the source code in an optimal way, taking advantage of the source code. Some common types of inheritance: single inheritance, multiple inheritance, multilevel inheritance, hierarchical inheritance.
When we start building the application, we will start designing the definition of the classes first. Usually some classes are related to other classes, they have similar properties.
For example: 2 layers Android, iPhone
Each layer represents a different type of smartphone but has the same properties such as calling, texting, taking pictures. Instead of copying these properties, we should put them in a common class called the superclass. We can define a superclass – in this case, Smartphone and have subclasses that inherit from it, creating a parent/child relationship.
Polymorphism is an action that can be performed in many different ways. This is again a property that can be said to contain most of the power of object-oriented programming.
See also: How to Fix Bug Splat, League of Legends Guide
In simpler terms: Polymorphism is a concept where two or more classes have the same methods but can be implemented in different ways.
For example, in the above example, each smartphone inherits from the Smartphone superclass but can store data in the cloud in different ways. Android stored using Google Drive iPhone stored on iCloud.
An example of polymorphism in practice. We have two animals: dog and cat. Both of these animals are class animals. But when we tell both animals to meow, the dog will woof and the cat will meow.
So in the example dogs and cats are treated as objects. 2 animals can understand the same call but in different ways.
Abstraction means to generalize something up, without paying attention to the details inside. It doesn’t care what the details are inside and people still understand it every time they hear about it.
For example: If you ride a scooter, the action is to increase the throttle to accelerate, then the throttle function represents abstraction. Users only need to know that when accelerating, the car accelerates, no matter how it is inside.
Here in OOP programming, abstraction means choosing the properties and methods of the object needed to solve the problem you are programming. Because an object has a lot of method properties, but for a particular problem, it is not necessary to select all.
For example: In the student management problem, we only need to manage information such as
Full nameDate of BirthGender…Test Score
without the need to manage additional information:
Because they really aren’t necessary.
See also: What is Delegate – Definition, Example, Explanation
The advantages of object-oriented programming
Based on the principle of inheritance, in the process of describing classes, it is possible to eliminate repetitive and redundant programs. And can extend the usability of classes without reimplementation. Optimizing and reusing code effectively. Guaranteed to shorten system construction time and increase performance. The appearance of two new concepts, class and object, is characteristic of oriented programming methods. subject. It has solved the shortcomings of the structure-oriented programming method. In addition, these two concepts have helped to better represent the real world on computers.
Some articles you may be interested in: