2012/01/03

CONSTRUCTOR ( OO ABAP)

Constructor:-
  1. A constructor is special methods used for initialize the attributes of the class.
  2. It is special because called Implicitly whenever an object is created  or whenever a class is loaded.
  3. It never returns any values.
  4. They are two types of constructor Instance constructor and Static constructor.
Instance Constructor:- 
  1. It's declared by using keyword 'CONSTRUCTOR'.
  2. It's executed whenever the class is instantiated i.e whenever a new object is created.
  3. It is object specific.
  4. It contains only importing parameter's and exceptions.
  5. It's executed only one'sin the life time of a Object. 
Static Constructor:-
  1. It is declared by using keyword 'CLASS-CONSTRUCTOR'.
  2. It will executed whenever a class is loaded.
  3. The class is loaded in two cases- (a) whenever we access the static component of the class using the class name before creating any objects (b) when we create the first object of the class.
  4. It cannot contain any parameters and exceptions.
  5. It is not object specific.
  6. It is executed only one's in the life time of a class.
(Static constructor are executed first and then Instance constructor)

REPORT  ZRDD_OOPS16.

class abc definition.
  
public section.
    
methods constructor.  "instance const
    
methods m1.  "instance normal method
    
class-methods class_constructor.
endclass.

class abc implementation.
  
method constructor.
    
write :/ 'instance const'.
  
endmethod.

  
method m1.
    
write :/ 'instance method'.
  
endmethod.

  
method class_constructor.
    
write :/ 'inside static const'.
  
endmethod.

endclass.

start-
of-selection.

data ob1 type ref to abc.
create object ob1.

call method ob1->m1.
call method ob1->m1.

data ob2 type ref to abc.
create object ob2.

example 2:

REPORT  ZRDD_OOPS17.

class abc definition.
  
public section.
    
class-data x type i.
    
class-methods class_constructor.
endclass.

class abc implementation.
 
method class_constructor.
   
write :/ 'inside static const'.
 
endmethod.
endclass.

start-
of-selection.
abc=>x = 
10.

data ob1 type ref to abc.
create object ob1.

EXample 3:

REPORT  ZRDD_OOPS18.

class abc definition.
  
public section.
     
methods constructor.
     
methods m1.
  
protected section.
     
data : empno type i,
            ename(
20type c.
endclass.

class abc implementation.
  
method constructor.
    empno = 
10.
    ename = 
'abc'.
  
endmethod.

  
method m1.
    
write :/ empno,ename.
  
endmethod.
endclass.

  start-
of-selection.
  
data ob type ref to abc.
  
create object ob.

  
call method ob->m1.


Hierarchy of constructor execution:-

  1. Whenever the super class as well as the subclass contains instance & static constructor's it must for sub class  instance constructor to call the super class instance constructor explicitly this done by using super keyword.
  2. This is the only place where a constructor can be or has to called explicitly .
  3. In this case if we instantiated the sub class then first static constructor are executed from super class to subclass and then instance constructor are executed from sub class to super class.
Example 1:

REPORT  ZRDD_OOPS27.

class abc definition.
  
public section.
    
methods constructor.
    
class-methods class_constructor.
endclass.

class abc implementation.
  
method constructor.
    
write :/ 'inside instance const - super class'.
  
endmethod.

  
method class_constructor.
      
write :/ 'inside static const - super class'.
  
endmethod.
endclass.

class pqr definition
      
inheriting from abc.

endclass.

start-
of-selection.
data ob type ref to pqr.
create object ob.


Example 2:

REPORT  ZRDD_OOPS28.

class abc definition.
  
public section.
    
methods constructor.
    
class-methods class_constructor.
endclass.

class abc implementation.
  
method constructor.
    
write :/ 'inside instance const - super class'.
  
endmethod.

  
method class_constructor.
      
write :/ 'inside static const - super class'.
  
endmethod.
endclass.

class pqr definition
      
inheriting from abc.
  
public section.
    
class-methods class_constructor.
endclass.

class pqr implementation.
  
method class_constructor.
      
write :/ 'inside static const - sub class'.
  
endmethod.
endclass.

start-
of-selection.
data ob type ref to pqr.
create object ob.

Example 3:

REPORT  ZRDD_OOPS29.

class abc definition.
  
public section.
    
methods constructor.
    
class-methods class_constructor.
endclass.

class abc implementation.
  
method constructor.
    
write :/ 'inside instance const - super class'.
  
endmethod.

  
method class_constructor.
      
write :/ 'inside static const - super class'.
  
endmethod.
endclass.

class pqr definition
      
inheriting from abc.
  
public section.
    
methods constructor.
    
class-methods class_constructor.
endclass.

class pqr implementation.

  
method constructor.
    
write :/ 'inside instance const - sub class'.
                
call method super->constructor.
  
endmethod.

  
method class_constructor.
      
write :/ 'inside static const - sub class'.
  
endmethod.
endclass.

start-
of-selection.
data ob type ref to pqr.
create object ob.
























No comments: