Object-Oriented Programming in Layman's Terms
I made a post on LinkedIn on how to get started in your journey as a Software Developer, I pointed out 6 steps in order to start your career. Today, I will be talking about Object-Oriented Programming
What is Object-Oriented Programming?
Now the official definition of OOP is that it’s a computer programming model that organizes software design around data, or objects, rather than functions and logic. Now, to non-technical folk’s, this might sound like absolute word salad, so allow me to make things simple, it is a way to do stuff on a computer. That is it. Just like there’s different economic models, such as Socialism or Capitalism, there’s different way’s to do stuff on a computer, anyways, I digress, I don’t want this article to be political.
How do you get started with OOP? First, download VS Code, its and IDE to actually write code. Then choose your language you will work with, for this article, I’m going to use TypeScript since that’s what I specialise in.
Here, in this example, I have defined a class, called Movie. For the attributes, I have chosen, director, dateReleased and name.
What is a class?
You can think of the term class, as a blueprint to something you want to create. Just like every invention has a blueprint, you can think of classes as digital blueprints to what we want to build, and what do we actually want to build? Well, we want to build Objects, hence the term Object-Oriented Programming.
Now, how we do we create objects using this class? You might be wondering. It’s easy, just invoke the class using the new keyword. As you can see from the picture below, every time you use the new keyword, you will create a new object from whatever class you are using. In this example, every time you create a new Movie, you must pass in the arguments that are defined, and they MUST be in string format.
This is just the basic parts of OOP, up until now, we have just created 2 new objects, which don’t actually do anything, they just return a new object with the relevant information. If we actually want to build some logic, we need to define methods into the class.
Now, I have defined a method inside the Movie class, now this is a very barebone example, so all it’s doing is returning the director of that said object. In reality, the logic inside these methods are complex, at times we utilise data from multiple other classes, manipulating them in specific ways to show the end result to the user.
Now, as I have defined the 2 objects, and also the method showDirector(), we can access that method from those two objects I have defined, and once we invoke that method from those objects, it will return the associated directors from when we created each individual object.
Now, this is the basics of OOP, very bare-bone, it may seem simple now, but there’s a lot more to it, principles such as Composition, Inheritance, Abstraction and Polymorphism are at the core of OOP, which I will get to in another article.
At the beginning of this article, I compared OOP to different economic models, and I said it’s a way of doing things on a computer, you might be wondering, well, what are the alternatives to OOP? That’s a good question, 2 of the well known alternatives to OOP are functional programming and procedural programming. Within the realm of Typescript, OOP is widely used as a programming model to build complex software, but in other languages, such as Golang, you may use functional programming as an alternative.
The next article, will be more on OOP, breaking into it’s principles like I mentioned before, and why it’s useful.
You can follow me or connect with me on LinkedIn, and see how I progress professionally in this career!