Understanding Object Oriented Programming
One of the most important concepts in programming is Object Oriented Programming regardless of your programming language. Once you got the core idea of how the concept operates, your code becomes more readable, clean, maintainable, and most importantly DRY. In this post, I will try to interpret that particular concept to be easy for you to remember and apply to any model whatever the language is.
Object Oriented Programming meaning
Object Orient Programming has four pillars which are Encapsulation, abstraction, inheritance, and polymorphism. These pillars are the heart of the OOP concept. So, we will dive into each pillar and understand what it implies.
In OOP, each object is an independent unit with a unique identity, just as objects in the real world are. For instance, both a desk and a chair are objects, but they are different in shape, color, and price though — which we call it an identity. Each identity has some characteristics or attributes which describe each object and differentiate it from other objects like, for example, color, price, and shape. An object has behavior too, for example, a dog can walk, bark, and so on.
Each object has three dimensions: identity, attributes, and behavior.
Attributes describe the object’s current state, and what the object is capable of doing is demonstrated through the object’s behavior.
Encapsulation
Encapsulation means the action of enclosing something in or as if in a capsule. From OOP point of view, encapsulation means to keep each object’s state private inside the class. Other objects don’t have access to this state. Instead, they can only invoke a list of public methods or functions. So, in that case, the object manages its state using methods. So, for instance, if we have two different classes of a dog and a cat objects, the dog object cannot access the cat’s methods. In real life, a dog can only bark and cannot do what cats can do.
Abstraction
Abstraction means the act of considering something as a general characteristic, apart from specific objects, or actual instances. Mobile phone devices are complicated. However, what you have to do is to press a button. The home button in your phone works like a public method that used for several functions on the phone itself. For instance, the home button can be used to take a screenshot, open the phone, recognize fingerprints, and so on. We used abstraction in large-scale programs to maintain a large code based applications. Applying abstraction means that each object should only expose a high-level mechanism for using it.
This mechanism should hide internal implementation details. It should only show operations connected with the other objects.
Using abstraction should be easy to use and should rarely change over time. Think of it as a small set of public methods which any other class can call without “understanding” how they work.
Inheritance
Inheritance means the genetic characters transmitted from parent to children, taken collectively. In programming, we might face a situation that we have multiple objects that share the same logic, but they are not the same. For example, your children share some characteristics like skin color, but they don’t share the same eye color. However, they are a mixture of features of their parents. So in programming, we can apply that concept to classes. The child class reuses all fields and methods of the parent class (common part) and can implement its own (unique part).This way — we form a hierarchy, each class adds only what is necessary for it while reusing common logic with the parent classes.
It means that you create a (child) class by deriving from another (parent) class.
Polymorphism
Generally, the ability to appear in many forms. In object-oriented programming, polymorphism refers to a programming language’s ability to process objects differently depending on their data type or class. More particularly, it is the ability to redefine methods for derived classes. For instance, given a base class shape, polymorphism enables the programmer to define different area methods for any number of derived classes, such as circles, rectangles, and triangles. No matter what shape an object is, applying the area method to it will return the correct results. Polymorphism is considered to be a requirement of any object-oriented programming language (OOP).
If you are interested to see more examples of Polymorphism check that link on stackoverflow
Conclusion
Object-Oriented Programming concept is a common topic for the software or web developer interview. So, make sure that you fully understand and digest the four principals of OOP. I hope that I could give you a better understanding of what is the OOP concept is. If you liked the post, please give me a 👏 👏 👏 👏 👏 or leave a comment. please follow me on twitter on @salmaeng