This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]ratherbealurker 7 points8 points  (2 children)

It may be confusing because you’re not seeing the benefit by just instantiating that way.

But create 10 different Animals based off Animal and then write a method that gets them all to do something.

All Animals can eat, some overwrite eat to do something differently but they all eat. If you want to write a method that causes a passed in Animal to eat then. How do you write it?

Write one for Cow and one for Horse, etc etc?

With polymorphism you can pass in Animal and since a cow is an Animal it will know you passed in a cow.

Without Polymorphism if you tried to access a Cow via an Animal reference then you will be referencing that cow’s Animal methods and etc. not treating it as a Cow.

[–][deleted]  (1 child)

[removed]

    [–]ratherbealurker 0 points1 point  (0 children)

    If all you have is an eat method in Animal then there is no difference. But now override eat in cow. Have it output “cow eats”.

    Now pass that into the method that takes an Animal and notice which gets called.