Level 1 & 2 More Programs
This commit is contained in:
30
MethodOverloading.java
Normal file
30
MethodOverloading.java
Normal file
@@ -0,0 +1,30 @@
|
||||
// Write a program in java to demonstrate method overloading
|
||||
|
||||
public class MethodOverloading {
|
||||
// Overloaded sum(). This sum takes two int parameters
|
||||
public int sum(int x, int y)
|
||||
{
|
||||
return (x + y);
|
||||
}
|
||||
|
||||
// Overloaded sum(). This sum takes three int parameters
|
||||
public int sum(int x, int y, int z)
|
||||
{
|
||||
return (x + y + z);
|
||||
}
|
||||
|
||||
// Overloaded sum(). This sum takes two double parameters
|
||||
public double sum(double x, double y)
|
||||
{
|
||||
return (x + y);
|
||||
}
|
||||
|
||||
// Driver code
|
||||
public static void main(String args[])
|
||||
{
|
||||
MethodOverloading s = new MethodOverloading();
|
||||
System.out.println(s.sum(10, 20));
|
||||
System.out.println(s.sum(10, 20, 30));
|
||||
System.out.println(s.sum(10.5, 20.5));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user