Monday, 28 April 2014

Overloading in Java



1) overloading is applicable only in java.

2) Its applicable between the methods in same class or super class.

3) Overloading helps to implement the polymorphism concept in java.

4) overloading allows to define more methods in same name, so that its give many options to choose method.


overloading Rules:

1) overloading method argument types or argument number or return type should change in argument list.

2) Include anyone option in point (1), access modifiers also can change.

3) Overloading methods can have throws exceptions.

Ex:
class calc{
public int division(int a,int b)
{ c=a+b; return c; }
private double division(double a,double b)
{ c=a+b; return c; }
int division(int a,int b, int c)
{ d=a+b+c; return d; }
public int division(int a,int b)
{ c=a+b; return c; }
private double division(double a,double b,int c) throws ArithmaticException
{ c=a+b; return c; }
}



Overloading in Constructor:

1) constructor always can be overloaded.

2) overriding is not possble as oer rules of overloading.

No comments:

Post a Comment