The 4 major principles of Object Oriented Programming.
Do to my interest in Apple’s new programming language Swift, I’ve taken the liberty of explaining the 4 major principles of Object Oriented Programming.
Encapsulation, Abstraction, Inheritance and Polymorphism.
The best way to explain it in my opinion is to use Pokemon.
- Encapsulate: You can tell Charmander to use Flamethrower. You don’t know what flamethrower does inside Charmander’s body, and you can’t change what Flamethrower does, but you know that you can teach him Flamethrower and if you call Charmander->Flamethrower() it will return fire. To further expand on this topic, its not just that the user doesn’t know what flamethrower does, its that no other component of the game knows. Your bike doesn’t care, watergun doesn’t care. Each piece contains and hides its own info from everything else.
- Abstraction: is the idea of a Move, Pokemon, Item being a concept that cannot be instantiated, but can be implemented. All Pokemon should inherit from a Pokemon class but you cant create an object just from “Pokemon”. You need a specific implementation of Pokemon such as Pikachu or Mewtwo.
- Inheritance: Charmander is Fire type. That means throwing water at him does double damage. Charmeleon inherits code from Charmander, so he’s also Fire type. However nowhere in Charmeleon’s code does it say he’s Fire type, but throwing water at him still does double damage.
- Polymorphism: Bulbasaur is a pokémon. Your pokéball works with any pokémon. That means your pokéball can store a Bulbasaur because the code reads Pokéball.Store(pokémon) and Bulbasaur is a pokémon. So if you call Pokéball->Store(Bulbasaur) it will work just as well as if you called Pokéball->Store(Squirtle).