Monday 16 March 2015

Q 08) Constructors And Methods ?




CONSTRUCTOR :A Constructor is a set of instructions designed to initialize an instance of a newly created object.

Two Rules defined for the Constructors :
·         A constructor should have the same name a Class.
·         Constructors must not return a value.


There are two types of constructors :

1.       Default Constructor:         
     
·         Default constructor refers to a constructor that is automatically created by compiler in the absence of explicit constructors.
·         You can also call a constructor without parameters as default constructor because all of its class instance variables are set to default values.

2.       Parameterized Constructor:
               ·         Parameterized constructors are required to pass parameters on creation of objects.

·         If we define only parameterized constructors, then we cannot create an object with default constructor. This is because compiler will not create default constructor. You need to create default constructor explicitly.
  
METHOD :

public     void             add     (int a, int b)     {  }
A Java method is a collection of statements that are grouped together to perform an operation. access 
  return type   name   parameters   Body 


Example programs of Constructors :


1)  Default Constructor :
package org.javanoobs.dconstructor;

public class DefaultConstructor {

     // This is the Constructor
     public DefaultConstructor() {

           System.out.println(" I am inside the default constructor !!! ");
     }

     public static void main(String args[]) {

           // Object created..
           DefaultConstructor dCon = new DefaultConstructor();
     }

}

OUTPUT :
I am inside the default constructor !!!

2)  Parameterized Constructor :

package org.javanoobs.pconstructor;

public class Test2 {

     int a, b; // instance variables.
     static int c;

     // this is the instance block.
     {
           System.out.println(" Inside Instance Block ");
           System.out.println(" C =  " + c);
           System.out.println(" A =  " + a);

     }

     static {

           System.out.println("Inside the static block !!!");
           System.out.println(" C =  " + c);

     }

     public void disp() {

           int d = 1; // Local varables, initialize to 0 or 1 or will result in an
                           // error.
           d += a + b + c;

           System.out.println("d after addition : " + d);
           System.out.println(" Disp method called...");
     }

     public static void main(String args[]) {

           Test2 t; // refference variable .
           t = new Test2(); // t is the object .
           t.disp();
     }

}




package org.javanoobs.pconstructor;

public class ParameterizedConstructor {

     private String name;

     public ParameterizedConstructor(String str) {
           this.name = str;
           System.out.println("I am Inside Parametrized Constructor !!!");
           System.out.println("The parameter Value is : " + str);
     }

     public static void main(String args[]) {

           ParameterizedConstructor pCon = new ParameterizedConstructor(" Java Noobs !!! ");
     }
}

OUTPUT :

I am Inside Parametrized Constructor !!!
The parameter Value is :  Java Noobs !!! 




1 comment: