binding - How is this called overloading - Java -


this question has answer here:

i have class bindingsample method takes no parameter

public class bindingsample {   public void printmsg(){       system.out.println("binding sample no parameter");   } } 

and class extends bindingsample , uses same method signature adds parameter it

public class application extends bindingsample {    public void printmsg(int i){         system.out.println("changed value " + i);   }    public static void main(string[] args) {         application app = new application();         app.printmsg(5);   } } 

the output changed value 5

why did work if parameters different? , why called overloading? don't think it's overriding because override method, method signature , parameter should same.

why did work if parameters different?

you application class has printmsg method takes single int argument. therefore app.printmsg(5) works.

note making following change cause code not pass compilation:

bindingsample app = new application(); app.printmsg(5); 

since compiler can't find printmsg method in bindingsample class takes int argument.

and why called overloading? don't think it's overriding because override method, method signature , parameter should same

overriding , overloading 2 different concepts.

method overloading occurs when multiple methods share same name have different number of arguments or different types of arguments.

your application class has 2 printmsg methods (one inherited super class) different number of arguments. hence method overloading.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -