James Bonds and Polymorphism in OOP
Polymorphism = Poly (many) + Morph (Forms)
James Bond also had many forms, the character was played by many actors in different James Bond movies. They all had the same name, James Bond, Agent 007.
The word Polymorphism signifies that one element can have many forms.
In context of programing, it states that there could be multiple Functions/Methods with same name, return type and parameters/arguments.
Just like in a classroom there could be multiple students with same popular name (like Ankita, Rahul), SIMILARLY, there could be multiple Functions/Methods in a program with same name [called Method Overloading].
Method Overloading = Same Name Only
Now, a lot of times the last names of those with the same first names will also be the same (like Gaurav Singh),
there could be multiple Functions/Methods in program with NOT JUST same name BUT ALSO the same Arguments/Parameters [called Method Overriding].
Method Overriding cannot be applied to a Class. Which means, there cannot be two or more functions/methods in a single Class with – same return type, name and parameters/arguments.
Method Overriding = Same Name, Same Type and Number of Parameters/Arguments
Example Code: Method Overloading
* Created on NETBEANS IDE
#include <cstdlib> //IDEs include header files like this
using namespace std;
//Class definition starts
public: int area(int a) // Method 1
int squareArea = a*a;
public: int area(int b, int c) // Method 2
int rectangleArea = b*c;
//Class definition ends
int main ()
FindArea obj; // Object created for Class
int area1 = obj.area(10);
// Calls Method 1 because of 1 integer in argument, matching 1st function
int area2 = obj.area(10,5);
// Calls Method 2 because of 2 integers in argument, matching 2nd function
cout<<"Two areas are "<< area1<<" and "<<area2;
Two areas are 100 and 50
Method Overriding is a bit complex hence we will cover that separately.
If you wish to copy this program and run on your NETBEANS IDE, please retype all the double quotes (“”) for smooth execution. If you are using any other program for execution, please make small changes that may be required in that environment.
Thanks @Code Basics