Javatpoint Logo
Javatpoint Logo

Static Polymorphism in Java

A key idea in object-oriented programming is polymorphism, which enables objects of various kinds to be considered instances of a single superclass or interface. Java has two methods for achieving polymorphism: static polymorphism (sometimes called compile-time polymorphism) and dynamic polymorphism (often called runtime polymorphism). The subject of this paper will be static polymorphism, as well as its application and Java implementation examples.

Understanding Static Polymorphism

Static polymorphism happens when the compiler chooses the right method or function to call at compile-time based on the quantity, nature, and order of inputs. Method overloading, which enables numerous methods with the same name but different arguments in the same class, is how it is accomplished.

Consider the following example to understand method overloading and static polymorphism in Java:

Filename: Calculator.java

Output:

15
6.2
6

Explanation

Explanation: The Calculator class has defined three add methods with the same name but distinct parameters. The first add method takes two integers and returns the total of the two numbers. The second add function takes two doubles and returns the total of the two doubles. The third add method takes three integers and returns the total of those three numbers. We build a Calculator class object and call the add function with various parameters in the main method. The compiler chooses the proper add method at build time because Java chooses the right method to call depending on the types of inputs. An illustration of static polymorphism is this.

Method Name and Parameter List

The method name and parameter list make up the method signature. The method signature for each overloaded method in method overloading has to be distinct. This requires either different parameter types or a different amount of arguments in the procedures. The method's return type has no bearing on the method signature and cannot be utilized to distinguish between overloaded methods.

Selecting the Correct Method

The Java compiler uses a mechanism known as static binding or early binding during compilation to identify which method should be called. It compares the parameters' amount, kind, and order to the method invocation and signature. The appropriate procedure is invoked if an exact match is discovered. The compiler uses implicit type conversions to identify the best match if a precise match cannot be made. The most specific approach is used if more than one fits the criteria for the argument.

Considerations for Method Overloading:

Different parameter types or a different number of arguments should be used in overloaded methods.

The process of method overloading is unaffected by the method's return type.

A compilation error will be produced if methods are overloaded only based on the return type since the compiler cannot distinguish between them.

Drawbacks

Static polymorphism has certain drawbacks compared to dynamic polymorphism, which is attained by method overriding. These restrictions include, among others:

Subclasses' inability to override methods with static polymorphism, the method implementation is chosen based on the reference type rather than the actual object type at build time. Because runtime polymorphism depends on dynamic method dispatch, it does not support it.

Low extensibility A class must be modified to add new methods with the same name but different arguments. This entails changing the code for the existing class, which may necessitate recompiling any dependent classes.

Conclusion

Despite its drawbacks, static polymorphism is a potent approach that aids in method overloading and enables programmers to resolve method calls at compile time. Java developers may create more flexible and maintainable code by correctly grasping the idea and using method overloading. Method overloading in Java, which achieves static polymorphism, enables the compiler to choose the right method at build time based on the types of inputs. It promotes efficiency, readability of the code, and adaptability. Developers may write simpler, easier-to-maintain code by correctly utilizing static polymorphism and comprehending it.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA