1class Calculate
2{
3 void sum (int a, int b)
4 {
5 System.out.println("sum is"+(a+b)) ;
6 }
7 void sum (float a, float b)
8 {
9 System.out.println("sum is"+(a+b));
10 }
11 Public static void main (String[] args)
12 {
13 Calculate cal = new Calculate();
14 cal.sum (8,5); //sum(int a, int b) is method is called.
15 cal.sum (4.6f, 3.8f); //sum(float a, float b) is called.
16 }
17}
1Method overloading is providing
2two separate methods in a class
3with the same name but different arguments,
4while the method return type
5may or may not be different, which
6allows us to reuse the same method name.
7In my framework==
8I use implicit wait in Selenium. Implicit wait
9is an example of overloading. In Implicit wait
10we use different time stamps such as SECONDS, MINUTES, HOURS etc.,
11A class having multiple methods with
12same name but different parameters
13is called Method Overloading
1Method 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.
2
3Method overriding means defining a method in a child class that is already defined in the parent class with the same method signature, same name, arguments, and return type
1// Overloading by Changin the number of Arguments
2class MethodOverloading {
3 private static void display(int a){
4 System.out.println("Arguments: " + a);
5 }
6
7 private static void display(int a, int b){
8 System.out.println("Arguments: " + a + " and " + b);
9 }
10
11 public static void main(String[] args) {
12 display(1);
13 display(1, 4);
14 }
15}
1/*A class having multiple methods with
2same name but different parameters
3is called Method Overloading*/
4
5public class Names{
6 static void name(String fname,String lname){
7 System.out.println("My firstname is "+fname+" and lastname is "+lname);
8 }
9 static void name(String fname,String mname,String lname){
10
11 System.out.println("My firstname name is "+fname+" and middlename is "+mname+" and lastname is "+lname);
12 }
13 public static void main(String[] args){
14 Names.name("Khubaib","Ahmed");
15 Names.name("Khubaib","Imtiaz","Ahmed");
16 }
17}
1public void Square ( int number )
2{
3 int square = number * number;
4 System.out.printIn(“Method with Integer Argument Called:“+square);
5 }
6public void Square(double number)
7 {
8 double square = number * number;
9System.out.printIn(“Method with double Argument Called:“+square);
10}
11public void Square(long number)
12 {
13long square = number * number;
14System.out.printIn(“Method with long Argument Called:“+square);
15}
16