2012/01/01

INTERFACE (OO ABAP)

Interface:-

  1. An interface is pure abstract class i.e by default all the methods.
  2. All the component of the interface by default public.
  3. we cannot instantiated the interface because they are not implemented.
  4. A Class has to implement the interface . This class we called the Implementation class.
  5. The class must have to implement all the methods of the interface otherwise the class should be declared as abstract.
  6. whenever the class has to implement the interface the class must redeclare the interface by using interfaces keyword.
  7. Interface must be implemented on public section.
  8. A class can implemented any no of the interface i.e a Class can make use of components of multiple interface which is nothing but multiple inheritance. 
  9. whenever the interface components are referred outside the interface , the component must be prefixed with the name of the interfaces.

Example 1-


REPORT  Z7PM_OOPS32.

interface rectangle.
  
constants : length type i value 10,
              breadth 
type i value 5.
  
methods : area,
            perimeter.
endinterface.

interface square.
  
constants side type i value 5.
    
methods : area,
            perimeter.
endinterface.

class abc definition.
  
public section.
   
interfaces : rectangle,
                square.
   
data r type i.
endclass.

class abc implementation.
  
method rectangle~area.
    r = rectangle~length * rectangle~breadth.
    
write :/ 'Area of rectangle is ',r.
  
endmethod.

  
method rectangle~perimeter.
    r = 
2 * ( rectangle~length + rectangle~breadth ).
    
write :/ 'perimeter of rectangle is ',r.
  
endmethod.

    
method square~area.
    r = square~side * square~side.
    
write :/ 'Area of square is ',r.
  
endmethod.

  
method square~perimeter.
    r = 
4 *  square~side.
    
write :/ 'perimeter of square is ',r.
  
endmethod.


endclass.


start-
of-selection.
data r type ref to rectangle.
data s type ref to square.

data ob type ref to abc.
create object ob.

call method : ob->rectangle~area,
              ob->rectangle~perimeter,
              ob->square~area,
              ob->square~perimeter.

write :/ 'RECTANGLE --> ABC'.
r = ob.
call method : r->area,
              r->perimeter.

write :/ 'SQUARE --> ABC'.
s = ob.
call method : s->area,
              s->perimeter.



Example 2:


REPORT  ZRDD_OOPS33.

interface abc.
      
constants : x type i value 10,
                  y 
type i value 5.
      
methods m1.
endinterface.

class pqr definition.
  
public section.
    
interfaces abc.
    
aliases : a1 for abc~m1.
  
protected section.
    
aliases : a2 for abc~x,
              a3 
for abc~y.
endclass.

class pqr implementation.
  
method a1.
    
data r type i.
    r = a2 + a3.
    
write :/ 'sum is ',r.
  
endmethod.
endclass.

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

call method : ob->a1.

No comments: