Friday, 18 August 2017

Overloading in Java

Overloading in java

Method Overloading is a feature that allows a class to have two or more methods having same name, if their argument lists are different. 

We don’t have to create and remember different names for functions doing the same thing.

Java doesn’t allow user defined overloaded operators. Internally Java overloads operators, for example + is overloaded for concatenation.

By changing number of arguments or parameters

By changing the data type

Code
class sum {

    // Overloaded sum() with two int parameters
    public int sum(int a, int b)
                {
        return (a + b);
    }

    // Overloaded sum() with three int parameters
    public int sum(int a, int b, int c)
                {
         return (a + b + c);
    }

    // Overloaded sum() with two double parameters
    public double sum(double a, double b)
                {
         return (a + b);
    }  
                public int sum(int a, int b, int c,int d)
                {
         return (a + b * c / d);
    }

   
    public static void main(String args[])
                {
        Sum s = new Sum();
        System.out.println(s.sum(20, 30));
        System.out.println(s.sum(20, 10, 30));
        System.out.println(s.sum(7.5, 15.5));
        System.out.println(s.sum(10,20,30,40));
    }
}
Output:
50
60
23.0
25


So in this post we learned about Overloading in java.

2 comments:

  1. Thanks for your informative post on Java application development. This open source platform assists software developers to create stunning mobile application with ease. Further, they can make use of this platform at free of cost. Best Java Training in Chennai | Best JAVA Course in Chennai

    ReplyDelete
  2. Thank you a lot for providing individuals with a very spectacular possibility to read critical reviews from this site.
    java-training in chennai

    ReplyDelete