A Brief Look At Object-Oriented Programming

Ryan Schleck
5 min readFeb 21, 2022

--

The term Object-Oriented Programming (or OOP) has been around since the late 1950s. Since its inception it has become an extremely popular programming paradigm and is a key feature in many programming languages such as: C++, Java, Python, and Ruby to name a few. In this article we will briefly define it, go into some of its key features, its 4 main principles…and perhaps some fun examples involving D&D!

That’s right. I’m about to publish the nerdiest thing on the internet!

A Summary Of Object-Oriented Programming

OOP can be described as a style of programming involving classes, objects, attributes, and methods. Lets break down each of these and take a look at how they fit together to better understand these concepts.

  • Classes: A class is like a blueprint for something. It lays out all of the shared features of each instance of it (we will cover such instances next). In programming this means that attributes and methods defined in the class will be inherent to all instances of the class. An example of this pertaining to the animal kingdom would be to list the qualities of a mammal. These would include warm blooded, has fur, and typically gives birth to live young. These would all be inherent to the mammal class and while individual mammals may have more or less of each of these…and perhaps sometimes additional features particular to that individual mammal, they all share these features in common. An attempt to reference this to D&D would have me drawing a comparison to a blank character sheet for creating a character:
Every character in D&D will have an AC rating, Hit Points, Initiative, Speed, Strength, Dexterity Etc. Though instances or “objects” of each character will have these numbers/attributes vary, they will all have these things
  • Objects: An object is an instantiated instance of a class. In other words it is a particular and individual example created from a class blueprint. Back to our two analogies we were using…If we made a character named Alex who was human they would get +1 to all d&d core stats, and inherit the features of warm blood and hair from the mammal class.
  • Attributes: An attribute is a specific member of data inside a class or object. The reason I say both class or object is that if an attribute is the same across all instances of the class it can be set within the class blueprint itself and thus be a class attribute. If it varies among instances of the class the value for the attribute should be set upon instantiation of the object. These attributes can be any of any data type or data structure. Referring back to our human Alex, an example of attributes could be those aforementioned d&d core stats which are all integers.
  • Methods: a method is a behavior or procedure that a class or object can perform. An example of a class method would be that all mammals (including Alex) can perform the task eating. An example of a method defined in an object (or instance, thus being called an instance method) would be that because Alex has a dexterity of 14, they get a +2 modifier to dexterity saving throws.

The 4 Key Principles Of OOP

All examples of Object-Oriented Programming share the fundamentals of polymorphism, encapsulation, inheritance, and abstraction. Lets break this down:

  • Polymorphism: polymorphism is any method that can be called by the same name yet achieves different results based on the objects attributes that call it. An example in regards to programming would be:
Language: Javascript

in this example every instance of the character class will have a firstName and lastName attribute. They will each also have polymorphic greeting and fullName methods that return different things based on the instances firstName and lastName attributes. In this particular example the greeting of alex would return:

>alex.greeting
'Hello, my name is Alex Smith.'

While the greeting of chris would return:

>chris.greeting
'Hello, my name is Chris Johnson.'
  • Encapsulation: These are methods that are private and can only be called within the instance or class itself. As of right now I a good example of encapsulation in regards to D&D eludes me. But a more real world example would be if you had an instance of a Person class perform a method called open_bank_account. Inside of the open_bank_account method you would need to retrieve the values of the person instances birthday, social security number, and various other private attributes. These are things you don’t want just anyone interacting with the person to get their hands on or view, however you still need to retrieve their values to perform the public facing method of opening a bank account. Thus these attributes are only available within the object in which they are defined.
  • Inheritance: is a practice of one class inheriting methods and data from another in a hierarchical fashion. For instance if you had a parent class of a vehicle a child class of a vehicle might be the truck class. A truck has all the things defined by a vehicle which are 4 wheels, an engine, a windshield etc. but it also has a bed and possibly a trailer hitch. An example involving D&D could be in the case of creature types. For instance all monsters in D&D have the standard core stats, but monsters of the ‘undead’ creature type inherit these stats and also typically have immunity to poison and at least resistance to necrotic damage.
A vampire
  • Abstraction: Can be summarized as only showing the required information to complete a process or fulfill a request. This hides data and processes that are unnecessary when simply requesting a desired output. An example of Abstraction could be (going back to the vehicles example again) when I drive my car to the grocery store, I do not need to know all of the inner workings of the engine. I instead just want the car to take me to the grocery store.

Conclusion

There are several benefits to the practice of Object Oriented Programming, and using this programming paradigm allows us to create non redundant and elegant code. I personally believe object orientation is an attempt to bring the things we define in code closer in relation to the way we define things in life.

--

--