Method overloading in java program

What is function or method overloading in java program?

– In one class, when we create two same name methods and give different parameters, this is basic set of creation function or method overloading in program.

– Then after, we call these method from our main class, that time compiler take some time to check same and parameter this is called function or method overloading in java program.

 

Let’s see example on function or method overloading in java program

class OverLoad

{

            void fun1()       // No parameter

            {

                        System.out.println(“Welcome in Function Overloading”);

            }

            void fun1(int x,int y)     // Two Argument

            {

                        System.out.println(“Sum of X and Y : ” + (x+y));         

            }

            void fun1(double a,double b)   // Two Argument | Data Type different

            {

                        System.out.println(“Sub of A and B : ” + (a-b));

            }

 

            /*

                        Same method – fun1

                        Different Parameter () , (int,int) , (double,double)

            */

 

}

class clEx3

{

 

            public static void main(String args[])

            {

 

                        OverLoad obj = new OverLoad();        // Object of class OverLoad

 

                        obj.fun1();        // Function Overloading

 

                        obj.fun1(40,30);           // Function Overloading

 

                        obj.fun1(6.7,4.5);         // Function Overloading

 

            }

 

}

 

Output:

Welcome in Function Overloading

Sum of X and Y: 70

Sub of A and B: 2.2

In this example, we can see two types of function or method overloading active in java program.

  1. Parameter different
  2. Parameter same but data types different

 

Simple Class Examples

NoTitle
1Create on Area class with three findArea() methods. One method calculate area of circle, second method calculate area of square and third method calculate area of rectangular.

Video available on YouTube.


[sbscrbr_form]

If you have query about this blog, you can directly post comment or subscribe on website.


 

284total visits,1visits today


mayank

Hello, I am Maddy a Java blogger, trainer and developer

Related articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Your comments (*)

Name (*)

Your full name please.

Email address (*)

Used for gravatar.

Website

Link back if you want.

Enter Captcha Here :*

Reload Image