Friday, 16 January 2015

Constructors in java

Constructors:-
Constructor is a special type of method that is used to initialize the object.
Constructor is invoked at the time of object creation.

Advantages of constructors:

1. A constructor eliminates placing the default values.
2. A constructor eliminates calling the normal method implicitly.

  1. A constructor is similar to a method that is used to  initialize the instance variables.
  2. A constructor's name and class name should be same.
  3. A constructor may or may not have parameters.
  4. A constructor without any parameters called default  constructor (or) zero parameterized constructor.
  5. A constructor with one (or) more parameters is called parameterizsed constructor.
  6. A constructor is called only once at the time of creating the objects.
  7. At the time of creating an object,we pass parameters to the parameterized constructor.
  8. A constructor doesn't return any value,not even 'void'.

Difference b/w default constructor and  parameterized constructor :

Default constructor is used to initialize every object with same data.But parametorized constructor is used to initialize each object with different data.

Difference between Constructor and method

Constructor
Method
Constructor is used to initialize the state of an object.
Method is used to expose behaviour of an object.
Constructor must not have return type.
Method must have return type.
Constructor is invoked implicitly.
Method is invoked explicitly.
The java compiler provides a default constructor if you don't have any constructor.
Method is not provided by compiler in any case.
Constructor name must be same as the class name.
Method name may or may not be same as class name.


e: If there is no constructor in a class, compiler automatically creates a default constructor.

  1. class Student{  
  2. int id;  
  3. String name;   
  4. void display()
  5. {
  6. System.out.println(id+" "+name);
  7. }    
  8. public static void main(String args[]){  
  9. Student s1=new Student();  
  10. Student s2=new Student();  
  11. s1.display();  
  12. s2.display();  
  13. }  
  14. }

Parameterized constructor

  1. class Student{  
  2.    int id;  
  3.    String name;  
  4.      
  5.    Student(int i,String n)
  6.   {  
  7.    id = i;  
  8.    name = n;  
  9.    }  
  10.    void display()
  11.          {
  12.             System.out.println(id+" "+name);
  13.          }  
  14.    public static void main(String args[])
  15.  {  
  16.    Student s1 = new Student(111,"aaa");  
  17.    Student s2 = new Student(222,"bbb");  
  18.    s1.display();  
  19.    s2.display();  
  20.   }  
  21. }


what is Constructor overloading:-

Writing two (or) more constructors with the same name but with difference in parameters is called constructor overloading.

Constructor returns a value but, what ?
No.The default return type of a constructor is the object of the same class. You cannot make a constructor explicitly return any other data type.

what is the purpose of constructor?
The purpose of the constructor is to instantiate the class

Note:-

If no constructor is written by the programmer then the JAVA compiler internally adds default constructor with default values in the class.


Constructor Overloading
  • Constructor is a special method that is used to initialize an object.
  • Every class has a constructor,if we don't explicitly declare a constructor for any java class the compiler builds a default constructor for that class.
  • A constructor does not have any return type
  • Constructor in Java can not be abstract, static, final or synchronized
  • Each time a new object is created at least one constructor will be invoked


Constructor overloading
  • Aconstructor can also be overloaded
  • Constructor overloading you have multiple constructor with different signature but only difference is that Constructor doesn't have return type in Java.
Example of constructor overloading

  1. class Student{  
  2.    int id;  
  3.    String name;  
  4.    int age;  
  5.    Student(int i,String n){  
  6.    id = i;  
  7.    name = n;  
  8.    }  
  9.    Student(int i,String n,int a){  
  10.    id = i;  
  11.    name = n;  
  12.    age=a;  
  13.    }  
  14.    void display(){System.out.println(id+" "+name+" "+age);}  
  15.   
  16.    public static void main(String args[]){  
  17.    Student s1 = new Student(111,"aaa");  
  18.    Student s2 = new Student(222,"bbb",25);  
  19.    s1.display();  
  20.    s2.display();  
  21.   }  
  22. }




class Cricketer
{
String name;
String team;
int age;
Cricketer ()  
{
 name ="";
 team ="";
 age = 0;
}
Cricketer(String n, String t, int a)   
{
 name = n;
 team = t;
 age = a;
}
Cricketer (Cricketer ct)     
{
 name = ct.name;
 team = ct.team;
 age = ct.age;
}
public String toString()
{
 return "this is " + name + " of "+team;
}
}

Class test:
{
public static void main (String[] args)
{
 Cricketer c1 = new Cricketer();
 Cricketer c2 = new Cricketer(“Dhoni", "India", 32);
 Cricketer c3 = new Cricketer(c2 );
 System.out.println(c2);
 System.out.println(c3);
 c1.name = "Virat";
 c1.team= "India";
 c1.age = 32;
 System .out. print in (c1);
}
}

this is Dhoni of india
this is Dhoni of india
this is virat of india

No comments:

Post a Comment