Wednesday, January 9, 2019

What is the purpose of polymorphism?

Polymorphism. Generally, the ability to appear in many forms. In object-oriented programming, polymorphism refers to a programming language's ability to process objects differently depending on their data type or class. More specifically, it is the ability to redefine methods for derived classes.
 

IMPORTANT POINTS ABOUT POLYMORPHISM

  •  A functionality can behave differently for different instances
  • The behavior depends on the type of data used in the operation
  • Polymorphism is used for implementing inheritance.

ADVANTAGES OF POLYMORPHISM

  • It helps programmers reuse the code and classes once written, tested and implemented. They can be reused in many ways.
  • Single variable name can be used to store variables of multiple data types(Float, double, Long, Int etc).
  • Polymorphism helps in reducing the coupling between different functionalities.
 [ A car is polymorphic because you can send commonly understood messages to ANY car (start(), accelerate(), turnLeft(), turnRight(), etc) without knowing WHO built the car. A light switch is polymorphic because you can send the message turnOn() and turnOff() to any light switch without knowing who manufactured it. ]


No comments:

Post a Comment