|
| 1 | + |
| 2 | +# Polymorphism in Java |
| 3 | + |
| 4 | +- The word polymorphism means having many forms. In simple words, we can define polymorphism as the ability of a message to be displayed in more than one form. |
| 5 | + |
| 6 | +- Real life example of polymorphism: A person at the same time can have different characteristic. Like a man at the same time is a father, a husband, an employee. So the same person posses different behaviour in different situations. This is called polymorphism. |
| 7 | + |
| 8 | +- Polymorphism is considered as 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. |
| 9 | + |
| 10 | + |
| 11 | +### In Java polymorphism is mainly divided into two types: |
| 12 | + |
| 13 | + - Compile time Polymorphism |
| 14 | + - Runtime Polymorphism |
| 15 | + |
| 16 | +## Compile time polymorphism: It is also known as static polymorphism. This type of polymorphism is achieved by function overloading or operator overloading. |
| 17 | + |
| 18 | +### Method Overloading: When there are multiple functions with same name but different parameters then these functions are said to be overloaded. Functions can be overloaded by change in number of arguments or/and change in type of arguments. |
| 19 | + |
| 20 | +- Example: By using different types of arguments |
| 21 | + |
| 22 | + |
| 23 | +// Java program for Method overloading |
| 24 | + |
| 25 | +class MultiplyFun { |
| 26 | + |
| 27 | + // Method with 2 parameter |
| 28 | + static int Multiply(int a, int b) |
| 29 | + { |
| 30 | + return a * b; |
| 31 | + } |
| 32 | + |
| 33 | + // Method with the same name but 2 double parameter |
| 34 | + static double Multiply(double a, double b) |
| 35 | + { |
| 36 | + return a * b; |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +class Main { |
| 41 | + public static void main(String[] args) |
| 42 | + { |
| 43 | + |
| 44 | + System.out.println(MultiplyFun.Multiply(2, 4)); |
| 45 | + |
| 46 | + System.out.println(MultiplyFun.Multiply(5.5, 6.3)); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | + |
| 51 | +- Example: By using different numbers of arguments |
| 52 | + |
| 53 | +// Java program for Method overloading |
| 54 | + |
| 55 | +class MultiplyFun { |
| 56 | + |
| 57 | + // Method with 2 parameter |
| 58 | + static int Multiply(int a, int b) |
| 59 | + { |
| 60 | + return a * b; |
| 61 | + } |
| 62 | + |
| 63 | + // Method with the same name but 3 parameter |
| 64 | + static int Multiply(int a, int b, int c) |
| 65 | + { |
| 66 | + return a * b * c; |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +class Main { |
| 71 | + public static void main(String[] args) |
| 72 | + { |
| 73 | + System.out.println(MultiplyFun.Multiply(2, 4)); |
| 74 | + |
| 75 | + System.out.println(MultiplyFun.Multiply(2, 7, 3)); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | + |
| 80 | +### Operator Overloading: Java also provide option to overload operators. For example, we can make the operator (‘+’) for string class to concatenate two strings. We know that this is the addition operator whose task is to add two operands. So a single operator ‘+’ when placed between integer operands, adds them and when placed between string operands, concatenates them. |
| 81 | + |
| 82 | +- In java, Only “+” operator can be overloaded: |
| 83 | + |
| 84 | + To add integers |
| 85 | + To concatenate strings |
| 86 | + |
| 87 | +- Example: |
| 88 | + |
| 89 | + |
| 90 | +// Java program for Operator overloading |
| 91 | + |
| 92 | +class OperatorOVERDDN { |
| 93 | + |
| 94 | + void operator(String str1, String str2) |
| 95 | + { |
| 96 | + String s = str1 + str2; |
| 97 | + System.out.println("Concatinated String - " |
| 98 | + + s); |
| 99 | + } |
| 100 | + |
| 101 | + void operator(int a, int b) |
| 102 | + { |
| 103 | + int c = a + b; |
| 104 | + System.out.println("Sum = " + c); |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +class Main { |
| 109 | + public static void main(String[] args) |
| 110 | + { |
| 111 | + OperatorOVERDDN obj = new OperatorOVERDDN(); |
| 112 | + obj.operator(2, 3); |
| 113 | + obj.operator("joe", "now"); |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | + |
| 118 | +## Runtime polymorphism: It is also known as Dynamic Method Dispatch. It is a process in which a function call to the overridden method is resolved at Runtime. This type of polymorphism is achieved by Method Overriding. |
| 119 | + |
| 120 | +### Method overriding, on the other hand, occurs when a derived class has a definition for one of the member functions of the base class. That base function is said to be overridden. |
| 121 | + |
| 122 | +- Example: |
| 123 | + |
| 124 | + |
| 125 | +// Java program for Method overridding |
| 126 | + |
| 127 | +class Parent { |
| 128 | + |
| 129 | + void Print() |
| 130 | + { |
| 131 | + System.out.println("parent class"); |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +class subclass1 extends Parent { |
| 136 | + |
| 137 | + void Print() |
| 138 | + { |
| 139 | + System.out.println("subclass1"); |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +class subclass2 extends Parent { |
| 144 | + |
| 145 | + void Print() |
| 146 | + { |
| 147 | + System.out.println("subclass2"); |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +class TestPolymorphism3 { |
| 152 | + public static void main(String[] args) |
| 153 | + { |
| 154 | + |
| 155 | + Parent a; |
| 156 | + |
| 157 | + a = new subclass1(); |
| 158 | + a.Print(); |
| 159 | + |
| 160 | + a = new subclass2(); |
| 161 | + a.Print(); |
| 162 | + } |
| 163 | +} |
| 164 | + |
0 commit comments