- class Ovrlod1
- {
- void show(String s)
- {
- System.out.println("String Args");
- }
- void show(int a)
- {
- System.out.println("Int Args");
- }
- void show(char a)
- {
- System.out.println("Char Args");
- }
- public static void main (String ...x)
- {
- Ovrlod1 o1 = new Ovrlod1();
- o1.show("Hello");
- }
- }
- Output:
- String Args
- //Method Overloading: By Changing Datatype Of The Parameter
- class Ovrlod2
- {
- void show(int a)
- {
- System.out.println("One Parameter");
- }
- void show(int a,int b)
- {
- System.out.println("Two Parameter");
- }
- void show(int a, int b, int c)
- {
- System.out.println("Three Parameter");
- }
- public static void main (String ...x)
- {
- Ovrlod2 o1 = new Ovrlod2();
- o1.show(10);
- o1.show(10,20);
- o1.show(10,20,30);
- }
- }
- Output:
- One Parameter
- Two Parameter
- Three Parameter
- //Method Overloading: By Changing Number Of Parameters
- class Ovrlod3
- {
- void show(int a, String b)
- {
- System.out.println("INT - String");
- }
- void show(String b, int a)
- {
- System.out.println("String - Int");
- }
- public static void main (String ...x)
- {
- Ovrlod3 o1 = new Ovrlod3();
- o1.show( 1, "Hello");
- }
- }
- Output:
- INT-String
- //Method Overloading: By Changing Number Of Parameters
- class Ovrlod4
- {
- void show(long a)
- {
- System.out.println("Long args");
- }
- void show(float a)
- {
- System.out.println("Float args");
- }
- void show(double a)
- {
- System.out.println("Double args");
- }
- void show(char a)
- {
- System.out.println("Char args");
- }
- public static void main (String ...x)
- {
- Ovrlod4 o1 = new Ovrlod4();
- o1.show(10);
- }
- }
- Output:
Long args- //method overloading: Type promotion
- class Ovrlod5
- {
- void show(float a)
- {
- System.out.println("Float args");
- }
- void show(double a)
- {
- System.out.println("Double args");
- }
- public static void main (String ...x)
- {
- Ovrlod5 o1 = new Ovrlod5();
- o1.show(3.7);
- }
- }
- Output:
- Double args
- //By default floating point literal are consider as double.
- class Ovrlod6
- {
- void show(float a)
- {
- System.out.println("Float args");
- }
- void show(double a)
- {
- System.out.println("Double args");
- }
- public static void main (String ...x)
- {
- Ovrlod6 o1 = new Ovrlod6();
- o1.show((float)3.7);
- }
- }
- Output:
- Float args
| Copyright ©2016 | All Rights Reserved |
| Design by Uves Khan |