Wednesday 19 October 2016

Complete Constructors tutorial in JAVA


                                                                                                                           To follow on twitter


Here is some samples on Constructors which are more confucious to understand.
This will help you practicing Constructor sample.


Constructors

Example 1:
public class Thiru {
     /*
      Thiru(){
      //empty implementations
      }
      */
    
void character(){
     System.out.println("optimist");
}
     public static void main(String[] args) {
           // TODO Auto-generated method stub
Thiru th=new Thiru();
th.character();
     }

}            
// when we create an object, the default constructor will be invoked with empty implementations.

Example-2
Constructors are used to initialize the instance variables

public class Thiru {
     int age;
     String fathername;
      Thiru(){
      age=23;
      fathername="Mallesham";
      }
     void details(){
           System.out.println(+age+"\n"+fathername);
     }
    


void character(){
     System.out.println("optimist");
}
     public static void main(String[] args) {
           // TODO Auto-generated method stub
Thiru th=new Thiru();
th.character();
th.details();
     }

}

           

Example-3


public class Thiru {
     int age;
     String fathername;
    
      Thiru(String mob){
      age=23;
      fathername="mallesham";
            }
     void details(){
           System.out.println(+age+"\n"+fathername);
           System.out.println("\n"+mob);//shows error
     }
    
void character(){
     System.out.println("optimist");
}
     public static void main(String[] args) {
           // TODO Auto-generated method stub
Thiru th=new Thiru("8686831655");
th.character();
th.details();
     }
}
Here we get error output.
“mob” is local to the constructor we can’t print the value another method details();

 

Example-4



public class Thiru {
     int age;
     String fathername;
     String mob;
      Thiru(String mob){
      age=23;
      fathername="mallesham";
      this.mob=mob;
      }
     void details(){
           System.out.println(+age+"\n"+fathername);
           System.out.println("\n"+mob);//shows error
     }
    
void character(){
     System.out.println("optimist");
}
     public static void main(String[] args) {
           // TODO Auto-generated method stub
Thiru th=new Thiru("8686831655");
th.character();
th.details();
     }         

}

Here I declared “mob” as a instance variable
 And one “mob” variable there in the constructor;
If we print the mob value in details(); method we will get ‘0’.
Because compiler takes mob as a local variable.
To overcome this, we need to convert the local variable to instance variable.
As       this.mob=mob;

Example-5

public class Thiru {
     int age;
     String fathername;
     String mob;
      Thiru(String mob){
      age=23;
      fathername="mallesham";
      this.mob=mob;
      }
     void details(){
           System.out.println(+age+"\n"+fathername);
           System.out.println("\n"+mob);//shows error
     }
    
void character(){
     System.out.println("optimist");
}
     public static void main(String[] args) {
           // TODO Auto-generated method stub
Thiru th=new Thiru();
//error.
/*We have declared parameterized constructor so we should call the
  Parameterized constructor only*/
th.character();
th.details();
     }

}







Example-6


public class Thiru {
     int age;
     String fathername;
     String mob;
      Thiru(){
      age=23;
      fathername="mallesham";
      mob="mob";
      }
     void details(){
           System.out.println(+age+"\n"+fathername);
           System.out.println("\n"+mob);//shows error
     }
    
void character(){
     System.out.println("optimist");
}
     public static void main(String[] args) {
           // TODO Auto-generated method stub
Thiru th=new Thiru();
new Thiru();
new Thiru();
th.character();
th.details();
th.details();
th.details();
     }

}

//OUTPUT
optimist
23
mallesham
mob
23
mallesham
mob
23
Malleaham
mob

// here the problem is every time it will print default constructor values whenever we call the datails();

To over come this
We use [parameterized constructor.

Example:


public class Thiru {
     int age;
     String fathername;
     String mob;
      Thiru(int age,String mob){
      age=23;
      fathername="mallesham";
      mob="mob";
      }
     void details(){
           System.out.println(+age+"\n"+fathername);
           System.out.println(mob);//shows error
     }
    
void character(){
     System.out.println("optimist");
}
     public static void main(String[] args) {
           // TODO Auto-generated method stub
Thiru th=new Thiru(23,"8686831655");
th.details();
new Thiru(21,"555777");
th.details();
new Thiru(20,"97972525");
th.character();
th.details();


     }

}


// here the problem is the constructor variables are local to that block.
// if we want to print them in details();
  We need to convert the constructor values with the instance variables.

this.mob=mob;


        KEY WORDS


public class Keywords {
     Keywords(){
      this(10);
     System.out.println("0-arg construtor");
     }
     Keywords(int a)
     {
     System.out.println("1-arg construtor");
     }
     public static void main(String[] args) {
           new Keywords();
     }
}

// this key word is used move from one constructor to another.
// It should be first line of the method.
// if we want to access the parameterized constr we need call this(parameters);




Example---super();

class Super{
     Super(){
          
     }
     Super(int b,int f){
           System.out.println("parent class constr");
     }
}
public class Keywords extends Super{
     Keywords()
     {
           /*Compiler generates super();
            the parent class will be executed first
            then child constructor executed.
            
            */
           this(10);
     System.out.println("0-arg construtor");
     }
     Keywords(int s)
     {
           super(45,12);
           //this(); --> invalid expression.
     System.out.println("1-arg construtor");
     }
     public static void main(String[] args) {
           new Keywords();
           new Super(34, 45);
     }
    
}




//when we extend a class to the parent class.
In child class the compiler generates the super(); to access the parent class constructor.

If we create parameterized constructor in parent class with out creating the default constructor compiler generates the error.
It asks us to create a default constructor explicitly.

               
Inheritance

Super class and child class variables representation
Example:



class Supar{
     int a=42,b=67;
}
public class Inheritance extends Supar {
     int a=90,b=80;
     public Inheritance(int a,int b) {
    
           // TODO Auto-generated constructor stub
           //this.a=a;
           //this.b=b;
     }
     void add(){
           System.out.println("a+b="+(this.a+this.b));
           System.out.println("a+b="+(super.a+super.b));
           System.out.println("a+b="+(a+b));
     }
public static void main(String[] args) {
     new Inheritance(12,13).add();
}
}



Instance Block

Here the example of the Instance block

//instance blocks and static blocks:
class Parent{
     int a=5;
     {
           //int a;
           System.out.println("parent instance block"+a);
     }
}
public class Instance extends Parent {
public Instance() {
     // TODO Auto-generated constructor stub
     int a;
     System.out.println("child cons");
     {
           System.out.println("child instance");
     }
}
{
     System.out.println("out side child instance ");
}
public static void main(String[] args) {
     new Instance();
}
}

Inside instance block if we want to declare any value, must initialize it.
We can write ins block inside the cons.
Ins blocks are executed after execution of static block.





STATIC Block


Here the example of the static block


public class Static {
     int x,j;
     {
           System.out.println("ins block");
     }
     public Static() {
           // TODO Auto-generated constructor stub
     System.out.println("cons");
     }
static void print(){
     int a=8;
     int y=5;
     //this.a=x;//error
     //can't use this in static block
     System.out.println("a+y="+(a+y));

}
static{
     int a=3;
    
     System.out.println("static block"+a);
}
public static void main(String[] args) {
     new Static().print();
}
}

Execution sequence:
static block
instance block 
constructor(parent   child)
methods.



Like here