Constructor is a special type of method that is used to initialize the state of an object. Constructor is invoked at the time of object creation. It constructs the values i.e. data for the object that is why it is known as constructor.
Constructor is just like the instance method but it does not have any explicit return type.
Rules for creating constructor:
There are basically two rules defined for the constructor.
1. Constructor name must be same as its class name
2. Constructor must have no explicit return type
Types of constructors
There are two types of constructors:
1. default constructor (no-arg constructor)
2. parameterized constructor
A constructor that have no parameter is known as default constructor.
<class_name>(){}
If there is no constructor in a class, compiler automatically creates a default constructor
Default constructor provides the default values to the object.A constructor that have parameter is known as parameterized constructor
Parameterized constructor is used to provide different values to the distinct objects
Tuesday, 3 July 2012
Constructor
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment