Understanding the key concepts of Object-Oriented Programming is an essential instrument in the toolbox of today’s developers. Most of the programming languages in use today focus on this idea of OOP. In fact, 8 of the 10 Most In-Demand Programming Languages of 2018 support this notion of OOP, including Java and Python which took the number one and number two spots respectively. So, what is OOP? The best way to explain Object-Oriented Programming is to cover the core concepts behind OOP.
Encapsulation – The first concept that I will touch on is this idea of encapsulation. In the most simplistic sense, encapsulation means that data in the program should only be manipulated by its methods. To illustrate, I will use an example that most people are familiar with, a bank account. Now, the user of the bank account should only be able to increase their balance by making a deposit. This deposit is a method of the bank account. Therefore, the user cannot increase their bank account balance by simply adjusting the balance. They must use the deposit method to increase the balance. Encapsulation allows us to negate nefarious manipulation of the data within our program.
Abstraction – Encapsulation and abstraction are linked very closely together because abstraction allows us to create objects and classes to organize our code. Therefore, abstraction allows us to create objects that are clearly defined and separate from other objects in our program. Abstraction also allows us to set the properties and methods of our objects and classes. To illustrate, I will expand on the bank account example from above. With abstraction, we can create a class called “bank account.” And in this class, we can create the methods that are used by the class (i.e. deposit, withdrawal, etc.). The methods in the class will change the property of the object “balance.” Thus, the best way to encapsulate data is to use abstraction.
Inheritance – The idea of inheritance in OOP is similar to the inheritance we see in nature. For instance, in biology, we use the taxonomic rank to classify different species. We start with the Domain, which is the most general classification, working our way to Kingdom, which is slightly more specific. We work our way toward Species, which is the most specific classification. The same principles apply in OOP. We work from the most general to the most specific. The most specific instances of an object inherit properties from their parent classes. Therefore, a button in the Visual Basic language will inherit properties from the button base class. Inheritance in programming, as in nature, is a one-way street. Thus, children can inherit properties from their parents, but not the other way around. Inheritance makes development easier, by allowing developers to reuse code of existing objects to establish properties of children objects.
Polymorphism – Polymorphism may be the most difficult concept for beginners in OOP to grasp. Basically, polymorphism means that methods can have the same name but support different functions. To illustrate the idea of polymorphism I will talk a little bit about overloading. When we overload a method, we are saying that the method has more than one way of being used, and each way supports a different arguments list. This idea is similar to how financial functions work in Excel. For instance, we can call the Future Value function (a method) and depending on the arguments that we pass to the function, the function will perform different calculations. So, in one overload we may pass the argument for payment if we will be making regular payments into our account, and in another instance, we would not pass this argument if we plan to just invest a lump sum and let it compound over time. In short, polymorphism allows us to create overloaded functions which supports the idea of code reusability.
With the heavy concentration of demand for developers with the knowledge of languages like Java and Python, having a grasp on the key concepts of OOP is essential. It is important that beginning developers, such as myself, to truly understand the idea of OOP and the reasoning behind this way of programming. Having a sturdy foundation to build on is the best way to start growing your knowledge about a subject. And for programming, this foundation is OOP and the concepts that it supports. I hope you enjoyed reading this post as much as I did writing it. Please comment below with examples that helped you grasp the concepts of OOP. Thanks for reading.