0% found this document useful (0 votes)
3 views31 pages

Unit-2 Java Constuctor 2

The document provides an overview of constructors in Java, detailing types such as default and parameterized constructors, as well as constructor overloading. It explains the rules for creating constructors, their purpose in object initialization, and differences between constructors and methods. Additionally, it discusses copying values between objects and the concept of call by value in Java.

Uploaded by

Himakshi Bhoi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views31 pages

Unit-2 Java Constuctor 2

The document provides an overview of constructors in Java, detailing types such as default and parameterized constructors, as well as constructor overloading. It explains the rules for creating constructors, their purpose in object initialization, and differences between constructors and methods. Additionally, it discusses copying values between objects and the concept of call by value in Java.

Uploaded by

Himakshi Bhoi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

UNIT-2

CONSTUCTOR
Constructors in Java

Types of constructors
Index • Default Constructor
• Parameterized Constructor
Constructor Overloading

Prepared by Harsha Chauhan 2


Constructors in Java
In Java, a constructor is a
• block of codes similar to the method.

It is called
• when an instance of the class is created.

At the time of calling constructor,


• memory for the object is allocated in the memory.

It is a special type of method


• which is used to initialize the object.

Prepared by Harsha Chauhan 3


Constructors in Java
Every time an object is created using the new() keyword,
# at least one constructor is called.

It calls a default constructor


# if there is no constructor available in the class.

In such case, Java compiler provides a


# default constructor by default.
Prepared by Harsha Chauhan 4
No-Argument Constructor
(Default Constructor)

Types of
Constructor Parameterized Constructor.

Prepared by Harsha Chauhan 5


Rules for creating Java constructor

There are two rules defined for the constructor.


Constructor name A Constructor A Java constructor
must be must have cannot be
• the same as its class • no explicit return • abstract, static, final,
name type and synchronized

Prepared by Harsha Chauhan 6


Java Default Constructor
A constructor is called "Default Constructor"
• when it doesn't have any parameter.

Syntax for Default Constructor:

<class_name>()
{….........}
Prepared by Harsha Chauhan 7
Java
Default
Constru
ctor

Prepared by Harsha Chauhan 8


Java Default Constructor
• OUTPUT

Prepared by Harsha Chauhan 9


Java
JavaDefault Constructor
Default Constructor
• If there is no constructor in a class, compiler automatically creates
a default constructor.
Class Notebook Class Notebook
{ {
. Compiler Notebook()
. {
.
} }
}
Prepared by Harsha Chauhan 10
The default constructor
is

Why Java • used to provide the


Default
default values
Constructor? • to the object like 0,
null, etc.,
• depending on the type.

Prepared by Harsha Chauhan 11


Java Parameterized Constructor
A constructor

• which has a specific number of parameters


• is called a parameterized constructor.

The parameterized constructor is

• used to provide different values to distinct objects.

However, you can provide the same values also.

Prepared by Harsha Chauhan 12


Java
Peramet
rized
Constru
ctor
Prepared by Harsha Chauhan 13
JavaJava
Parameterized Constructor
Default Constructor

• OUTPUT

Prepared by Harsha Chauhan 14


A constructor is just like a method
• but without return type.

It can also be overloaded like


• Java methods.
Constructor Constructor overloading in Java is a
Overloading • technique of having more than one constructor
• with different parameter lists.
They are arranged in a way that
• each constructor performs a different task.

They are differentiated by the compiler


• by the number of parameters in the list and
• their types.

Prepared by Harsha Chauhan 15


Constru
ctor
Overloa
ding

Prepared by Harsha Chauhan 16


Example of Constructor Overloading
Example of Constructor Overloading

• OUTPUT

Prepared by Harsha Chauhan 17


Difference between constructor and method
Java Constructor Java Method

•A constructor is used to initialize the state of •A method is used to expose the behavior of
an object. an object.
•A constructor must not have a return type. •A method must have a return type.

•The constructor is invoked implicitly. •The method is invoked explicitly.

•The Java compiler provides a default •The method is not provided by the compiler
constructor if you don't have any constructor in a in any case.
class.
•The constructor name must be same as the •The method name may or may not be same
class name. as the class name.
Prepared by Harsha Chauhan 18
Java Copy Constructor
There is no copy constructor
• in Java.

However, we can copy the values


• from one object to another like

There are many ways to copy the values of one object into another in Java.
• By constructor
• By assigning the values of one object into another
• By clone() method of Object class

Prepared by Harsha Chauhan 19


C
O
P
Y
C
O
N
S
T
U
C
T
O
R
Prepared by Harsha Chauhan 20
Java
JavaCopy
Copy Constructor
Constructor

• OUTPUT

Prepared by Harsha Chauhan 21


We can copy the values of
Copying • one object into another
• by assigning the objects values
values
• to another object.
without
constructor In this case,
• there is no need to create the
constructor.

Prepared by Harsha Chauhan 22


Copying
values
without
construct
or

Prepared by Harsha Chauhan 23


Javavalues
Copying Copywithout
Constructor
constructor
• OUTPUT
Obj1 Obj2 After Obj2
copying
106 Krina
id = 106 id = 0 id = 106 106 Krina

Name=Krina Name=Null Name=Krina

Prepared by Harsha Chauhan 24


Javavalues
Copying Copywithout
Constructor
constructor
• OUTPUT

Prepared by Harsha Chauhan 25


Call by value in Java
There is only call by value in java,
• not call by reference.

If we call a method passing a value,


• it is known as call by value.

The changes being done in the called method,


• is not affected in the calling method

Prepared by Harsha Chauhan 26


Call by
value
in Java
Examp
le:

Prepared by Harsha Chauhan 27


Call by value in Java Example:

• OUTPUT

Prepared by Harsha Chauhan 28


Exampl
e of
Call by
Refence

Prepared by Harsha Chauhan 29


Example of Call by Refence

• OUTPUT

Prepared by Harsha Chauhan 30


THANK YOU

Prepared by Harsha Chauhan 31

You might also like