Q:

overloading

//https://www.geeksforgeeks.org/overloading-in-java/
// Java program to demonstrate working of method
// overloading in Java. 
  
public class Sum { 
  
    // 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[]) 
    { 
        Sum s = new Sum(); 
        System.out.println(s.sum(10, 20)); 
        System.out.println(s.sum(10, 20, 30)); 
        System.out.println(s.sum(10.5, 20.5)); 
    } 
} 
1
Method Overloading 
Method overloading is providing two separate methods in a class 
with the same name but different arguments, while the method return type 
may or may not be different, which allows us to reuse the same method name.
1) Method Overloading occurs with in the same
class
2) Since it involves with only one class inheritance
is not involved.
3)In overloading return type need not be the same 
4) Parameters must be different when we do
overloading
5) Static polymorphism can be acheived using
method overloading
6) In overloading one method can’t hide the
another
0
Method overloading is providing two separate methods in a class 
with the same name but different arguments, while the method return type 
may or may not be different, which allows us to reuse the same method name.
0
/https://www.geeksforgeeks.org/overloading-in-java/
// Java program to demonstrate working of method
// overloading in Java. 
  
public class Sum { 
  
    // 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[]) 
    { 
        Sum s = new Sum(); 
        System.out.println(s.sum(10, 20)); 
        System.out.println(s.sum(10, 20, 30)); 
        System.out.println(s.sum(10.5, 20.5)); 
    } 
} 
0
program for method overloading in java
0

New to Communities?

Join the community