Why Pokémon? Why not? Many of my peers grew up with Pokémon or now have kids that are watching Pokémon. Also, many beginners struggle to grasp the basics of Object Orientated Programming. My hope is that mixing the four core principles with something so many people have seen will help it click. By making the topic less abstract (which is ironic) and more relatable, it could help someone grasp the basics. What more could we ask for? So let's get started!
Note: If you don't follow Pokémon, this article probably won't make much sense.
What is Object Orientated Programming?
First; Object Orientated Programming (OOP) is a programming language model where Objects communicate with one another in order to solve a problem. Objects are snippets of code that can be reused so that you don't have to always rewrite it. Objects are used in classes and classes are just blue prints for those Objects. By using those blueprints (or classes) you can create instances of those Objects, which are essentially just versions of the original Object, built from the blueprint.
Okay that was a lot in a small paragraph, but it's important that you understand that before proceeding. It's also the only part of this entire article that won't use Pokémon to help you learn, so make sure you grasp these concepts before reading rest. Objects are used in classes. Classes are blueprints. Instances of objects are built from those classes.
Encapsulation
Encapsulation is the process of combining functions and data into a class. Data is not accessed directly but rather through functions within the class. The point of encapsulation is to restrict the access to your Object's components so that users aren't modifying your objects directly.
Think of encapsulation as a Pokéball. Everyone knows how to use it, regardless of how it works on the inside. The Pokemon trainer doesn't have to know why it works, they just need to know how to throw it and play by the rules. In programming, the user doesn't have to know anything other than the interface you provide, and you only want to provide them with access to relevant information. This is called information hiding and it is important.
In order to use Encapsulation properly, you will need to use access modifiers. Different languages use different variations of access modifiers, but in Java they are:
Public: Code from anywhere in the application can access it.
Private: Only code within the same class can access it.
Default: When you don't define an access modifier for your class, you will be using the default access modifier. Code within the same class and package can access the code.
Protected: The protected class has the same access as default with the addition of sub-classes having the ability to access the code.
Inheritance
Inheritance in programming is when one class acquires properties from another class. In Java a class can only be derived from one other class. We call the original class a parent class (or superclass). The derived class is called the child class (or subclass). This is where Pokémon as a comparison really starts to shine. Remember evolution? Remember when Charamader turned into Charmeleon. That's actually a great example of inheritance.
Let's say you have a Charmader (Superclass) and it knows two basic moves. Scratch and Burn. Suddenly your Charmander is strong enough to evolve and it turns into Charmeleon (Subclass). Charmeleon learns a new move called Flamethrower, but it also still has access to Scratch and Burn because it inherited those abilities from Charmander (which was the Superclass). Whoa, that kind of makes sense right?
That is the basic concept of Inheritance in object orientated programming. If you were writing a program you wouldn't want to write the same code over and over again, that's not efficient and it's a nightmare to manage. Instead you would extend your parent class with a new child class, and all of the original properties from the parent class will be extended to the child class. Same results, more efficient, but a lot less work.
Polymorphism
Polymorphism is the ability to take on many forms. In programming polymorphism means to process objects differently based on their data type. In other words, if you have one method, it may have multiple implementations for a certain class action. At the point the application runs (runtime), it will decide what to do.
In Pokémon two characters show this ability prominently, Ditto and Eevee. Let's use Eevee as the example. If you have a parent class called Eevee with multiple child classes (Vaporeon, Leafeon, Jolteon, etc...), and then put Eevee in contact with an evolution stone... The corresponding child class would be called. So if Eevee came into touch with a water stone, the Vaporeon child class would be triggered at runtime and the Vaporeon code would be used opposed to Leafeon, Jolteon, etc...
So how do you use Polymorphism? Well in Java, polymorphism can be either static or dynamic. Method Overloading is static and Method Overriding is dynamic. Overloading is considered static because the method that is invoked is decided at the time of compilation. You use overloading when methods that share the same name behave differently based on the arguments passed to the method. Here's an example in code:
public class Eevee {
public void sound () {
System.out.println("Eevee");
}
public void sound (int num) {
for (int n : num) a
{
System.out.println("Eevee");
}
}
}
Overriding is considered dynamic because it is resolved at runtime. Overriding means that a derived class is implementing a method of its parent class. If that sounds confusing, think about the Eeveelution example we talked about. Here it is in code form:
class Eevee{
public void sound(){
System.out.println("Eevee");
}
}
class Vaporeon extends Eevee{
public void sound(){
System.out.println("Vaporeon");
}
}
public class OverridingTest{
public static void main(String [] args){
Eevee eevee = new Vaporeon();
eevee.Vaporeon();
}
}
In this example "Vaporeon" would be the output.
Abstraction
Abstraction is the process of showing only relevant data to the users and hiding unnecessary information. A great example in Pokémon is trading with a friend. During this process, surely there is a lot going on behind the scenes. A lot of data is being transferred, files are being updated and objects are definitely being instantiated. However to the users, all you see is your Pokéball being sucked through a machine and disappearing before a new Pokéball lands before you. This is abstraction. That's it. It doesn't need to become anymore complicated than that.
Wrap it up
It goes without saying that this was a simplified explanation of Object Orientated Programming but I hope that this article can help someone who may have been struggling to understand the concepts a little better. As with most things, programming is a skill and it takes a lot of practice to master. If these concepts aren't making complete sense, you should study them deeper because they are the core fundamentals of OOP. Having a strong foundation is the best way to build your skills. I'll leave you all with the obligatory Pikachu GIF. Thanks for reading.
Top comments (0)