Thursday, April 10, 2014

What is Constructor and What is Constructor in Apex?

What is Constructor and What is Constructor in Apex?


A constructor is code that is invoked when an object is created from the class blueprint. You do not need to write a constructor for every class. If a class does not have a user-defined constructor, an implicit, no-argument, public one is used.

The syntax for a constructor is similar to a method, but it differs from a method definition in that it never has an explicit return type and it is not inherited by the object created from it.

After you write the constructor for a class, you must use the new keyword in order to instantiate an object from that class, using that constructor. For example, using the following class:

public class TestObject {
// The no argument constructor
public TestObject() {
// more code here
}
}

A new object of this type can be instantiated with the following code:
TestObject myTest = new TestObject();

Definition 2 :-

A constructor is a special method that is used to initialize a newly created object and is  called just after the memory is allocated for the object It can be used to initialize the objects ,to required ,or default values at the time of object creation It is not mandatory for the coder to write a constructor for the class

If no user defined constructor is provided for a class, compiler initializes member variables to its default values.
In order to create a Constructor observe the following rules
  1. It has the same name as the class
  2. It should  not return a value not even void

Assignment 1: Create your First Constructor
Step 1: Type following code in your editor

public class ConstructorDemo{
    integer  value1;
    integer  value2;
    public ConstructorDemo(){
       value1 = 10;
       value2 = 20;
       System.debug('Inside Constructor');
    }
    public void display(){
      System.debug('Value1 is =' +value1);
      System.debug('Value2 is= '+value2);
    }
}

Step 2) Save , and test the code.
     
Demo d1 = new Demo();
d1.display();


Constructor overloading

Constructor overloading is a technique in apex in which a class can have any number of  constructors that differ in parameter lists.The compiler differentiates these constructors by taking into account the number of parameters in the list and their type
Examples of valid  constructors for class Account are

Account(int a);
Account (int a,int b);
Account (String a,int b);

EX:-

Public class ConstructorOverloading{
     integer  value1;
     integer  value2;
     /*public ConstructorOverloading(){
      value1 = 10;
      value2 = 20;
      System.out.println("Inside 1st Constructor");
    }*/
Public ConstructorOverloading (integer a){
     value1 = a;
     System.debug(‘Inside 2nd Constructor’);
   }
Public ConstructorOverloading (integer a,integer b){
   value1 = a;
   value2 = b;
   System.debug(‘Inside 3rd Constructor’);
  }
  public void display(){
     System.debug(‘Value1 is = ‘+value1);
     System.debug(‘Value2 = ‘+value2);
 }

}

To Test the code.

  ConstructorOverloading d1 = new ConstructorOverloading();
  ConstructorOverloading d2 = new ConstructorOverloading (30);
  ConstructorOverloading d3 = new ConstructorOverloading (30,40);
  d1.display();
  d2.display();
  d3.display();
Constructor chaining
Consider a  scenario where a base class is extended by a child .Whenever an object of the child class is created , the constructor of the parent class is invoked first.This is called Constructor chaining.

Copy the following code in the editor

Public class Demo{
  integer  value1;
  integer  value2;
   public Demo(){
     value1 = 1;
     value2 = 2;
     System.debug(‘Inside 1st Parent Constructor’);
  }
  Public Demo(integer a){
     value1 = a;
     System.debug (‘Inside 2nd Parent Constructor’);
  }
 public void display(){
    System.debug(‘Value1 === ‘+value1);
    System.debug(‘Value2 === ‘+value2);
 }
  
}
//class 2
Public class DemoChild extends Demo{
   integer value3;
   integer  value4;
   public DemoChild(){
   //super(5);
    value3 = 3;
    value4 = 4;
   System.debug(‘Inside the Constructor of Child’);
   }
   public void display(){
     System.debug (‘Value1 ===’+value1);
     System.debug (‘Value2 ===’ +value2);
     System.debug (‘Value1 ===’+value3);
     System.debug (‘Value2 ===’ +value4);
  }
}

Run the Code. Owing to constructor chaining , when object of child class DemoChild is created , constructor Demo() of the parent class is invoked first and later constructor DemoChild() of the child is created.

Expected Output  =
Inside 1st Parent Constructor
Inside the Constructor of Child
Value1 === 1
Value2 === 2
Value1 === 3
Value2 === 4

You may observe the constructor of the parent class Demo is overridden . What is you want to call the overridden constructor Demo(int a) instead of the default constructor Demo() when your child object is created ???
In such cases you can use the keyword “super” to call overridden constructors of the parent class.

Syntax:-
super();
--or--
super(parameter list);

Ex: If your constructor is like Demo(String Name,int a)
you will specify super(‘Apex’,5)
If used ,the keyword super needs to be the first line of code in  the constructor of the child class.





0 comments:

Post a Comment

 
| ,