Can someone explain the benefits of polymorphism?

前端 未结 10 455
难免孤独
难免孤独 2021-02-01 10:41

So I understand pretty much how it works, but I just can\'t grasp what makes it useful. You still have to define all the separate functions, you still have to create an instanc

10条回答
  •  遥遥无期
    2021-02-01 11:45

    Polymorphism is One of the principles OOP. With polymorphism you can choose several behavior in runtime. In your sample, you have a implementation of Parent, if you have more implementation, you can choose one by parameters in runtime. polymorphism help for decoupling layers of application. in your sample of third part use this structers then it see Parent interface only and don't know implementation in runtime so third party independ of implementations of Parent interface. You can see Dependency Injection pattern also for better desing.

提交回复
热议问题