re: Explain Encapsulation and Polymorphism Like I'm Five VIEW POST


You an Animal class with children classes Herbivore and Carnivore for example. Then each child implements its own overriden version of function feed ().
For herbivores:
-find herb
-eat herb

For carnivores:
-find prey
-hunt prey

Even when the implementation is different for each kind of animal, you know that for any animal you can call the function feed for it and it will have the right behavior, this is Polymorphism.

Now imagine you have two pokemons in the pension and one day you come and the okd lady says "Your pokemons came back wih an egg, we don't know where they found it but here it is". So you got a behavior: receive an egg. But there is some internal hidden behavior : You don't know where it comes from. You know what happens but not how. This is encapsulation; you have access to some interface function but not access to the parameters it plays with

Code of Conduct Report abuse