Posts Tagged function overriding

Java Tutorial – 9 Method Overloading ( function Overloading )

Overloading is nothing but defining different behavior ( functionality ) for same function.

for example u can create add method which will add two ints, three ints, two doubles, three doubles.

it is not possible to define one method again and again with same name but u can do it by changing arguments.

example:

add(int x) { return x++; }

add(int x, int y) { return x+y; }

add(double x, int y) { return ((int)y + x); }

  1. if u call the method with 1 int it will call the first add method and return incremented value
    Example.
    int ans =add(3);
  2. if u call the method with 2 ints it will return addition of them
    Example.
    int ans =add( 3, 4 );
  3. and with 1 double and 1 int i will return again the sum.
    Example.
    int ans =add( 3.5, 4 );

This was simple example but we can also overload as many times u want, it will provide very flexibility to the software.

as always doubts invited.
have a nice 1, Peace.

, , ,

Leave a comment

Design a site like this with WordPress.com
Get started