Constructor

So, at this point, you know about variables and what to do with variables. Now, let's take a look how they can be organised in a Class to achieve some functionality.

A class is the place where you instruct your computer to do something. While in the runtime, these instructions get executed.

So, let's look, what are the things that can be placed into a class.

A Class can have a bunch of Variables, a bunch of Methods, a bunch of Constructors.

Well, I know about variables and methods, what is this new term Constructor ?
Constructor is a set of execution to create an instance of your class. The whole purpose of a constructor in its life is to instantiate a class.

Well, if it is a set of execution, can I execute it in my code ?
A constructor is called only once during the lifetime of an object. Constructor gets called when an object is being created.

OK, how does it look like ?
Constructors look like methods but they differ in syntax, it does not specify any return type and it has the same name as the containing class.
OK, let's take a short example.

 /*  
  * Copyright 2014-2015 Palash Kanti Kundu.  
  *  
  * Licensed under the Apache License, Version 2.0 (the "License");  
  * you may not use this file except in compliance with the License.  
  * You may obtain a copy of the License at  
  *  
  *   http://www.apache.org/licenses/LICENSE-2.0  
  *  
  * Unless required by applicable law or agreed to in writing, software  
  * distributed under the License is distributed on an "AS IS" BASIS,  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  * See the License for the specific language governing permissions and  
  * limitations under the License.  
  */  
 /**  
  * @author Palash Kanti Kundu  
  * @version 1.0  
  * @since Jun 29, 2015  
  */  
 public class Demo {  
      Demo() {  
           // I am constructor  
      }  
 }  
Look carefully, Demo() is the constructor of class Demo. It does not specify any return type and exactly the same name as that of the class.

OK, what if I provide a return type to a constructor?
Well, in that case, it will be a method of that class having the same name as that of the class and not a constructor.
Let's take a look on that as well,

 /*  
  * Copyright 2014-2015 Palash Kanti Kundu.  
  *  
  * Licensed under the Apache License, Version 2.0 (the "License");  
  * you may not use this file except in compliance with the License.  
  * You may obtain a copy of the License at  
  *  
  *   http://www.apache.org/licenses/LICENSE-2.0  
  *  
  * Unless required by applicable law or agreed to in writing, software  
  * distributed under the License is distributed on an "AS IS" BASIS,  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  * See the License for the specific language governing permissions and  
  * limitations under the License.  
  */  
 /**  
  * @author Palash Kanti Kundu  
  * @version 1.0  
  * @since Jun 29, 2015  
  */  
 public class Demo {  
      Demo() {  
           // I am constructor  
      }  
     void Demo() {  
           // I am a method with the same name of class  
      }  
 }  

OK, that's fine. What should I do in a constructor ?
Well, you can initialize variables, create database connections, put some code logic, do some business and whatever you can do in a method, you can do the same in a constructor. But it is really a bad practice of writing your whole code into constructor but Java don't have any problem with that. Its your algorithm, its your implementation so you are free to do anything with it.
Let's write a code implementing some business in our Demo() constructor,

 import java.io.BufferedReader;  
 import java.io.IOException;  
 import java.io.InputStreamReader;  
 /*  
  * Copyright 2014-2015 Palash Kanti Kundu.  
  *  
  * Licensed under the Apache License, Version 2.0 (the "License");  
  * you may not use this file except in compliance with the License.  
  * You may obtain a copy of the License at  
  *  
  *   http://www.apache.org/licenses/LICENSE-2.0  
  *  
  * Unless required by applicable law or agreed to in writing, software  
  * distributed under the License is distributed on an "AS IS" BASIS,  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  * See the License for the specific language governing permissions and  
  * limitations under the License.  
  */  
 /**  
  * @author Palash Kanti Kundu  
  * @version 1.0  
  * @since Jun 29, 2015  
  */  
 public class Demo {  
      Demo() throws IOException {  
           // I am constructor. I am implementing business. Ohh no!!!  
           // Guys, I am not meant to implement algorithm
           BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  
           String line = br.readLine();  
           if (line.equals("cold")) {  
                System.out.println("It is cold outside, use jacket");  
           } else if (line.equals("hot")) {  
                System.out.println("It is hot outside, use sunscreen");  
           } else if (line.equals("rainy")) {  
                System.out.println("It is raining outside, use umbrella");  
           } else {  
                System.out.println("What a pleasant weather");  
           }  
      }  
 }  

Well, this constructor is loaded with logic implementation and Java allows us to do such stuffs, but it is bad practice.

Word of caution: REMEMBER CONSTRUCTORS GET CALLED ONLY ONCE IN AN OBJECT LIFETIME ONLY DURING OBJECT CREATION.

Well, that's pretty understandable that, if I write all my code into constructor, I have to create new object every time to achieve the same functionality.

Hey, I knew about constructor, but still you did not mention, how does it gets invoked.
Well, lets invoke our Demo() constructor,

 Demo myDemo = new Demo();  

A new operator is used to instantiate a class which in turn invokes constructor.

Well, there are other possible ways to invoke constructor and we'll go though them as and when required.

What if, I don't provide a constructor ?
Will my class not be created ?
Good Question dear reader, let me get you an answer.

If you are not providing a constructor for your class, Java will do that for you. That is known as default constructor.

So, how does this default constructor look like ?
The default constructor looks like a simple constructor with a call to super ();
Let me show you,

 import java.io.BufferedReader;  
 import java.io.IOException;  
 import java.io.InputStreamReader;  
 /*  
  * Copyright 2014-2015 Palash Kanti Kundu.  
  *  
  * Licensed under the Apache License, Version 2.0 (the "License");  
  * you may not use this file except in compliance with the License.  
  * You may obtain a copy of the License at  
  *  
  *   http://www.apache.org/licenses/LICENSE-2.0  
  *  
  * Unless required by applicable law or agreed to in writing, software  
  * distributed under the License is distributed on an "AS IS" BASIS,  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  * See the License for the specific language governing permissions and  
  * limitations under the License.  
  */  
 /**  
  * @author Palash Kanti Kundu  
  * @version 1.0  
  * @since Jun 29, 2015  
  */  
 public class Demo {  
      public Demo() {  
           super();  
      }  
 }  

What is this super (); ?
Well, this is part of the core concepts in OOP. We'll discuss this very soon.


Prev     Next
Palash Kanti Kundu

No comments:

Post a Comment