constructor-in-java

CONSTRUCTOR IN JAVA PART . 12

CONSTRUCTOR IN JAVA

* Constructors are used to initialize the value of data members of a class.

* The Constructors is the member function of the class.

* Constructors have the same name as the class.

* Constructors do not have a return type.

* When the object of the class is created, the Constructor is automatically called.

* They are never virtual. They are declared in the public section.

* There are three types of Constructors :-

1 : Default Constructor

2 : Parameterized Constructor

3 : Copy Constructor

* When we do not provide any Constructors in the program, the compiler generates the default Constructor for us.

1 :- Default Constructor : It has no parameters.

        If we do not define any constructors then the compiler automatically calls the default Constructor. But the thing to remember is that the name of the Constructor is the name of the class. And we can never declare Constructor with the help of return type.

Syntax :

class-Name()

{

// Constructor definition

}

2 :- Parameterized Constructor :

Using this Constructor, we can give different values to the data members of different objects.

Why use parameterized constructor ?

parameterized constructor is used to provide different values to the distinct objects.

Syntax :

class_Name(parameterized_list)

{

}

3 :- Copy Constructor :

In this Constructor we pass the object of the class to another object of the same class.

                   Copy Constructor copying the value of the object to another object of the class. And to copy the values, we have to pass the object whose values we have to copy and when we pass an object into Constructor, we need to use (Address operator).

Syntax :

class – Name (class – name&)

{

}

Rating: 5 out of 5.

Now Friends If you like this post please tell us how can we help you to provide better knowledge?

If this post is use full for you please share the post with your friends.

Please follow as Facebook : www.facebook.com

Please follow as Twitter : www.twitter.com

And more information please visit here : www.weindians.in

Please Subscribe on youtube : www.youtube.com

Leave a Reply

Your email address will not be published.

%d bloggers like this: