The Principles of Object Orientated Programming as explained by Pokémon
Tags: oop, java, newbie, tutorial
Published: 2019-08-18 15:22:20

---
title: The Principles of Object Orientated Programming as explained by Pokémon
published: true
tags: oop, java, newbie, tutorial
canonical_url: https://iamtravisw.com/object-orientated-programming-as-explained-by-pokemon/
---

 ![The Principles of Object Orientated Programming as explained by Pokémon](http://iamtravisw.com/content/images/2019/08/pexels-photo-1310847.jpeg)

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._

<!--kg-card-begin: image-->

![The Principles of Object Orientated Programming as explained by Pokémon](http://iamtravisw.com/content/images/2019/08/chancey.gif)

<!--kg-card-end: image-->
### 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
<!--kg-card-begin: image-->

![The Principles of Object Orientated Programming as explained by Pokémon](http://iamtravisw.com/content/images/2019/08/pokeball.gif)

<!--kg-card-end: image-->

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
<!--kg-card-begin: image-->

![The Principles of Object Orientated Programming as explained by Pokémon](http://iamtravisw.com/content/images/2019/08/char.gif)

<!--kg-card-end: image-->

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
<!--kg-card-begin: image-->

![The Principles of Object Orientated Programming as explained by Pokémon](http://iamtravisw.com/content/images/2019/08/eevee.gif)

<!--kg-card-end: image-->

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:

<!--kg-card-begin: markdown-->
```
public class Eevee {

    public void sound () {
        System.out.println("Eevee");
    }

    public void sound (int num) {
        for (int n : num) a
        {  
            System.out.println("Eevee");    
        }    
    }
}

```
<!--kg-card-end: markdown-->

**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:

<!--kg-card-begin: markdown-->
```
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();
    }
}

```
<!--kg-card-end: markdown-->

In this example "Vaporeon" would be the output.

### Abstraction
<!--kg-card-begin: image-->

![The Principles of Object Orientated Programming as explained by Pokémon](http://iamtravisw.com/content/images/2019/08/trade.gif)

<!--kg-card-end: image-->

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
<!--kg-card-begin: image-->

![The Principles of Object Orientated Programming as explained by Pokémon](http://iamtravisw.com/content/images/2019/08/pikachu.gif)

<!--kg-card-end: image-->

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.