Polymorphism is a topic that programmers seem to be afraid of...even though it's a simple concept attached to a frightening word.
Polymorphism describes a pattern in object oriented programming in which classes have different functionality while sharing a common interface. A variable of a supertype can refer to a subtype object.
An example:
Circle and Rectangle class implement the shape interface. Shape contains the getArea() declaration. By implementing the Shape interface, Circle and Rectangle must have the getArea() method. Each method performs a different operation since retrieving the area is different when it comes to circles and rectangles.
Lets say you have a file with the function computeCost(). That method will take the getArea() and multiply it by 1.25. If you wanted to compute the cost for the circle, you would have to instantiate the Circle class, pass the radius, call the computeCost() function and pass the instantiated circle object. Within the computeCost() method, you would return $circle->getArea() * 1.25. If you wanted to do the same for Rectangle you would have to pass the rectangle object; The statement within the computeCost() method would still be $circle->getArea() * 1.25. With polymorphism you can specify that the parameter being passed to the computeCost() method will be a Shape $shape. It doesn't matter which one since both Circle and Rectangle inherit the method declarations from the Shape interface. You'll return $shape->getArea() * 1.25. Later you may want to get areas for other more complicated geometric properties. The computeCost() method would not change whatsoever and would be able to compute the cost for each new method.
Polymorphism describes a pattern in object oriented programming in which classes have different functionality while sharing a common interface. A variable of a supertype can refer to a subtype object.
An example:
Circle and Rectangle class implement the shape interface. Shape contains the getArea() declaration. By implementing the Shape interface, Circle and Rectangle must have the getArea() method. Each method performs a different operation since retrieving the area is different when it comes to circles and rectangles.
Lets say you have a file with the function computeCost(). That method will take the getArea() and multiply it by 1.25. If you wanted to compute the cost for the circle, you would have to instantiate the Circle class, pass the radius, call the computeCost() function and pass the instantiated circle object. Within the computeCost() method, you would return $circle->getArea() * 1.25. If you wanted to do the same for Rectangle you would have to pass the rectangle object; The statement within the computeCost() method would still be $circle->getArea() * 1.25. With polymorphism you can specify that the parameter being passed to the computeCost() method will be a Shape $shape. It doesn't matter which one since both Circle and Rectangle inherit the method declarations from the Shape interface. You'll return $shape->getArea() * 1.25. Later you may want to get areas for other more complicated geometric properties. The computeCost() method would not change whatsoever and would be able to compute the cost for each new method.
Comments
Post a Comment