Welcome to my
presentation
Name: Md. Anik Hasan
ID: 201-35-572 Instructor
Section: PC-A Name: S A M Matiur Rahman
Department of Software Department of Software engineering
Engineering
Topics : Constructor in java
What is Constructor ?
Constructor is a special type method which will be called
automatically when you create an object of any class.
The main purpose of using constructor is to initialize
an object.
Properties of constructor
1. Constructor name must be same as class name
2. Constructor will be called automatically
3. Constructor can’t return any value even void.
4. Constructor should not be static
Type of constructor
There are 2 types of constructor.
1. Non-Parameterized
2. Parameterized
Non-Parameterized
Non-Parameterized don't accept values at the time of object creation, but they
can be used to display message.
Parameterized
Parameterized constructors are the one which helps us to accept values at the
time of object creation.
Access Modifiers in Constructor
Private: The access level of a private modifier is only within the class. It cannot be accessed from outside
the class.
Default: The access level of a default modifier is only within the package. It cannot be accessed from
outside the package. If you do not specify any access level, it will be the default.
Protected: The access level of a protected modifier is within the package and outside the package through
child class. If you do not make the child class, it cannot be accessed from outside the package.
Public: The access level of a public modifier is everywhere. It can be accessed from within the class,
outside the class, within the package and outside the package.
Non-Parameterized Constructor
Non-Parameterized Constructor can be use to display
the message.
Parameterized Constructor
Advantages of constructor
We don’t have to use object instance for
assigning the values to class variables.
It helps to reduce LOC(Line of Code)