Turn Desktop View Off
You are here:   HomeJava ProgrammingClasses in JavaCalculate Area of Rectangle using Constructor Overloading
by Dinesh Thakur Category: Classes

Constructor Overloading means a class having multiple constructors with the same name, but with different number of arguments or different type of arguments. The compiler differentiates the constructors based on the number of parameters in the list and their types. That means, on the basis of the number and type of the arguments that we pass into the constructor, the compiler determines which constructor to call.

class Rectangle

         {
            int length,breath;
            Rectangle(int x,int y)
                {
                   length=x;
                   breath=y;
                }
                   Rectangle(int x)
                     {
                         length=x;
                         breath=10;
                     }
                         Rectangle()
                           {
                               length=31;
                               breath=11;
                           }
                               float GetData()
                                  {
                                      return(length*breath);
                                  }
          }
                  class ConstructorOverloading
                         {
                             public static void main(String args[])
                                {
                                    Rectangle Rect = new Rectangle();
                                    Rectangle Rect1 = new Rectangle(200);
                                    Rectangle Rect2 = new Rectangle(200,700);
                                    System.out.println("Area of First Rectangle is : "+Rect.GetData());
                                    System.out.println("Area of Secons Rectangle is : "+Rect1.GetData());
                                    System.out.println("Area of Third Rectangle is : "+Rect2.GetData());
                                }
                         }

Area of Rectangle using Constructor 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