What is the difference between Abstraction and Polymorphism

后端 未结 11 678
别那么骄傲
别那么骄傲 2021-01-30 07:17

I seem to not understand two OOP concepts very well. Could you explain what abstraction and polymorphism are, preferably with real examples and

11条回答
  •  旧时难觅i
    2021-01-30 07:52

    Abstraction and Polymorphism are similar in nature with a different purpose.

    For ex.

    A driving license: you are given a license mentioning the class of vehicles you are allowed to drive. The license mentions the class of vehicle allowed by the authority but it doesn't define or mention which specific car or brand you should drive. This is Abstraction.

    here License is an Abstract class and its method, the allowed vehicles is its Abstract method.

    Now, here, Polymorphism is different ways individual License is alloted by the authority to different people, some are issued for light vehicles while some for heavy and some are given for commercial vehicles, according to different requirement. Here, License is a base class, and other kinds of licenses are its child classes, also obeying the is-a relationship. Commercial License is a License.

    So, Abstraction is a general guideline giving independence of implementation to the follower classes while Polymorphism is differential approach which override the methods/rules set by parent class.

提交回复
热议问题