Object-oriented programming is, according to Wikipedia:
“Object-oriented programming 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).”
This all pretty much boils down to the following statement: I am an instance (or object. The most pedant amongst you
will say I am wrong, and you are technically right) of the class Human
. I have a few fields (or properties) like age,
gender, or hobbies. I can do stuff, like writing terrible articles, and that’s one of the many methods we humans have
access to.
Figure 1: OOP dog. A good boy.
Now, the sharpest readers I may have, if I get any, might have already realized that Human
is fairly broad for a class.
It isn’t wrong, but it isn’t precise either. That’s where we can talk about child classes (or subclasses, synonyms) and
inheritance. Human
is what we call a superclass (or parent class). It defines some common stuff to all humans and pretty much stops there.
For instance, not all of us may have access to the method WriteTerribleArticle()
, but I somehow do. There are multiple
ways for your objects to get this diversity. For example, you could set a parameter in your Human
class, which could
be defined by a boolean called enjoys_writing
. If set to true, your object can run the method WriteTerribleArticle()
,
and otherwise, it cannot.
Or you could just make a subclass of Human
and call it Writer
, and put all the people who enjoy writing in there, as
people who inherit from Human
and also have their special parameters, like writing languages and subjects.
This would be a terrible idea in our case since humans can develop these skills over time, but objects cannot suddenly turn into a new instance of their subclass without being erased and more or less cloned. But what if someone who isn’t a writer wants to write a terrible article?
You could theorize that learning to love writing is a transformative experience and some sort of rebirth, but that would also make for terribly optimized code, especially if we start needing one subclass per type of person: what if I enjoy both writing and programming? We’d end up with a class for that, and it would suck big time. Usually, we call this issue “class explosion”, and as you might have guessed by now, it isn’t a good thing.
Figure 2: A nice diagram from Dhruv Tatran's very serious (unlike what you are currently reading) LinkedIn Article.
“Subclass explosion happens when we try to create a new subclass for every possible variation of behavior or attributes, leading to an unmanageable and bloated hierarchy.”
The Python maniacs here might already be screaming at me: “Just switch to Python, we have dynamic classes!” And to that,
I say, “Your classes are objects whose type is type
. That’s a whole other can of worms I am not ready to deal with.”
While powerful, this flexibility can feel like letting loose chaos in a neatly structured system. Plus, we have a way more
elegant solution for our human class problem, that might be more informative for the confused masses (composed of, well, likely nobody).
That’s right, you may have guessed it (or not), we’re diving into composition! Classes can have other classes as fields.
For example, maybe our Human
class could have a list of classes as the field Skills
, and each of these classes would
hand your human skills to use. That’s great, but we could dive into more, and more, and more detail into this singular
issue until we emulate a true skills system into our humans.
Basically, subclassing says, “You’re stuck as a Writer forever.” Composition says, “I see now that the circumstances of one's birth is irrelevant, it is what you do with the gift of life that determines who you are.” (That quote was from Mewtwo actually, but you get the gist)
Figure 3: Are you a writer, or do you have strong writing skills? The difference matters!
This is where ontology kicks in! Ontology is, well, the philosophical branch that tries to define stuff. And the more detail we go into, the longer our definition of the human class gets (think in terms of computer memory. Between stocking a class called human with one boolean per hobby and a dude, or stocking a class called human, a list of pointers, and a few other classes for each hobby, before stocking said dude. Yeah, the latter sounds slower even if you’ve never heard of pointers).
What a whole bunch of philosophers might want to murder me for (it’s not only programmers, yay!), is the following statement: maybe maximizing ontological granularity or whatever is not always the end goal in real life. For the sake of science and philosophy, quite certainly, maximizing precision is key.
As soon as you start getting into the muddy waters of design and sometimes even engineering, however (and yes, a huge chunk of OOP is design in my opinion), this is where you might accept settling for heuristics, simply because they are lighter to compute, and help you avoid cognitive overload.
Basically: in philosophy and research, chasing maximum ontological granularity is noble because it gives us a clearer view of reality. In engineering or design, though, the pursuit of extreme precision often runs into constraints like time, memory, or sanity. This is where we settle for heuristics: they’re simpler, faster, and, let’s face it, just good enough most of the time.
Now that we got the basic concepts of OOP and Ontology out of the way, perhaps it’s time for us to discuss how Object Oriented
Programming is a form of ontology, and perhaps even, some sort of digital platonic ontology? If classes are Platonic Forms—perfect,
eternal templates for reality—then objects are their imperfect, concrete instances. Just as Plato’s chair Form exists beyond time
and space, so too does the abstract idea of a Human
class. Right?
Anyway, if you agree, or if I pissed you off throughout this likely inaccurate mess, or if you wanna sing Python’s praises (or Rust, I know Rust devs love doing that and y’all have your own weird version of OOP) or tell me you use Arch Linux for absolutely no reason, The chat's right here!