Polymorphism in oops
Polymorphism in OOPS
The literal meaning of Polymorphism would mean” a state of having many shapes.” When this concept is applied to the Object Oriented Programming System (OOPS) language such as Java or C++, it would be able to process in a simple manner. The language would be able to process various kinds of classes and objects through a uniform and single interface.
Basically, Polymorphism is one of the abilities of OOPS for redefining methods for Derived Classes. The behavior of the polymorphic function would depend on the types of data that are being used in the programming. Polymorphism is generally used for implementing inherence in the programming.
For the example of the polymorphism in the OOPS language, it would be the relationship between the parent class object and child class objects. Any object that is able to satisfy the relationship of IS-A for more than one time would be Polymorphic in nature.
There are two major types of polymorphisms in Object Oriented Programming (OOPS) languages. They are Static Binding (Compile time Polymorphism) and Dynamic Binding (Runtime Polymorphism). Method overriding would be the example of Dynamic Polymorphism and Method Overloading would be the example of Static Polymorphism.
Compile time Polymorphism
By using the method of overloading, one can reach the state of static polymorphism in Object Oriented Programming languages. This method would allow the programmer to implement various methods. They might use the same names but their parameters are different. Method overloading is an example of Static Polymorphism. There are certain conditions that are essential for static polymorphism. They are:
- All Parameters would have to be different in types.
- The order of the Parameter might have to different.
- The number of parameters of one method would have to be different from the other method.
During the run time, compiler of the language would identify various methods by identifying signatures of these methods. The compiler would first identify the method signature and then decide the method for a specific method call while compiling the program. The execution time for Compile time Polymorphism is much faster but this process is not very flexible.
This process is also known as Dynamic method dispatch. Under this process, a call to a single overridden method is solved during the runtime of the program. Method overriding is the prime example of Runtime Polymorphism. In this process, the call is not solved by the compiler. The overriding is achieved through pointers and virtual functions.
The Method Overriding is the process of declaring a single method in a sub-class that is present in a parent class. Through this process, the child class would gain a method for implementation. This method is given by the parent class of the child class. This process of polymorphism is slow but it is more flexible than the static polymorphism.
The main advantage of Runtime Polymorphism is the ability of the class to offer the specification of its own to another inherited method. This transfer of implementation of one method to another method is possible without changing or modifying the codes of the parent class object. Thus, if a single child class needs the implementation method of the parent class while other child class might use the overriding feature to have different implementation methods.
Advantages of using Polymorphism
Polymorphism is one of the core concepts of OOPS. This feature is applicable while handling various classes and sub-classes. Here are some of the advantages of using polymorphism in programming.
- One of the biggest advantages of using Polymorphism is that it allows the programming to extend itself.
- This process allows the user to reuse the old classes and codes that were once tested and already implemented successfully. These codes and classes would be applicable in various other methods.
- Through this process, the user would be able to store several variables of different types (double, Float, Int, Long, etc) in a single variable. This would make it easier for the user to search for and implementing these variables.
- Polymorphism would also help in reducing coupling between two different functions.
Polymorphism is the core concept of the Object Oriented Programming (OOPS) languages. This language is used for executing various types of huge programming. This concept makes it possible to access different classes of objects through the same interface. Through this concept, the programming would become more efficient and quicker.
Click Here-> Get Java Training with Real-time Projects