In order to overload or override a method we need to follow certain rules, some of them are mandatory while others are optional and to become a good programmer we should always try to understand the reason behind these rules.
I am going to write two articles where I will try to look into the method overloading and overriding rules and try to figure out why we need to follow them.
In this article, we will see what rules we should follow to overload a method and we will also try to know why we should follow these rules.
Method Overloading
In general, method overloading means reusing same method name to define more than one method but all methods must have a different argument list.We can take the example of the
print
method present in PrintStream
class which gets called when we call System.out.print()
to print something. By calling this method on several data types it seems like there is just one print
method which is accepting all types and printing their values.But actually, there are 9 different
print
methods as shown in below imageWell, the
PrintStream
class creator could have created methods like printBoolean
or printInt
or printFloat
, but the idea behind naming all the 9 methods same is to let the user think that there is only one method which is printing whatever we pass to it.Which sounds like polymorphism but as discussed in the article How Does JVM Handle Method Overloading and Overriding Internally that how method overloading get resolved at compile time, some people also term method overloading as compile-time polymorphism.
Method Overloading Rules
Mandatory Rules
- Overloaded methods must have same method name: Having the same name let us reuse the same method name for different purposes and let the user believe that there is only one method which is accepting different kinds of input and doing the work according to the input.
- Overloaded methods must have different argument lists: Since all overloaded methods must have the same name, having a different argument list becomes necessary because it is the only way to differentiate the methods from each other. Java compiler differentiates a method from other based on its method name and argument list, So different argument list helps the compiler to differentiate and recognize methods from each other so the compiler will know which method is getting called at compile time only.
Optional Rules
So the different argument list is sufficient for the compiler to differentiate between the methods even if they have the same name so the rules mentioned below are optional and we are free to follow or not follow them. Going with below rules totally depends on your requirements and they are there to just provide us with additional functionality.
- Overloaded methods can have different return types: Return type matters when the method call is finished and JVM assigning back the value returned by that method call to some variable. But it is not required while calling the method and JVM cannot use it to differentiate between methods based on just return type. So we can either return the same as the overloaded method did or return something different or return nothing.
- Overloaded methods can have different access modifiers: If a method is getting called by the JVM it means it has passed the compilation phase because executing the bytecode which is already compiled. So access specifier of a method is useful for the compiler but it is useless for JVM and JVM cannot differentiate between methods based on access modifier. So an overloading method can have any access modifier and we can use it according to our need.
- Overloaded methods can throw different checked or unchecked exceptions: Again what exceptions a method might throw cannot differentiate a method from another method. And also overloaded methods are different from each other but usually, they perform the same operation on different data set and in order to do so overloaded methods may do some different operation as well which may throw a different exception.
0 comments :
Post a Comment