Java Method Overloading Example

Category: Java   Tags: Java, Java Basic, Java Method Overloading

In method overloading two or more methods within the same class has same name but different parameter declarations. Method overloading is one of the ways that Java supports polymorphism. Some examples:

                          private void dummy(String s) {}
                          private void dummy(int i) {}
                          private void dummy(String s1, String s2) {}
                          private void dummy(int i1, int i2) {}
                          private double dummy(double d1, double d2) {}
                        

Two overloaded method can have different return type. Return type alone is insufficient to distinguish two versions of a method. Here is one example:

OverloadExample1.java

                          public class OverloadExample1 {

                              private int a;
                              private int b;

                              public OverloadExample1() {
                                  print();
                                  print("test");
                                  print(100);
                              }

                              private void print() {
                                  System.out.println("Print nothing");
                              }

                              private void print(String s) {
                                  System.out.println("Print string: " + s);
                              }

                              private void print(int i) {
                                  System.out.println("Print integer: " + i);
                              }

                              public static void main(String s[]) {
                                  OverloadExample1 ob = new OverloadExample1();
                              }
                          }
                        

Output of this Java program is:

                            Print nothing
                            Print string: test
                            Print integer: 100
                          

Here, print is overloaded. All three version of print differ based on the method parameter type.

Overloading Constructors

In addition to overloading normal methods, you can also overload constructor methods. Here is one Java example:

OverloadExample2.java

                            public class OverloadExample2 {

                                private int width;
                                private int height;

                                public OverloadExample2() {
                                    width = 100;
                                    height = 200;
                                }

                                public OverloadExample2(int n) {
                                    width = n;
                                    height = n;
                                }

                                public OverloadExample2(int w, int h) {
                                    width = w;
                                    height = h;
                                }

                                public void area() {
                                    System.out.println("Area is = " + width * height);
                                }


                                public static void main(String s[]) {
                                    OverloadExample2 ob1 = new OverloadExample2();
                                    ob1.area();

                                    OverloadExample2 ob2 = new OverloadExample2(100);
                                    ob2.area();

                                    OverloadExample2 ob = new OverloadExample2(400, 500);
                                    ob.area();
                                }
                            }
                        

Output:

                            Area is = 20000
                            Area is = 10000
                            Area is = 200000
                          

Here, you can see there are three version of constructor: OverloadExample2(), OverloadExample2(int n) and OverloadExample2(int w, int h) all differ with parameter type.