First, you’re probably wondering, “What Is Object-Oriented Programming?”, well according to Wikipedia —
“Object-oriented programming (OOP) is a programming paradigm based on the concept of “objects”, which can contain data and code: data in the form of fields (often known as attributes or properties), and code, in the form of procedures (often known as methods).”
Think of OOP as making breakfast! The properties for breakfast would be — eggs, flour, meat, water, coffee-grinds, etc. The methods along with it would be — makeCoffee(), preparePan(), and whiskEggs(). OOP is similar to this because the properties in our object are like the ingredients and the methods are the recipe instructions!
Now, let’s talk about the 4 pillars of Object-Oriented Programming!
the provision of an interface for a piece of software or hardware to allow or simplify access for the user.
In Object-Oriented Programming or OOP, encapsulation is achieved when each object keeps it’s state private, inside a class. Other objects can have access to this object’s state by only calling a list of methods.
Here, the state is not tampered with, and no other class can touch the state. When communication is necessary, you must use the methods provided by the object. The state cannot be touched because it is “private ”, so our class object can call on its methods and variables whenever, and will always have its state stay the same!
In order to gain access to the Parent class method is by defining “public methods”, this gives another class a way to access the private properties and change the state within them, while preserving the state of the Parent class.
Think of encapsulation as a blueprint t creating a car. The instructions to get you started are the private variables. It does not tell you exactly how to build the car, so you must use public methods and call on the variables within the blueprint to design your own version of a car!
It is the process of taking away or removing characteristics from something in order to reduce it to a set of essential characteristics.
In Object-Oriented Programming, the benefits of abstraction are:
Interface Simplicity: An object with few exposed properties and methods will make for a simpler user interface. Only revealing simple implementations like “the home button n your phone” or “turning to different channels with just a button” on your TV and hiding the complex functionalities hidden, like for example a user's password or an internal mechanics of a coffee machine.
Reduce Impactful Changes: Meaning, if changes to the code were done for whatever reason, these changes of that object would be hidden from the outside. This is necessary to preserve the complexity of data, by showing the necessary information which can also increase efficiency.
Abstraction is, if you were to describe to another parent at a park which child is yours. You would proceed to mention features like — hair color, clothing, height, and facial features — however, you would hopefully leave out the personal details about your child like — their favorite food, where they attend school, etc —
A mechanism to eliminate redundant code!
Inheritance’s job is to reduce the use of redefining the same logic as we add on unique logic. Inheritance gives us the ability to create child classes to inherit the variables and methods we need from the parent class while adding in a unique variable or method to that child class.
A way to think about Inheritance would be a family tree diagram. Where the grandparents are at the top, their children inherits features and characteristics from them along with added unique features. The same goes down the line to their children, inheriting their parent’s features along with special unique characteristics.
Is a concept that refers to the ability of a variable, function, or object to take on multiple forms.
To break it down, “Poly” means “many” and “morph” means “forms”. When we have a Parent class with a few Child classes inheriting from it, usually you will find yourself wanting to use a method that's implemented for the parent, yet we also want it implemented for the children as well. Thankfully, polymorphism makes that possible by allowing us to perform a single action in different ways.
An example of Polymorphism is a Parent class Shape with children classes of Square, Rectangle and, Triangle. The class Shape can hold all the needed interfaces for the children to do their own implementations. For example, the Shape class could have a method called calculateArea(), and that method can be inherited by all the children of Shape, which they can use to calculate the area of their shapes.
That concludes my simplified version of Object-Oriented Programming! I hope this was helpful in understanding the basics of OOP!