Constructors

 Once we create objects, compulsorily we have to initialize it. Otherwise the objects are not in a position to respond properly. Constructors are specially designed methods for initializing instance variables/objects. After the creation of objects immediately the constructor will be executed and initialization is performed.

Rules for constructors

  1. Name of the constructor and the name of the class must be same.
  2. There is no return type for constructors, not even void. ( If return type is given, it is treated as a normal method. ie, you can give the name of the class as the name of a normal method,but strictly it is not recommended.)
  3. The access modifiers applicable for constructors are public, <default>, protected and private.
Example:
         
            class test
            {        String name;
                       int  rollno;
                    
                    test( String name, int rollno)
                    {    name = name;
                           rollno = rollno;
                    } 
    
                    public static void main(String[ ] args)
                    {   
                        test t1 = new test( "Kiran", 1);
                        System.out.println(t1.name);
                         System.out.println(t1.rollno);
                    }
            }

Output:  Kiran
                1
Default Constructor

            If you haven't written any constructor explicitly, the compiler will provide a constructor automatically which is called the Default constructor. A default constructor and a constructor written by the programmer will not exist simultaneously.Default constructor is added only when there no constructor written in the class. 
The access modifier of constructor is always the access modifier, but applicable only for public and default classes.

Constructor Overloading
    
        The constructor overloading can be defined as the concept of having more than one constructor with different parameters so that every constructor can perform a different task.
With the use of constructor overloading, objects can be initialized with different data types.

Example :

              class test
            {        String name;
                       int  rollno;
                     test( )
                    {    
                        System.out.println(" No argument constructor");
                    }
                    
                    test( String name, int rollno)
                    {    name = name;
                           rollno = rollno;
                    } 
    
                    public static void main(String[ ] args)
                    {   
                        test t1 = new test( "Kiran", 1);
                        System.out.println(t1.name);
                         System.out.println(t1.rollno);
                        test t2 = new test( );
                    }
            }
  Output :  Kiran
                    1
                    No argument constructor

Comments

Popular posts from this blog

Enhanced for - loop or for-each loop

Arrays