Function overloading happens when a class has multiple methods with the same name. If we define a getData
method twice in our class, we can say that the getData
function is overloaded, as shown in the following code:
package coreJava; //function overloading public class childlevel extends childClassDemo { public void getData(int a) { } public void getData(String a) { } public static void main(String[] args) { childlevel cl=new childlevel(); cl.getData(2); cl.getData("hello") } }
There are a few rules that we need to remember while using multiple instances of a function with the same name. The first rule is that the number of arguments present in the function-overloaded method should be different, and the second is that the argument data type should be different. If we keep both the getData
methods with the int a
argument, it will throw an error, so we need to have a different number of arguments for each method...