You are here:   HomeJava ProgrammingInheritancevarargs - Variable-Length Arguments Method in Java Example
by Dinesh Thakur Category: Inheritance

When in a class, we have more then one method with similar name but with different type signatures i.e. with different number of parameters or with different types of parameters, then we say that the method is overloaded. Compiler will recognize which method to execute on the basis of the type and number of parameters used while calling the method.

class Rectangle

          {
              int length,breath,a;
              void getData(int x,int y)
                 {
                     length=x;
                     breath=y;
                 }
                     void getData(int x)
                      {
                          length=x;
                          breath=15;
                       }
                          void getData()
                            {
                                length=60;
                                breath=15;
                            }
                                int getArea()
                                   {
                                       a=length*breath;
                                       return(a);
                                    }
           }
                     class FunctionMethod
                        {
                             public static void main(String args[])
                               {
                                   Rectangle Rect = new Rectangle();
                                   Rectangle Rect1 = new Rectangle();
                                   Rectangle Rect2 = new Rectangle();
                                   Rect.getData();
                                   Rect1.getData(30);
                                   Rect2.getData(40,60);
                                   System.out.println("Area of First Rectangle is : "+Rect.getArea());
                                   System.out.println("Area of Second Rectangle is : "+Rect1.getArea());
                                   System.out.println("Area of Third Rectangle is : "+Rect2.getArea());
                                }
                       }

Function Overloading and Method Overloading



Subscribe To Free Daily Newsletter!

Get Free News Updates Delivered Directly To Your Inbox
About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to contact us.



What's New and Popular





Popular Article