Polymorphism in Java. In English

                                                  Polymorphism

In Java, polymorphism is the idea that allows us to execute a single action in multiple ways. The words poly and morphs are both Greek words. The definition of "morphs" is shapes, while "poly" means many. Hence, polymorphism refers to variety. One of the key components of object-oriented programming is polymorphism.

After a subclass overrides a superclass method, polymorphism is the process of uploading and implementing subclass methods through the superclass reference.



There are two types of polymorphism in Java.

1.Compile-time polymorphism

2.Runtime polymorphism




Compile-time polymorphism......................

Another name for it is static polymorphism. Operator overloading or function overloading are the methods used to achieve this kind of polymorphism. 

There are three subtypes of compile-time polymorphism.

1. Function Overloading - Multiple functions with different argument lists can share the same name thanks to a feature in C++. Based on the quantity and kind of arguments supplied to the function, the compiler will select which function to call.

2.Operator Overloading - When applied to user-defined data types, operators like +, -, *, and so forth can take on new meanings thanks to a feature in C++.Java doesn’t support the Operator Overloading.

3.Template - The ability to create generic classes and functions is a strong feature of C++. A template is a set of instructions used to create a class or family of functions.

Method Overloading

Ex: -        class Sum {
                static int Multiply (int a, int b)
                {
                 return a * b;
                 }
                static int Multiply (int a, int b, int c)
                {
                return a * b * c;
                }
               }
               class Test {
               public static void main(String [] args)
               {
                 System.out.println(Sum.Multiply(1, 2));
                 System.out.println(Sum.Multiply(1, 2, 3));
                }
              }
output-2
          6

Runtime polymorphism....................

Another name for it is Dynamic Method Dispatch. It is a procedure wherein a runtime function call to the overridden method is handled. Method Overriding is used to achieve this kind of polymorphism. On the other side, method overriding happens when a derived class defines a member function of the base class. It is asserted that the base function has been overridden.

Virtual functions - It permits child class objects to function similarly to parent class objects. To provide its own implementation, the child class has the ability to override the parent class's virtual function. Depending on the object's true type, the function call is handled at runtime.

Ex: -
                        class Animal {
                         void Print() {
                            System.out.println("Animal class");
                          }
                         }
                         class Birds extends Animal {
                          void Print() {
                              System.out.println("Birds"); 
                           }
                          }
                         class Snakes extends Animal {
                          void Print() {
                             System.out.println("Snakes");
                          }
                         }
                        class Test {
                           public static void main(String [] args)
                         {
                        Animal a;
                        a = new birds();
                        a.Print();
                        a = new Snakes();
                        a.Print();
                        }
                       }
Output - Birds
             Snakes

Advantages of Polymorphism in Java.........................
  • Permits objects of various classes to be handled as objects of a common class, increasing the reusability of code.
  • Reduces the quantity of code that needs to be written and maintained, which improves readability and maintainability of code.
  • Allows for dynamic binding, which allows the appropriate method to be invoked depending on the object's actual class at runtime.
  • Makes it easier to develop generic code that can handle objects of diverse types by enabling objects to be treated as a single type.
Disadvantages of Polymorphism in Java......................

  • Can make it more challenging to comprehend how an object behaves, particularly if its code is complicated.
  • Performance problems could result from this because polymorphic behavior could call for extra runtime calculations.

Comments

Popular posts from this blog

Lambda expression in Java. In English

Loops in Java. In English

Conditions in java. In English