JAVA and OOPS for beginners-Part 1: Basics with examples and exception examples(learn in instant) by Feroz Navi

JAVA and OOPS for beginners-Part 1: Basics with examples and exception examples(learn in instant) by Feroz Navi

Author:Feroz, Navi [Feroz, Navi]
Language: eng
Format: azw3
Published: 2017-12-19T16:00:00+00:00


2. Data type of parameters.

For example:

add(int,int)

add(int,float)

3. Sequence of Data type of parameters.

For example:add(int,float)

add(float,int)

Invalid case of method overloading:

When I say argument list, I am not talking about return type of the method, for example if two methods have same name, same parameters and have different return type, then this is not a valid method overloading example. This will throw compilation error.

int add(int,int)

float add(int,int)

Method Overloading examples

As discussed in the beginning of this guide, method overloading is done by declaring same method with different parameters. The parameters must be different in either of these: number, sequence or types of parameters (or arguments). Lets see examples of each of these cases.

Example 1: Overloading – Different Number of parameters in argument list

This example shows how method overloading is done by having different number of parameters

classDisplayOverloading

{

publicvoid disp(char c)

{

System.out.println(c);

}

publicvoid disp(char c,int num)

{

System.out.println(c +""+num);

}

}

classSample

{

publicstaticvoid main(String args[])

{

DisplayOverloading obj =newDisplayOverloading();

obj.disp('a');

obj.disp('a',10);

}

}

Output:

a

a 10



Loading...
Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.