Thursday, 13 October 2011

Constructor


An object is constructed using constructor. It called constructor because it constructs the values of data member of the class automatically.

The constructor is a special method for initialization of an object automatically. Whenever an object is created, the constructor is called to initialize the instance variable.

The constructor is always given the same name as the class. The constructor is automatically called immediately after the object is created ,before the new operator completes. It has no return type not even void.  It can be declare as follow
         
  class ClassName
  {
            [variable declaration]
            Classname(Parameter list)
            {
                      // Body of constructor.
         }
        [method declaration]
   }


As shown in above format the constructor can be declared as the same name as class name without any return type.

A constructor may have different types: such as constructors have no arguments, with arguments. A class may have more than one constructor.

Ex,.

class Data
  {
            int no1,no2;
            Data()
                      {
                               no1=10;
                               no2=20;
                      }
  }

Here, the Data is the constructor with no parameter.
It can also having list of parameters. It is known as parameterized constructor.

For ex,
class Data
  {
            int no1,no2;
            Data(int n1,int n2)
                      {
                               no1=n1;
                               no2=n2;
                      }
  }

constructor can be invoked as .
Data obj1=new Data();
Data obj1=new Data(100,200);

Note : When no constructor is defined for a class , then the compiler supplies the default constructor. It automatically initializes all the instance variables to zero.

Ex,
                                 A obj=new A();


Constructors are similar to methods, but with some important differences. 

Topic
Constructors
Methods
Purpose
Create an instance of a class
Group Java statements
Modifiers
Cannot be abstract, final, native, static, or synchronized
Can be abstract, final, native, static, or synchronized
Return type
No return type, not even void
void or a valid return type
Name
Same name as the class (first letter is capitalized by convention) -- usually a noun
Any name except the class. Method names begin with a lowercase letter by convention -- usually the name of an action
this
Refers to another constructor in the same class. If used, it must be the first line of the constructor
Refers to an instance of the owning class. Cannot be used by static methods
super
Calls the constructor of the parent class. If used, must be the first line of the constructor
Calls an overridden method in the parent class
Inheritance
Constructors are not inherited
Methods are inherited
Compiler automatically supplies a default constructor
If the class has no constructor, a no-argument constructor is automatically supplied
Does not apply
Compiler automatically supplies a default call to the superclass constructor
If the constructor makes no zero-or-more argument calls to super, a no-argument call to super is made
Does not apply








Posted by : Ruchita Pandya




















No comments:

Post a Comment