2. • Q- class with Constructor.
• Constructors are used to initialize the object’s
state.
• Like methods, a constructor also contains
collection of statements(i.e. instructions) that are
executed at time of Object creation.
3. Rules of Constructor:
• The constructor is a special method which has the same name
as its class.
• It initializes the object of its class automatically at the time of
creation.
• One important point about constructor is that they have
no return type. Not even void.
• This is because the implicit return type of a class’ constructor is
the class type itself.
• When we create an object, we call the constructor after new
keyword.
4. • So the statements written in the constructor are executed at
the time.
• Constructors can use any access modifier, including private.
• The constructor name must match the name of the class.
• Constructors must not have a return type.
• It's legal (but stupid) to have a method with the same name
as the class.
5. Class classname
{
Type instance variable1;
Type instance variable2;
Classname() //Non parameter constructor
{
Body of constructor1;
}
Classname(parameter list) // Parameterize constructor.
{
Body of constructor2;
}
Type methodname1(parameter list)
{
Body of methods;
}
}
6. Types of constructor
There are two type of constructor in Java:
No-argument constructor:
• A constructor that has no parameter is known as default
constructor.
• If we don’t define a constructor in a class, then compiler
creates default constructor(with no arguments) for the
class.
• And if we write a constructor with arguments or no-argument
then compiler does not create default constructor.
•
Default constructor provides the default values to the object
like 0, null etc. depending on the type.
7. class Abc
{
int num;
String name;
// this would be invoked while object
// of that class created.
Abc()
{
System.out.println("Constructor called");
}
}
8. class Para_No
{
public static void main (String[] args)
{
// this would invoke default constructor.
Abc a1 = new Abc();
// Default constructor provides the default
// values to the object like 0, null
System.out.println(a1.name);
System.out.println(a1.num);
}
}
9. 2)Parameterized Constructor:
• A constructor that has parameters is known as
parameterized constructor.
• If we want to initialize fields of the class(static variable)
with your own values, then use parameterized constructor.
10. // parameterized constructor.
class XYZ
{
// data members of the class.
String name;
int id;
/** constructor would initialized data members
with the values of passed arguments while
object of that class created. */
XYZ(String name, int id)
{
this.name = name;
this.id = id;
} }
11. class With_Para
{
public static void main (String[] args)
{
// this would invoke parameterized constructor.
XYZ x1 = new XYZ(“raj", 1);
System.out.println("Name :" + x1.name + " and Id :" + x1.id);
}
}
13. Constructor Overloading
• Like methods, we can overload constructors for creating
objects in different ways.
• Compiler differentiates constructors on the basis of
numbers of parameters, types of the parameters and order
of the parameters.
• For example:
14. class Demo
{
// constructor with one argument
Demo(String name)
{
System.out.println("Constructor with one " +
"argument - String : " + name);
}
// constructor with two arguments
Demo(String name, int age)
{
System.out.print("Constructor with two arguments : " +
" String and Integer : " + name + " "+ age);
}
15. // Constructor with one argument but with different
// type than previous..
Demo(long id)
{
System.out.println("Constructor with one argument : " +
"Long : " + id);
}
}
16. class OverLoading_Demo
{
public static void main(String[] args)
{
// type 'String'.
Demo d2 = new Demo("Ram");
// Invoke the constructor with two arguments
Demo d3 = new Demo("Dharmesh", 26);
// Invoke the constructor with one argument of type 'Long'.
Demo d4 = new Demo(325614567);
}
}