0% found this document useful (0 votes)
38 views19 pages

Constructor

The document discusses constructors in Java. Constructors are called when objects are created and are used to initialize values. There are default, parameterized, and no-argument constructors. Constructors can be overloaded but not overridden. Memory is allocated for an object when a constructor is called.

Uploaded by

Nada
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)
38 views19 pages

Constructor

The document discusses constructors in Java. Constructors are called when objects are created and are used to initialize values. There are default, parameterized, and no-argument constructors. Constructors can be overloaded but not overridden. Memory is allocated for an object when a constructor is called.

Uploaded by

Nada
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
You are on page 1/ 19

Constructor

Constructor in Java
02 Features of constructor
01 What is a constructor ?

When is constructor called ? 04 Types of constructor


03

05 Overloaded constructor 06 Copy constructor


01
What is a constructor ?
• A constructor in Java is special method ,It is called when an instance of the
class is created.

• Constructor used to create object by assign values to the global variables


at the time of object creation.

• 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.

• It is called constructor because it constructs the values at the time of


object creation.
02
Features of constructor
• Constructor name must be the same as its class
name.
• A Constructor must have no return type.
• A Java constructor cannot be abstract, static, final.
• Constructor can be overloaded but can not be
overriden.
03
When is constructor called ?
• At the time of calling constructor, memory for the object is allocated
in the memory.

• Every time an object is created using the new() keyword, at least one constructor
is called.
04
Types of constructors
In Java, constructors can be divided into 3 types:

• Default Constructor.

• Parameterized Constructor.

• No-Arg Constructor.
Default constructor
• If we do not create any constructor, the Java compiler automatically create a constructor during

the execution of the program. This constructor is called default constructor.The default constructor

initializes any uninitialized instance variables with default values.

- boolean -> false


- byte -> 0
- short -> 0
- int -> 0
- double -> 0.0d
- float -> 0.0f
- object -> null
Parameterized constructor

• A Java constructor can also accept one or more parameters. Such constructors are known as

parameterized constructors (constructor with parameters).


No argument constructor
• Similar to methods, a Java constructor may or may not have any parameters (arguments).

• If a constructor does not accept any parameters, it is known as a no-argument constructor.

• No argument constructor is the same as default constructor but you can write any code in body.
05
Overloaded constructor
• Similar to Java method overloading, we can also create two or more constructors

with different parameters. This is called constructors overloading.


06
Copy constructor
• A copy constructor in a Java class is a constructor that creates an object using another object

of the same Java class.


Instance flow control :
1. Inheritance.

2. Block.

3. Initial global variables.

4. Code inside it.


THANKS
Any questions?

You might also like