Mastering Object-Oriented programming with Pokemon.

Paper #005

Mastering Object-Oriented programming with Pokemon.

2024-05-12 | 3 min read

codejava

When I first started learning Object-Oriented Programming (OOP), it was challenging to visualize this concept without confusing myself. I could only understand the idea when I applied it to another topic. That’s my secret technique anytime I try to learn subjects. I use my pre-existing knowledge as a foundation for understanding new subjects. That pre-existing idea was Pokemon. Hopefully, Nintendo does not sue me for using their names to understand OOP, so please don’t let them know about this.

When I was first introduced to OOP, my professor explained that it’s a programming paradigm that revolves around the concepts of “classes” and “objects,” which can contain data and methods. Hearing that for the first time did not help me get any closer to understanding it until I applied it to Pokemon.

This is just a reminder to anyone who doesn’t know what Pokemon are. They are fictional creatures from a popular video game series. In these games, players, known as Pokémon Trainers, capture and train Pokémon to battle each other. Each Pokémon has unique abilities, and players strategize to defeat opponents using their Pokémon’s moves.

Consider a “class” as the recipe for a Pokémon. When I say recipe, I mean the ingredients and steps needed to make a Pokemon. What is the minimum amount of information required to be considered a Pokemon? In this case, every Pokemon has a name, a type, and an index. Those are the ingredients. The ingredients are the instance variables for the class.

Now, what are the steps? When I say steps, I ask what behaviors or actions every Pokemon does. A simple example is that every Pokemon speaks with its name and has a set of moves. These behaviors are the methods within your class.

Now, write down those ingredients and steps in a Java file. Congratulations, you just created a Java class that can be used to create objects. Below is an example of our Pokemon class written in Java.

Screenshot of a Java class for a Pokemon containing methods and instance variables Pokemon Class Example Now that we have a class ready, we can create an object, more precisely, a Pokemon object. An object is just an instance of a class. We can make more than one Pokemon. We can create as many as we want. They can all have different values or the same values.

Screenshot of a objects being creating from the Pokemon class. Pokemon Object Example However, how is it possible for an object to be created with the same information? It’s similar to catching Pokemon. Once you catch one Pikachu, nothing is stopping you from grabbing another one and another one. They might all be Pikachus with the same name and index, but they are different Pokemon with their personalities and moves. Like in OOP, creating an object with similar data won’t be a problem because they are distinct objects with their own memory space. They can be manipulated independently without affecting each other.

Ultimately, this is how I applied Pokemon to my understanding of OOP. Make sure to expand this understanding to not just OOP but to other aspects of coding as well. Using pre-existing knowledge as a baseline to learn new subjects creates stronger connections in your brain and makes it easier to remember it for a longer time. Plus, it makes the learning process that much more enjoyable.