Method Overloading

  1. class Ovrlod1
  2. {
  3. void show(String s)
  4. {
  5. System.out.println("String Args");
  6. }
  7. void show(int a)
  8. {
  9. System.out.println("Int Args");
  10. }
  11. void show(char a)
  12. {
  13. System.out.println("Char Args");
  14. }
  15. public static void main (String ...x)
  16. {
  17. Ovrlod1 o1 = new Ovrlod1();
  18. o1.show("Hello");
  19. }
  20. }
  21. Output:
  22. String Args
  23. //Method Overloading: By Changing Datatype Of The Parameter
  1. class Ovrlod2
  2. {
  3. void show(int a)
  4. {
  5. System.out.println("One Parameter");
  6. }
  7. void show(int a,int b)
  8. {
  9. System.out.println("Two Parameter");
  10. }
  11. void show(int a, int b, int c)
  12. {
  13. System.out.println("Three Parameter");
  14. }
  15. public static void main (String ...x)
  16. {
  17. Ovrlod2 o1 = new Ovrlod2();
  18. o1.show(10);
  19. o1.show(10,20);
  20. o1.show(10,20,30);
  21. }
  22. }
  23. Output:
  24. One Parameter
  25. Two Parameter
  26. Three Parameter
  27. //Method Overloading: By Changing Number Of Parameters
  1. class Ovrlod3
  2. {
  3. void show(int a, String b)
  4. {
  5. System.out.println("INT - String");
  6. }
  7. void show(String b, int a)
  8. {
  9. System.out.println("String - Int");
  10. }
  11. public static void main (String ...x)
  12. {
  13. Ovrlod3 o1 = new Ovrlod3();
  14. o1.show( 1, "Hello");
  15. }
  16. }
  17. Output:
  18. INT-String
  19. //Method Overloading: By Changing Number Of Parameters
  1. class Ovrlod4
  2. {
  3. void show(long a)
  4. {
  5. System.out.println("Long args");
  6. }
  7. void show(float a)
  8. {
  9. System.out.println("Float args");
  10. }
  11. void show(double a)
  12. {
  13. System.out.println("Double args");
  14. }
  15. void show(char a)
  16. {
  17. System.out.println("Char args");
  18. }
  19. public static void main (String ...x)
  20. {
  21. Ovrlod4 o1 = new Ovrlod4();
  22. o1.show(10);
  23. }
  24. }
  25. Output:Long args
  26. //method overloading: Type promotion
  1. class Ovrlod5
  2. {
  3. void show(float a)
  4. {
  5. System.out.println("Float args");
  6. }
  7. void show(double a)
  8. {
  9. System.out.println("Double args");
  10. }
  11. public static void main (String ...x)
  12. {
  13. Ovrlod5 o1 = new Ovrlod5();
  14. o1.show(3.7);
  15. }
  16. }
  17. Output:
  18. Double args
  19. //By default floating point literal are consider as double.
  1. class Ovrlod6
  2. {
  3. void show(float a)
  4. {
  5. System.out.println("Float args");
  6. }
  7. void show(double a)
  8. {
  9. System.out.println("Double args");
  10. }
  11. public static void main (String ...x)
  12. {
  13. Ovrlod6 o1 = new Ovrlod6();
  14. o1.show((float)3.7);
  15. }
  16. }
  17. Output:
  18. Float args

-Webpage Under Development-


| Copyright ©2016 | All Rights Reserved |
| Design by Uves Khan |

Free Web Hosting