Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Constructor Overloading in Java 
 

In this section you will learn more about Constractor and how to constractors are overloaded in java.

 

Constructor Overloading in Java

                         

Here, you will learn more about Constructor and how constructors are overloaded in Java. This section provides you a brief introduction about the Constructor that are overloaded in the given program with complete code absolutely in running state i.e. provided for best illustration about the constructor overloading in Java.

Constructors are used to assign initial values to instance variables of the class. A default constructor with no arguments will be called automatically by the Java Virtual Machine (JVM). Constructor is always called by new operator. Constructor are declared just like as we declare methods, except that the constructor don't have any return type. Constructor can be overloaded provided they should have different arguments because JVM differentiates constructors on the basis of arguments passed in the constructor.

Whenever we assign the name of the method same as  class name. Remember this method should not have any return type. This is called as constructor overloading. 

We have made one program on a constructor overloading, after going through it the concept of constructor overloading will get more clear. In the example below we have made three overloaded constructors each having different arguments  types so that the JVM can differentiates between the various constructors.

The code of the program is given below:

public class ConstructorOverloading{
  public static void main(String args[]){
    Rectangle rectangle1=new Rectangle(2,4);
    int areaInFirstConstructor=rectangle1.first();
    System.out.println(" The area of a rectangle in 
  first constructor is :  " 
+ areaInFirstConstructor);
    Rectangle rectangle2=new Rectangle(5);
    int areaInSecondConstructor=rectangle2.second();
    System.out.println(" The area of a rectangle in 
   first constructor is :  " 
+ areaInSecondConstructor);
    Rectangle rectangle3=new Rectangle(2.0f);
    float areaInThirdConstructor=rectangle3.third();
    System.out.println(" The area of a rectangle in first 
        constructor is :  " 
+ areaInThirdConstructor);
    Rectangle rectangle4=new Rectangle(3.0f,2.0f);
    float areaInFourthConstructor=rectangle4.fourth();
    System.out.println(" The area of a rectangle in first 
      constructor is :  " 
+ areaInFourthConstructor);
  }
}

class Rectangle{
  int l, b;
  float p, q;
  public Rectangle(int x, int y){
    l = x;
    b = y;
  }
  public int first(){ 
    return(l * b);
  }
  public Rectangle(int x){
    l = x;
    b = x;
  }
  public int second(){
    return(l * b);
  }
  public Rectangle(float x){
    p = x;
    q = x;
  }
  public float third(){
    return(p * q);
  }
  public Rectangle(float x, float y){
    p = x;
    q = y;
  }
  public float fourth(){
    return(p * q);
  }
}

Output of the program is given below:

C:\java>java ConstructorOverloading
The area of a rectangle in first constructor is : 8
The area of a rectangle in first constructor is : 25
The area of a rectangle in first constructor is : 4.0
The area of a rectangle in first constructor is : 6.0

Download this program

                         

» View all related tutorials
Related Tags: c com file ide class files stl date time variables static directory io make variable dates sed get ria this

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

2 comments so far (
post your own) View All Comments Latest 10 Comments:

Hi Sir ,
Pls let me know more about constructor and its application in java programming

Posted by Rajkumar Chaudhary on Tuesday, 09.30.08 @ 09:37am | #80792

hello sir,
explain about the this operator?
Thank u sir...

Posted by maheswari on Saturday, 08.9.08 @ 11:51am | #71853

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.