Constructor

Rules :

Constructor name must be same as class name.
Constructor is by default provided by java if we not declare in program.
constuctor contain logic also like method do.

constructor is required to initiate object in java.
There is no need to call constructor externally as it call automatically when object created

constructor support method overloading

Constructor types:

1) default constructor
2) parametrized constructor

default constructor

class student{
    //Declare default constructor
    student(){
	  System.out.println("hello Geeks");
	  }	  
    public static void main(String args[]){
       student s=new student(); //call of default constructor automatically
    }
}	
    

OUTPUT:
hello Geeks
    

Parametrized constructor:

constructor argument value passed during object declaration


class student{   
    //Declare parametrized methods with two parameters-->int ,String	  
	student(int id ,String name){
	  System.out.println("hello Geeks");
	}
	  
	public static void main(String args[]){
	  student s=new student(123,"Sam"); //call of parametrized constructor and parameters are-->int,String
	}
}    
          


OUTPUT:
123 sam    
          

constructor support method overloading:


class student{
    int id;
	String name;
	 
    //Declare method with two parameters -->int ,String	 
	student(int id ,String name){
	 System.out.println("hello Geeks");
	}
	//Declare method with two parameters -->int ,int	 
	student(int id ,int y){
	 System.out.println(x+" "+y);
	}
	  
	public static void main(String args[]){
	  student s=new student(123,"Sam");  //call of constructor in which parameter type-->int ,String
	  student s=new student(456,789);    //call of constructor in which parameter type-->int ,int
	}
}   
          


output:
123 sam
456 789    
          

Address

Plot number-56/1
Near Gurudwara
LandMark :
Opposite A.N.D college
Govindpuri Metro station
New Delhi
Mobile number:
8826034620