Denis' Micro Blog

Welcome! 👋

Design | Development | General
Guides | Life | Tips

The multi-transformation of everything a.k.a polymorphism

May 2, 2023 | Development

What's up, everybody.

Today I want to talk about a pretty "basic" but still important concept in the world of programming.

The war between the Autobots and the Decepticons... Wait... Wrong blog.

I meant the transformability of a single programming interface into different types. Polymorphism.

Let me give you an example that is a bit more connected to real life.

See this pencil?

A picture of a black pencil.

Well... What can you do with it? You can pick it up and write with it. Those are characteristics that apply to every type of pen.

Additionally, a pencil has the ability to be easily erasable, which isn't possible with every type of pen.

Let's put that into a diagram.

A relationship diagram of the generic pen and the more concrete pencil.

I think from here you can see the relation between these different objects.

If we add this fella, a gel pen, right here, look what happens.

A relationship diagram of the generic pen and the more concrete pencil and gel pen.

Polymorphism works exactly like that. Just with more abstract objects...

The cool thing now is that you're able to treat all of those different types of objects generically while also being able to access all of those object-specific details and their implementation.

In our example, this means you're able to tell someone... Hey... Write something down with this pen.

And the person immediately knows how to hold the pen correctly, move it across the paper to leave the "writing material" etc.

All of that while not losing any specific details or responsibilities, like the erasability of our pencil from earlier.

So yeah... Maybe you learned a thing or two and my metaphors weren't that bad.

See ya.