Macademics: Object Oriented Programming
Updated: Apr 6
Hello everyone! Hope you all are doing well and staying safe during these hard times. For this post, I thought I would take it back to some of the fundamentals of programming and talk about Object Oriented Programming. Curious to learn more? Keep reading.
Object Oriented Programming (OOP) is a type of programming where you define a data type of a data structure and the functions that can be applied to that data structure. In other words, it's a method of programming to design and architect your code base in the form of classes with different functions that allow other classes to access data between each other. Common coding languages that are OOP languages are Java, PHP, C# and C++. The four main principles of Object Oriented Programming are encapsulation, abstraction, inheritance, and polymorphism.
Encapsulation binds together the data and the functions that manipulate the data. Meaning that the fields in an object are private so you cannot access them but there are functions that allow you to manipulate and update the data for that particular object. Those barriers between the data of a class and the functions that manipulate the data are defined by whether or not a variable or function is private, protected, or public. A public function can be accessed anywhere, a protected function can only be access within the class itself and by inheriting or parent classes, or and a private function can only be accessed within the class. Here's an example of encapsulation:
The get and set methods allow other classes to manipulate those variables in the Shampoo class however you cannot directly call those variables in the class at all.
Abstraction is used as a tool used to declare what functions are used in a class that decides to implement this abstract class. It's supposed to create a sense of simplicity while leaving the other classes to actually do the implementation of those operations.
If you're going to implement an Abstract class, you have to call all of the methods that are associated with that abstract class.
Inheritance is what allows an object to get all the methods from the class that it inherits with the option to override the existing implementation of those methods as well as add any additional methods as well. You're getting the best of both worlds.
Our last principle of OOP is polymorphism. Polymorphism is a principle that allows you to call the same function of two different classes that implement the same functions.
Even though getBrand() is implemented in different places, you're still able to get the right value associated with the object.
My #1 tip for looking at a new codebase is to look to see how different classes relate to one another. Is it encapsulation? Do multiple classes implement this one class? What are each of the classes that are calling this one method doing? Object Oriented Programming allows you to create those maps for yourself will make it that much easier to tackle any new code base.