Polymorphism is considered one of the important features of Object-Oriented Programming. Polymorphism allows us to perform a single action in different ways. In other words, polymorphism allows you to define one interface and have multiple implementations. The word “poly” means many and “morphs” means forms, So it means many forms.
Method Overloading
// Java Program for Method overloading// By using Different Types of Arguments// Class 1// Helper classclassHelper {// Method with 2 integer parametersstaticintMultiply(int a,int b) {// Returns product of integer numbersreturn a * b; }// Method 2// With same name but with 2 double parametersstaticdoubleMultiply(double a,double b) {// Returns product of double numbersreturn a * b; }}// Class 2// Main classclassGFG {// Main driver methodpublicstaticvoidmain(String[] args) {// Calling method by passing// input as in argumentsSystem.out.println(Helper.Multiply(2,4));System.out.println(Helper.Multiply(5.5,6.3)); }}
2. Operator Overloading
It is a feature in C++ where the operators such as +, -, *, etc. can be given additional meanings when applied to user-defined data types.
3. Template
it is a powerful feature in C++ that allows us to write generic functions and classes. A template is a blueprint for creating a family of functions or classes.
// Java Program for Method Overriding// Class 1// Helper classclassParent {// Method of parent classvoidPrint() {// Print statementSystem.out.println("parent class"); }}// Class 2// Helper classclasssubclass1extendsParent {// MethodvoidPrint() { System.out.println("subclass1"); }}// Class 3// Helper classclasssubclass2extendsParent {// MethodvoidPrint() {// Print statementSystem.out.println("subclass2"); }}// Class 4// Main classclassGFG {// Main driver methodpublicstaticvoidmain(String[] args) {// Creating object of class 1Parent a;// Now we will be calling print methods// inside main() method a =newsubclass1();a.Print(); a =newsubclass2();a.Print(); }}