Introduction
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.
Types of polymorphism
- Compile-time Polymorphism
- Runtime Polymorphism
In this article, we will read about Compile time polymorphism 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 behaviour.
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
Output:
30
60
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
Output:
Codingninjas
5
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
Output:
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 behaviour of some operators like +, -, *, etc, to work with user-defined objects along with their predefined behaviour.
It is a really powerful feature but we need to be careful while using it to avoid confusion or unexpected behaviour of operators. The behaviour which we define should be consistent with the predefined behavior of the operator.
Note: Operator overloading is not supported in Java. However, in the following example, we aim to achieve similar functionality using methods.
Here is an example of operator overloading in Java.
Output:
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