Skip to content

Commit 79be949

Browse files
committed
MethodOverloading class added
1 parent 1f74ad9 commit 79be949

2 files changed

Lines changed: 105 additions & 0 deletions

File tree

3.13 KB
Binary file not shown.
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package com.concepts._01oops;
2+
3+
public class MethodOverloading
4+
{
5+
6+
public static void main(String[] args)
7+
{
8+
9+
MethodOverloading ml = new MethodOverloading();
10+
11+
ml.add(10, 22);
12+
13+
ml.add(10l, 22l);
14+
15+
ml.add(10.25f, 22.64f);
16+
17+
ml.add(10.25, 22.64);
18+
19+
System.out.println( "max of 10 and 12 is " + ml.maxOfTwo(10, 12));
20+
21+
System.out.println( "max of 10 and 12 is " + ml.maxOfTwo(100, 12000000l));
22+
23+
try
24+
{
25+
ml.methodWithThrowClause();
26+
ml.methodWithThrowClause(007);
27+
ml.methodWithThrowClause("Hi Anil");
28+
}
29+
catch(ArrayIndexOutOfBoundsException e)
30+
{
31+
e.printStackTrace();
32+
}
33+
catch(IndexOutOfBoundsException e)
34+
{
35+
e.printStackTrace();
36+
}
37+
catch(RuntimeException e)
38+
{
39+
e.printStackTrace();
40+
}
41+
}
42+
43+
// modified return type of methods does not impact the overloading
44+
public int add(int num1, int num2)
45+
{
46+
System.out.println("I am in int add method");
47+
System.out.println("Sum of " + num1 + " and " + num2 + " is " + (num1 + num2));
48+
49+
return num1 + num2;
50+
}
51+
52+
public long add(long num1, long num2)
53+
{
54+
System.out.println("I am in long add method");
55+
System.out.println("Sum of " + num1 + " and " + num2 + " is " + (num1 + num2));
56+
57+
return num1 + num2;
58+
}
59+
60+
public float add(float num1, float num2)
61+
{
62+
System.out.println("I am in float add method");
63+
System.out.println("Sum of " + num1 + " and " + num2 + " is " + (num1 + num2));
64+
65+
return num1 + num2;
66+
}
67+
68+
public double add(double num1, double num2)
69+
{
70+
System.out.println("I am in double add method");
71+
System.out.println("Sum of " + num1 + " and " + num2 + " is " + (num1 + num2));
72+
73+
return num1 + num2;
74+
}
75+
76+
// modified access of methods does not impact the overloading
77+
public int maxOfTwo( int num1, int num2)
78+
{
79+
System.out.println("I am in int-int max");
80+
return (num1>num2) ? num1 : num2;
81+
}
82+
83+
protected long maxOfTwo( int num1, long num2)
84+
{
85+
System.out.println("I am in int-long max");
86+
return (num1>num2) ? num1 : num2;
87+
}
88+
89+
// Throws clause of methods does not impact the overloading
90+
public void methodWithThrowClause() throws IndexOutOfBoundsException
91+
{
92+
System.out.println("In IndexOutOfBoundsException method");
93+
}
94+
95+
public void methodWithThrowClause(String msg) throws ArrayIndexOutOfBoundsException
96+
{
97+
System.out.println("In ArrayIndexOutOfBoundsException method");
98+
}
99+
100+
public void methodWithThrowClause(int errorCode) throws RuntimeException
101+
{
102+
System.out.println("In RuntimeException method");
103+
}
104+
105+
}

0 commit comments

Comments
 (0)