Constructor-in-Java .

In this session i have discussed constructor in Java .


Constructor is a special type of method which is used to initialized the value of object . Constructor has no any return type . Constructor name same as the class name so it is called special function .

Types of Constructor :

1-Default constructor .

2-Parameterized constructor .

Constructor syntax :

    class A{
   
            int x,y;
        
            public  A(){}  //Constructor
    }
    

Default Constructor :

A constructor that do not take any argument is called the default constructor . A default constructor is invoked when ever the object of the class is created .

Example :Write a program that demonstrate the use of default constructor in C++ .

    class A{
    
           int x , y;
           
           public A(){ 

                x=10;
                y=20;
                System.out.println("Default constructor is called ");
           }
           void show(){
            
                System.out.println("X : "+x);
                System.out.println("Y : "+y);
           }
           public static void main(String args[]){

                A obj=new A(); 
                obj.show();
           }
    }
   OUTPUT : Default constructor is invoked 
            X : 10
            Y : 20
   

Paramerterized Constructor :

A constructor that take the at least one argument that is called the parameterized constructor . You can initialized the different-diefferent value to object with the help of parameterized constructor .

Example :Write a program that demonstrate the use of parameterized constructor in C++ .

    class A{
    
        int x , y;
        
        public A(int a,int b){
            x=a;
            y=b;
            System.out.println("Parameterized constructor is called ");
        }
        void show(){
            
                System.out.println("X : "+x);
                System.out.println("Y : "+y);
        }
        public static void main(String args[]){

                A obj1=new A(20,10); //Constructor called implicitly
                obj1.show();
                A obj2=new A(50,40);// Constructor called explicitly 
                obj2.show();
        }
    } 
   OUTPUT : Parameterized constructor is called
            X : 20 
            Y : 10
            Parameterized constructor is called
            X : 50 
            Y : 40 

Constructor Overloading :

We can create multiple constructor in a class with different parameter list . It is known as constructor overloading . It is very similar to function overloading .

Example :Write a program that demonstrate the use of constructor overloading in Java .

    class A{
    
          int x,y;
    
          public  A(){              //Default constructor
            
            x=0;
            y=0;
            System.out.println("Default constructor : ");
          }
          public  A(int a){        // Constructor with one argument 
          
             x=a;
             y=0;
             System.out.println("Constructor with one argument is called ");
          }
          public  A(int a, int b){  //Constructor with two argument
          
               x=a;
               y=b;
               System.out.println("Constructor with two argument is called ");
          }  
          public void show(){
            
                System.out.println("X : "+x);
                System.out.println("Y : "+y);
          }
          public static void main(String args[]){
             
               A obj1=new A();//Default constructor called 
               obj1.show();
               A obj2=new A(10);//Constructor with one args called
               obj2.show(); 
               A obj3=new A(10,20);//Constructor with two args called
               obj3.show();
         }
    }
    

     
   OUTPUT : Default constructor is called
            X : 0
            Y : 0
            Constructor with one argument is called
            X : 10 
            Y : 0
            Constructor with two argument is called
            X : 10 
            Y : 20 

Copy Constructor :

There is no copy constructor in java . But we can copy the value from one object to another like C++ .

There are many ways by which you can copy the values from one object to another as follows .

1-By constructor

2-By assigning the values of one object into another

3-By clone method of Object class

1-By constructor

Example :Write a java program that demonstrate the copy values from one object to another using constructor in Java .

    class Employee{
       
       int emp_id;
       String emp_name;
       
       public Employee(int id , String name){
            
             emp_id=id;
             emp_name=name;
           
       }
       public Employee(Employee emp){
            
                emp_id=emp.emp_id;
                emp_name=emp.emp_name;
       }
       void show(){
        
              System.out.println("EmpID : "+emp_id+" "+"EmpName : "+emp_name);
       }
       public static void main(String args[]){

            Employee obj1=new Employee(1,"Rakesh");
            Employee obj2=new Employee(obj1);
            obj1.show();
            obj2.show();
      }
  }  
   OUTPUT : EmpID : 1 EmpName : Rakesh
            EmpID : 1 EmpName : Rakesh 
   

2-By assigning the values of one object into another

Example :Write a java program that demonstrate the copy values from one object to another using assigning in Java .

    class Employee{
       
       int emp_id;
       String emp_name;
       
       public Employee(int id , String name){

            emp_id=id;
            emp_name=name;

       }
       void show(){
        
              System.out.println("EmpID : "+emp_id+" "+"EmpName : "+emp_name);
       }
       public static void main(String args[]){

            Employee obj1=new Employee(1,"Rakesh");
            Employee obj2=new Employee();
            obj2.emp_id=obj1.emp_id;
            obj2.emp_name=obj1.emp_name;
            obj1.show();
            obj2.show();
      }
  }  
   OUTPUT : EmpID : 1 EmpName : Rakesh
            EmpID : 1 EmpName : Rakesh 
   

3-Clone method

It is the best way to copy the value from one object to another object . It is very fast when compare with other above two ways . We will discuss later in details .

Welcome to AndroidATPoint.

back image                     next image