Tuesday, November 12, 2013

Constructors


This is the 17th post on my blog. This post had to be posted before. But I tried much to finish it quickly as I can. Enjoy with constructors. 


What is the constructor?

This is another valuable thing in programming. You may heard about constructor companies. What they are doing is constructing somethings means building, roads or whatever. Likewise in programming there are also constructors. But they don't creating that buildings or roads. When you creating objects, constructors are always invoked. If you use 'new' keyword, constructors are running. 

Rules of constructor

  • Every class even abstract classes must have a constructor. There can be one or more.
  • Interfaces don't have constructors.
  • Constructor name should be exactly same as the class name.
  • Constructor does not have a return type.
  • Constructors can use any access modifiers (private, protected,default or public).

Default constructor

  • 'The default constructor' is created by default by the compiler if you don't create any constructors in your class, but if the programmer wants to create a constructor, he can type it as,  
                   Class ConstructorTest{
                           ConstructorTest() {  }
                   }

  • The default constructor is always no-args constructor. But no-arg constructor is not necessarily the default.
  • Every constructor has, as its first statement either this() or super()
                   this()   -   overloaded constructor
                   super()   -   superclass constructor  

  • Default constructor provide no-arg call to super(). It means if you don't type any constructor, it will be generated a default constructor and it create no-arg call to super().
  • Default constructor is provide the default values for objects. They may be 0 or null or etc.


public class DefaultCon {
     int id;
     float avg;
     double d;
     String name;
           
     void test(){
          System.out.println(id + " | " + name + " | " + avg + " | " + d);
     }
             
     public static void main(String args[]){
          DefaultCon obj1 = new DefaultCon();
          obj1.test();
     }
} 

In the above section we have talked about default constructor, super() and such like these things. Following shows the auto generated code for what you typed code.




Other constructors with parameters


public class OtherCon {
     int id;
     String name;
  
     OtherCon(int id, String name){
          this.id=id;

          this.name=name;
     }
 
     void display(){
          System.out.println(id + "   " + name);
     }
 
     public static void main(String args[]){
          OtherCon obj1 = new OtherCon(1,"John");
          OtherCon obj2 = new OtherCon(2,"Adam");
  
          System.out.println("ID  Name");
          obj1.display();
          obj2.display();
     }
}


Constructor Overloading


public class OverloadingCon {
     String name;
     int marks;
 
     OverloadingCon(String name, int marks){
          this.marks=marks;
          this.name=name;
     }
 
     OverloadingCon(String name){
          this.name=name;
     }
 
     void display(){
          System.out.println(name+ "  " + marks);
     }
 
     public static void main(String args[]){
          OverloadingCon obj1 = new OverloadingCon("Adam",80);
          OverloadingCon obj2 = new OverloadingCon("John");
  
          obj1.display();
          obj2.display();
     }
 }



Constructor chaining

  • This occurred when a class inherits another class.
  • You know that subclass inherits the things in superclass. 
  • When you create an object from subclass, its constructor is invoked.
  • But thing is, the super class constructor needs to be invoked because of inheritance.
  • It is done by super() keyword and it can be done as follows.


class Fruit{
     String type; 
           
     Fruit(String type){
         this.type=type;
     }
}    


class Apple extends Fruit{

     String color;
     Apple(String type, String color){
          super(type); 
          this.color=color;
     }
 
     void display(){
          System.out.println("Fruit type is: " + type);
          System.out.println("Fruit color is: " + color);
     }
}   
public class ConstructorChain {

     public static void main(String args[]){
           Apple obj = new Apple("Sweet", "Red");
           obj.display();
     }
}



Click here to download above codes