Polymorphism comprises two Greek words: poly and morphs, “poly” means many, and “morphs” means forms. Therefore, Polymorphism signifies many forms. Polymorphism, in simple terms, is the ability of a message to be displayed in multiple formats. Compile-time polymorphism, often referred to as method overloading, is a feature in Java that allows multiple methods in the same class to share the same name but differ in the number or type of their parameters.
In this article, we will discuss what is compile time polymorphism. We will also discuss some examples based on it. In the end of this article, we will discuss its advantages.
Also see, Polymorphism in C++
Types of polymorphism
- Compile-time Polymorphism
- Runtime Polymorphism
In this article, we will read about Compile time polymorphism in Java
Also see, Swap Function in Java
What is Compile-time Polymorphism in Java
Compile-Time Polymorphism is named after it occurs during the compilation process. The compiler checks the method signature at build time to identify which method to call for a given method call at build time. It's also known as early binding, static polymorphism, or overloading.
Compile-time polymorphism is rigid because it executes everything during compilation, limiting flexibility for programmers. On the other hand, run-time polymorphism is more flexible as it executes during runtime, allowing for dynamic behavior.
Compile-time polymorphism in Java is achieved by method overloading and operator overloading. Let us know about it in detail.
1. Method overloading
Method overloading in Java means defining multiple methods with the same name but different parameters, allowing flexibility and code reuse. The Java compiler determines which method to execute based on the arguments passed, enhancing code readability and usability.
What are the benefits of method overloading?
It improves the readability of the programme.
In Java, we have three ways to overload the method.
- By changing the number of parameters
- By changing the data types of parameters
- By changing the sequence of parameters
Method 1: By changing the number of parameters
In the Example class, there are two methods with the same name: “sum“. The first sum method takes two integers as input and returns their total. On the other hand, the second sum method takes three integers and returns the sum of those three values.
Both the methods are called in the main, and the compiler decides which method will be invoked at compile time.
Method 2: By changing the data types of parameters
In the above example, method overloading is achieved by changing the data type of the parameters. For example, one display() method accepts string parameters while another display() method takes the int parameter.
Method 3: By changing the sequence of parameters
Integer: 5, Double: 3.14 Double: 3.14, Integer: 5
In this example, we have two print functions that have the same name but different parameter sequences. The first print function takes an integer followed by a double, while the second print function takes a double followed by an integer.
Invalid cases of method overloading
- If two methods have the same name and parameter types, and it's not possible to distinguish between them based on their parameter order or return type, then the method signature is ambiguous and overloading is not allowed.
- Overloading is not allowed if two methods have the same name, parameter types, and return type but different access modifiers (e.g., public vs. private). In this case, the two methods are considered to have the same signature and cannot be overloaded.
- If two methods have the same name and parameter types, but one method has a variable argument list (varargs) and the other has an array parameter, then the method signature is ambiguous and overloading is not allowed.
- If a subclass inherits a method with the same signature as a method defined in the superclass, then overloading the inherited method is not allowed, as it would result in two methods with the same signature in the subclass.
2. Operator Overloading
Operator overloading is a feature in a few programming languages like C++. This means that we can customize the behavior of some operators like +, -, *, etc, to work with user-defined objects along with their predefined behavior.
It is a really powerful feature but we need to be careful while using it to avoid confusion or unexpected behaviour of operators. The behavior which we define should be consistent with the predefined behavior of the operator.
Here is an example of operator overloading in C++.
n1 + n2 = 15
In this example, we have a Number class that represents a simple integer value. We overload the + operator to allow two Number objects to be added together using the + operator. The operator+ function takes another Number object as a parameter and returns a new Number object that represents the sum of the two numbers. In the main function, we create two Number objects n1 and n2, and then use the + operator to add them together. The resulting Number object is printed.
Reasons Why is Operator Overloading not supported in Java
Java does not support operator overloading in the same way that C++ does. In Java, operators such as +, -, *, and / are predefined and have a fixed meaning for the types that they operate on. However, Java does allow for operator-like behavior to be defined for classes through method overloading.
For example, a Vector class might define add() and subtract() methods to perform vector addition and subtraction, respectively. These methods could be used in a way that resembles operator overloading, even though the actual operators cannot be redefined.
Also read - Difference Between Inheritance and Polymorphism
Advantages of Compile-time Polymorphism in Java
Here are some of the advantages of Compile time Polymorphism.
- Compile-time polymorphism allows the compiler to generate specialized code for each template instantiation, which can result in more efficient code than runtime polymorphism.
- It can be used to generate code for multiple data types, reducing the need to write duplicate code for each type.
- It allows for parameterized types, which can be checked for correctness at compile time. This leads to increased type safety and fewer runtime errors.
- Compile-time polymorphism can make it easier to debug code because errors are caught at compile time rather than at runtime.
- It can lead to improved performance compared to runtime polymorphism, which incurs the overhead of dispatching virtual function calls at runtime.
You can practice by yourself with the help of Online Java Compiler.
Frequently Asked Questions
Q. How compile time polymorphism increases the quality of the program?
Compile time polymorphism provides better quality of the program by making use of function overloading, operator overloading and by changing the function signature. Functions are overloaded using the same name but different return types and parameters.
Q. Is compile time polymorphism achieved by function overloading?
Yes, compile-time polymorphism can be achieved by function overloading, as a function is called during the compile time. It is technically an implementation of compile-time polymorphism in which we can assign the same name to different functions.
Q. Why compile time polymorphism is called early binding?
Compile time polymorphism is also called early binding, as the binding happens during compilation time. In binding, the body of a function is connected to the call of the function, which takes place during the compile time.
Q. Is static binding and compile time polymorphism same?
Yes, Static binding and compile time polymorphism are the same. This is because the binding of functions happens at compile time. Although, there can be static binding without polymorphism as well.
In this article, we have extensively discussed Polymorphism in Java. Polymorphism is one of the most critical aspects of Object-Oriented Programming. By coming to the end of this article, we understood the two forms of polymorphism: compile-time and run-time polymorphism in java. Both compile-time and run-time polymorphism differ in their approach to method binding and method invocation. We hope that this blog has helped you enhance your knowledge of Polymorphism in Java, and if you would like to learn more, check out our articles on Library. Do upvote our blog to help other ninjas grow. Happy Coding!”