Sunday, 7 December 2014

What is Constructor ?

 What is Constructor ?

Constructor is a special type of method that is used to initialize the state(means to provide default value to object)  of an object. Constructor is invoked at the time of object creation. It constructs the values i.e. data for the object that is why it is known as constructor.Constructor is just like the instance method but it does not have any explicit(by default) return type.

Rules for creating constructor:

There are basically two rules defined for the constructor.

 >1. Constructor name must be same as its class name.
 >2. Constructor must have no explicit return type.

Types of constructor:

There are two types of constructors:

>1. default constructor (no-arg constructor)
>2. parameterized constructor


1) Default Constructor

A constructor that have no parameter is known as default constructor.

Syntax of default constructor:

<class_name>(){}

Example of default constructor:

In this example, we are creating the no-arg constructor in the Student  class.It will be invoked at the time of object creation.

class Student{
Student() {  System.out.println("Student is created"); }

public static void main(String []args){
Student s =new Student();
   }
}

Output: Student is created

Note: If there is no constructor in a class, compiler automatically creates  a default constructor during compilation of java file.

Que) What is the purpose of default constructor?

Ans) Default constructor provides the default values to the object.

Example of default constructor that displays the default values

class Student{
int id;
String name;
void show() { System.out.println (id+" "+name); }

public static void main(String args[]){
Student s1=new Student();
Student s2=new Student();
s1.show();
s2.show();
  }
}

Output: 0 null
               0 null

Explanation:In the above class,you are not creating any constructor  so compiler provides you a default constructor.Here 0 and null values  are provided by default constructor.

2)Parameterized constructor:

Why use parameterized constructor?

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

Example of parameterized constructor.

A constructor that have parameter is known as parameterized constructor. In this example, we have created the constructor of Student class that have two parameters. We can have any number of parameters in the constructor.

class Student{
int id;
String name;
Student(int i,String n){
id = i;
name = n;
}
void show(){  System.out.println(id+" "+name); }

public static void main(String []args){
Student s1 = new Student(123,"vishwa");
Student s2 = new Student(456,"dolly");
s1.show();
s2.show();
  }
}

Output:123 vishwa
               456 dolly

Que)Does constructor return any value?

Ans: Yes, that is current class instance (You cannot use return type  yet it returns a value).

Copying the values of one object to another like copy constructor in C++ :

There are many ways to copy the values of one object into another. They are:

>1. By constructor
>2. By assigning the values of one object into another
>3. By clone() method of Object class

In this example, we are going to copy the values of one object into another  using constructor.

>1. Copying the values of one object to another using Constructor.

class Student{
int id;
String name;
Student(int i,String n){
id = i;
name = n;
}
Student(Student s){
id = s.id;
name =s.name;
}
void show() {  System.out.println(id+" "+name);}

public static void main(String []args){
Student s1 = new Student(123,"vishwa");
Student s2 = new Student(s1);
s1.show();
s2.show();
   }
}

Output:123 vishwa
            123 vishwa

>2. Copying the values of one object to another without constructor:

We can copy the values of one object into another by assigning the objects values to another object.In this case, there is no need to create the constructor.

Another program of Copying the values of one object to another

class Student{
int id;
String name;
Student(int i,String n){
id = i;
name = n;
 }
Student() {}
void show() { System.out.println(id+" "+name);}

public static void main(String []args){
Student s1 = new Student(123,"vishwa");
Student s2 = new Student();
s2.id=s1.id;
s2.name=s1.name;
s1.show();
s2.show();
  }
}

Output:123 vishwa
               123 vishwa

3) Copying the values of one object to another with clone()  method of  object class:

The clone() method (Object Cloning in Java):

The object cloning is a way to create exact copy of an object. For this  purpose, clone() method of Object class is used to clone an object. The java.lang.Cloneable interface must be implemented by the class whose object clone we want to create.If we don't implement Cloneable interface, clone() method gives CloneNotSupportedException. The clone() method is defined in the Object class.

Syntax of the clone() method is as follows:

protected Object clone() throws CloneNotSupportedException  { }

Why use clone() method ?

The clone() saves the extra processing task for creating the exact copy  of an object. If we perform it by using the new keyword, it will take a lot of processing to be performed that is why we use object cloning.

Example of clone() method (Object cloning)

class Student implements Cloneable{
int rollno;
String name;
Student(int rollno,String name){
this.rollno=rollno;
this.name=name;
 }

public static void main(String args[]){
try{
Student s1=new Student(222,"vishwa");
Student s2=(Student)s1.clone();
System.out.println(s1.rollno+" "+s1.name);
System.out.println(s2.rollno+" "+s2.name);
}
catch(CloneNotSupportedException c) {System.out.println(c);}
   }
}

Output: 222 vishwa
                222 vishwa

Note: As you can see in the above example, both reference variables  have the same value. Thus, the clone() copies the values of an object to another. So we don't need to write explicit code to copy the value of an object to another. If we create another object by new keyword and assign the values of another object to this one, it will require a lot of processing on this object. So to save the extra processing task we use clone() method.

Can constructor perform other task instead of initialization ?

Yes, like object creation, starting a thread, calling method etc. You can perform any operation in the constructor as you perform in the method.

No comments:

Post a Comment