Method overloading in java is the facility which allow us to write same name method with different arguments and hence different implementation.So now with the help of method overloading we can use same method name with different implementation and method overloading should we within same class. Let me explain you with the help of an example.

public  class prog{

void display() {
System.out.println("Method without argument") ;
}

void display(int x) {
System.out.println("X="+x) ;
}

void display(String x){
System.out.println("X="+x) ;
}

void display(String y, int x){
System.out.println(y) ;
System.out.println(x) ;
}

public static void main(String sush[ ]) {

  prog p = new prog();

  p.display( ) ;

  p.display(10) ;

  p.display("sushant") ;

  p.display("sush",20) ;

   }

}

In the above code we can see that display ( ) is overloaded with different arguments.

Important point regarding overloading

1> If we are passing int value then it will search for any overloaded method with integer argument, if not available then it will search for any method with its just higher type like long and again if it is also not available then it will search for its wrapper class definition Integer .For ex-

public  class prog{

void display(Integer x){
System.out.println("Integer="+x);
}

void display(long x){
System.out.println("Long="+x);
}

public static void main(String sush[ ]) {

  prog p = new prog();

  p.display(10);

   }

}

In the above example display method is overloaded with Integer (wrapper class) and long as an argument and from main( ) we had passed the value as int type then it will search for it wider type i.e long , if available then it will execute else it will search for its wrapper class.

Rules to be followed while doing overloading 

1> Method name must be same.
2> Return type can be different.
3> Modifier can be different.
4> Parameters must be different as follows.
     a> Types of parameter.
     b> Number of parameter.
     c> Order of parameters type.

Note 1 : Method overloading will be used to achieve static / compile type ploymorphism. 

Note 2 : Try to execute the program passing byte value and defining overloaded with byte type , short type, int type, long type and lastly its wrapper type then if 

a> Overloaded method with byte type is available then it will execute that method only and if not available then it will search for its just higher i.e short

b> short is also not available then it will search for its more higher type i.e int type. If available then it will execute.

c> int is not available then it will search for its more n more higher type i.e long . If available then it will execute.

d> long is not available then it will search for overloaded method with its wrapper type i.e Byte. If available then it will execute.

e> Wrapper class in not available then it will search for VAR-ARGS ( Variable arguments ) . To know more about VAR-ARGS Click Here ! .

Related Posts:

  • Introduction to Java Like all other programming language it is also a well known programming language basically designed for web application . As we all know that c++ is an objected oriented language but not fully object oriented, an attempt mad… Read More
  • How and Why to use main() method in java? In this post i will tell you something interesting about main() method in java. Like What is the use of main() method? How we are going to use main() method? Why we are using main() method in our java program? You will find… Read More
  • Program to print a simple text in java After installing java and setting the path for it now its turn to have some fun with codes. Now i am going to tell you how to print your text in java. Its a very simple program and basically for beginners. There are some im… Read More
  • Compilation and Interpretation in java In this post i will tell you something about compilation which is a task of compiler and on other side interpretation which is the task of interpreter.  There are few queries related to this topic which i am going … Read More
  • Setting CLASSPATH in Java and configure it in windows ! Setting PATH and CLASSPATH are two different task in Java. Let me make you more clear about PATH setting and CLASSPATH setting in Java. If you had missed your lectures on PATH setting then this post will make you clear vi… Read More

0 comments:

Post a Comment

Blog Archive

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

201976

Online Members

Live Traffic