Monday, 8 February 2016

JAVA Constructors

The purpose of constructor is to perform initialization of our created object. Constructor is just like a method. But method has return type. Constructor will invoke at the time of object creation time by the ‘new’ keyword. Constructor name should be class name.
 Ex:
     class Sample {
        Sample() {
            System.out.println(“I am constructor”);
        }
         public static void main(String []a)
         {
               Sample s=new Sample();
         }
    }
Important Points:
1.       Constructor concept is applicable for every class including abstract class.
2.       Interfaces doesn’t have constructor.
3.       Allowed access modifiers for constructors are public, protected, default, private.
4.       We can’t give return type even void also. If we use return type then that constructor treated as method.
5.       If we not provide constructor the compiler provide default constructor.
6.       For calling super class constructor we will use ‘super’ in child class constructor. This ‘super’ statement should be first statement in child class constructor.
7.       For calling same class overloaded constructor we should use ‘this’
  
Example: Calling overloaded constructor using ‘this’ and calling super class constructor using ‘super’

public class ConsDemo extends superclass
{
          String name;
          int no;
          String course;
         ConsDemo(String name,int no)
         {
            this("B.Tech");//calling overloaded constructor
               this.name=name;
               this.no=no;
         }
          ConsDemo(String course)
         {
            super();//calling super class constructor
                 this.course=course;
               
         }
         public static void main(String[] a)
         {
           ConsDemo cd=new ConsDemo("raju",9);
              //ConsDemo cd1=new ConsDemo("B.Tech");
              System.out.println("Student Name: "+cd.name);
              System.out.println("Student No: "+cd.no);
              System.out.println("Course: "+cd.course);
         }
       }
       class superclass
       {
          superclass()
          {
             System.out.println("super class construcor");
          }
       }
Static block Vs instance block Vs constructor: These three are using for executing some important statement like initializing values to variables, caching some data ect.. Before  creating an object. These three blocks are execution one after another. Below is an example which illustrates the order of execution
 Example:

public class ThreeBlocks {
       {
              System.out.println("I am instance block");
       }
       static{
              System.out.println("I am static block");
       }
      
       public ThreeBlocks() {
              System.out.println("I am constructor");
       }

       public static void main(String[] args) {
              ThreeBlocks t = new ThreeBlocks();
              System.out.println("I am main");
       }
}
Output:
I am static block
I am instance block
I am constructor
I am main

No comments:

Post a Comment