Polymorphism in Oops

In this tutorial, you will learn about polymorphism in OOP. And you’ll also learn what is the role of polymorphism in OOP, and why overriding and overloading are not supported in PHP & to overcome this interfaces and abstract classes play an important role to achieve polymorphism.



 

Polymorphism describes a concept in Object Oriented Programming in which a class has different functionality while sharing a common interfaces

Polymorphism in Oops – PHP

What is polymorphism?

Polymorphism came from the Greek words Poly (which means many) and morphism (which means forms). like a single function having many signatures.



How to Implement Polymorphism in PHP?

Polymorphism in PHP can be implemented by either the use of interfaces or abstract classes. But incase other languages like C++, Java, and, .Net, etc the function overloading and function overriding are useful for implementing polymorphism. In PHP, function overloading and function overriding do not support it.

See some examples given below to get a clear idea about polymorphism in PHP



Here OnlineCalculator is an interface, which does not contain any kind of code, only a function. So using the interface we can use the same method to find the area of a circle and rectangle. Using implements keyword we can implement an interface with a Class.

See the below example.

 

Questions asked in the Interview on Polymorphism in PHP



  1. What is Polymorphism
  2. What is an Interface
  3. How to implement an interface in a Class

 

Tags: ,