3. 1
Java Constructor
A constructor in Java is a
special method that is used
to initialize objects. The
constructor is called when an
object of a class is created. It
can be used to set initial
values for object attributes
4. 2
Rules for Java Constructor
The constructor
name must match
the name of the
class
The default
constructor is
ALWAYS as no-arg
constructor
Constructors can
use any access
modifier, including
private
Constructors
must not have a
return type.
5. 3
Difference Between Constructor and Method
Constructor are
used to initialize
the state of
object
Constructor
must not have
return type
Constructor
compil er provide
default
constructor .
Method is expose
the behaviour of
object.
Method must have
return type.
Method compiler
does't provide.
1
2
3
1
2
3
7. 5
01
02
A constructor that
have no parameter is
known as default
constructor.
Syntax of default
constructor:
<class_name>(){}
Default Constructor
Class Measuring
{
Private double Metre;
Private double kilometer;
Private double centimeter;
Private double Millimeter;
Measuring()
{
Metre=120; kilometer=10.0; centimeter=12.0;
Millimeter=5.0;
}
Void display()
{
System.out.println(“kilometer=“+kilometer);
}
}
Output:
Kilometer=10.0
8. 6
01
02
A constructor that have
parameters is known as
parameterized
constructor.
Parameterized
constructor is used to
provide different values to
the distinct objects
Parameterize
dconstructor
Class Rectangle
{
int length; int breadth;
Rectangle(int len, int bre)
{
length=len;
breadth=bre;
}
}
Class demo
{
Public static void main(String args[])
{
Rectangle r1= new Rectangle(20,10);
System.out.println(“length of rectangle=“
r1.length);
System.out.println(“breadth of
rectangle=“ r1.breadth);
}}
9. 7
01
02
We can create the object
with any initial value to
extract values in the
parameterized
constructor
Parameterized
constructor can pass a
value as:
1. An implicit call
2. An explicit call
Passing initial values
As arguments
Implicit call constructor:
It retrieves the parameterized
constructor even if it is not declared in
the main().
We can create the object with any
initial value to extract values in the
parameterized constructor
Explicit call constructor:
It helps in creating a temporary
instance.
A temporary instance remains in the
memory as long as it is being used in an
expression.
10. 8
01
02
It is used to create a
temporary object of a
class object.
A copy constructor takes
only one argument, which
is of the type as the class.
Copy
constructor
Class Rectangle
{
int length; int breadth;
Rectangle(int len, int bre)
{
length=len;
breadth=bre;
}
Rectangle(Rectangle obj)
{
System.out.println(“copy constructor
invoked”);
length=obj.len;
breadth=obj.bre;
}
int area()
{
return(length*breadth);
}
11. 9
01
02
It is used to create a
temporary object of a
class object.
A copy constructor takes
only one argument, which
is of the type as the class.
Copy
constructor
Class demo
{
Public static void main(String args[])
{
Rectangle r1= new Rectangle(20,10);
Rectangle r2=new Rectangle(r1);
System.out.println(“area of 1st rectangle=“
r1.area());
System.out.println(“area of 2nd rectangle=“
r2.area());
}}
12. 10
01
02
A constructor may be
overloaded, so a single
class may have more
than one constructor, all
of which have the same
name but different
argument lists.
Constructor
overloading
Class overloading
{
int x,
float y;
Overloading()
{x=0;y=0.0;}
Overloading(int a)
{x=a; y=0;}
Overloading(int a, float b)
{x=a; y=b;}
Public static void main(String args[])
{
Overloading ov1=new overloading();
Overloading ov2=new overloading(40);
Overloading ov3=new overloading(10,4.5);
}
}