0% found this document useful (0 votes)
7 views1 page

Constructors

A constructor is a special method that has the same name as the class and is used to initialize objects of a class. Constructors must not have a return type, may or may not have parameters, and are called automatically when an object is created.

Uploaded by

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

Constructors

A constructor is a special method that has the same name as the class and is used to initialize objects of a class. Constructors must not have a return type, may or may not have parameters, and are called automatically when an object is created.

Uploaded by

hejekdbdghsnwnb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

Constructors

A constructor is a special method that has the same name as the class name

* It must have the same name as the class name

* It must not have any return type, not even void, else it will be treated as an ordinary method

* May or may not have parameters

* Is called automatically whenever an object of the class is created

* Should not be of a private type, otherwise you will not be able to create an object of a class

* Should not be static because it will be invoked each time an object of a class is created

* Can be overloaded

* Cannot be inherited at all

* Used to initialize the objects of a class

If constructors are not manually declared, the java compiler automatically provides a default
constructor for a class to initialize all the instance variables of a class with their default values

Constructors are required in a program to initialize the variables with meaningful values

Default Constructors –

Non-parameterized constructors

Don’t contain any parameter

Parameterised Constructors –

Contain parameters

Values to the parameters are provided at the time of creating an object

You might also like