I’m a huge OOP fan, and I love how OOP could potentially represent any kind of real life object. For example, let’s say we want to depict an animal. It’d be a cat, a dog, an alligator, etc..
The first attribute that comes to my mind is wildness. Cats and dogs are usually domestic, while alligators aren’t. So, our
Animal object should have a boolean property called
Thinking more deeply, an animal can have a lot of properties, like
weight… and so on.
But a real life animal can also do things like eating, running, crying… so let’s add some cool methods to our brand new class:
The methods declared above are the simplest we could implement, but what if we want to be as close as possible to real life?
An animal could be carnivorous, erbivorous, or even omnivorous. We could implement these as animals’ attributes, but I prefer a slightly different solution: I’d set a property called
eats, that is an array defining all foods that our animal can actually eat. Foods are just strings.
Straightforward, isn’t it? We can now create as many
Animal instances as we want!
But hey, these are just generic
Animal instances… what if we want to create a
Cat instance, or a
Simple! Let’s extend the
And now, let’s have fun with the console!