Constructors
In JAVA
-M Vishnuvardhan,
Dept. of Computer Science,
SSBN Degree College, ATP
SSBN Degree College, ATP M Vishnuvardhan
Introduction
Constructor is a special method of a class used to
initialize the properties of an object.
Rules for creating a constructor
»It should always have the same name as that of class
name.
»It doesn’t have any return type not even void also.
SSBN Degree College, ATP M Vishnuvardhan
Introduction
Syntax :
<<accessSpecifier>> ClassName(<<args>>)
{
======
======
}
Eg:
class Box
{
int length,width,height,volume;
Box()
{
==== //constructor
}
====
}
SSBN Degree College, ATP M Vishnuvardhan
Properties of Constructors
» A constructor cannot be called like a ordinary method.
» A constructor is called only once in the life time of an object.
» A constructor is always invoked at the time of creation of object
with the help of new keyword.
» Every class must have a constructor. Without constructor object
creation is not possible.
» If a constructor is not defined for a class the compiler provides a
constructor for the class.
SSBN Degree College, ATP M Vishnuvardhan
Constructor vs. method
Java Constructor Java Method
Constructor is used to initialize the
state of an object.
Method is used to expose
behaviour of an object.
Constructor must not have return
type.
Method must have return type.
Constructor is invoked implicitly. Method is invoked explicitly.
The java compiler provides a default
constructor if you don't have any
constructor.
Method is not provided by
compiler in any case.
Constructor name must be same as
the class name.
Method name may or may not
be same as class name.
SSBN Degree College, ATP M Vishnuvardhan
Types of Constructors
Constructors in java can be of two types
»Default Constructor:
Constructor provided by the compiler is called as default
constructor or a constructor with out parameters is also called as
default constructor
»Parameterized Constructor:
Constructor with parameters is called as parameterized
constructors.
SSBN Degree College, ATP M Vishnuvardhan
Types of Constructors
Constructors in java can be of two types
»Default Constructor:
Constructor provided by the compiler is called as default
constructor or a constructor with out parameters is also called as
default constructor
»Parameterized Constructor:
Constructor with parameters is called as parameterized
constructors.
SSBN Degree College, ATP M Vishnuvardhan
Questions

Constructors

  • 1.
    Constructors In JAVA -M Vishnuvardhan, Dept.of Computer Science, SSBN Degree College, ATP
  • 2.
    SSBN Degree College,ATP M Vishnuvardhan Introduction Constructor is a special method of a class used to initialize the properties of an object. Rules for creating a constructor »It should always have the same name as that of class name. »It doesn’t have any return type not even void also.
  • 3.
    SSBN Degree College,ATP M Vishnuvardhan Introduction Syntax : <<accessSpecifier>> ClassName(<<args>>) { ====== ====== } Eg: class Box { int length,width,height,volume; Box() { ==== //constructor } ==== }
  • 4.
    SSBN Degree College,ATP M Vishnuvardhan Properties of Constructors » A constructor cannot be called like a ordinary method. » A constructor is called only once in the life time of an object. » A constructor is always invoked at the time of creation of object with the help of new keyword. » Every class must have a constructor. Without constructor object creation is not possible. » If a constructor is not defined for a class the compiler provides a constructor for the class.
  • 5.
    SSBN Degree College,ATP M Vishnuvardhan Constructor vs. method Java Constructor Java Method Constructor is used to initialize the state of an object. Method is used to expose behaviour of an object. Constructor must not have return type. Method must have return type. Constructor is invoked implicitly. Method is invoked explicitly. The java compiler provides a default constructor if you don't have any constructor. Method is not provided by compiler in any case. Constructor name must be same as the class name. Method name may or may not be same as class name.
  • 6.
    SSBN Degree College,ATP M Vishnuvardhan Types of Constructors Constructors in java can be of two types »Default Constructor: Constructor provided by the compiler is called as default constructor or a constructor with out parameters is also called as default constructor »Parameterized Constructor: Constructor with parameters is called as parameterized constructors.
  • 7.
    SSBN Degree College,ATP M Vishnuvardhan Types of Constructors Constructors in java can be of two types »Default Constructor: Constructor provided by the compiler is called as default constructor or a constructor with out parameters is also called as default constructor »Parameterized Constructor: Constructor with parameters is called as parameterized constructors.
  • 8.
    SSBN Degree College,ATP M Vishnuvardhan Questions