2011/12/31

INHERITANCE (OO ABAP )

Inheritance:-

  1. Inheritance is a process of acquiring the properties of other class.
  2. Inheritance provides reusability of the components.
  3. Only public and protected components can be inherited i.e private components cannot be inherited.
  4. The class which gives the properties is called as super class or Base Class or parent class.
  5. The class which takes the properties is called as Sub-Class or Derived-Class or Child-Class.
Types of Inheritance:-

  1. Single Inheritance:
  2. Multiple Inheritance:
  3. Multilevel Inheritance:
Single Inheritance: A class drived from single super class.

Multiple Inheritance:  A class derived from more than one super class.
(Note: ABAP doesn't support multilevel inheritance directly it can be implemented indirectly through the concept of interface.)

Multilevel Inheritance: A Class drived from another drived class.

Final Class:
final Class cannot be Inherited .
final is a keyword used as part of class definition to declare the local classes.

Example Program:

REPORT  ZRDD_OOPS21.

class cycle definition.
   
public section.
     
methods : setcycle,
               display.
   
protected section.
      
data : wheels type i,
             brakes 
type i,
             colour(
20type c.
endclass.

class cycle implementation.
  
method setcycle.
    wheels = 
2.
    brakes = 
2.
    colour = 
'green'.
  
endmethod.

  
method display.
    
write :/ wheels,brakes,colour.
  
endmethod.
endclass.

class scooter definition inheriting from
                          cycle.
  
public section.
    
methods setscooter.
endclass.

class scooter implementation.
  
method setscooter.
    wheels = 
2.
    brakes = 
4.
    colour = 
'red'.
  
endmethod.
endclass.


class car definition
        
inheriting from scooter.
  
public section.
    
methods setcar.
endclass.

class car implementation.
  
method setcar.
    wheels = 
4.
    brakes = 
5.
    colour = 
'cyan'.
  
endmethod.
endclass.

start-
of-selection.
data ob1 type ref to cycle.
create object ob1.

write :/ 'CYCLE...'.
call method : ob1->setcycle,
              ob1->display.

data ob2 type ref to scooter.
create object ob2.

write :/ 'SCOOTER...'.
call method : ob2->setscooter,
              ob2->display.

data ob3 type ref to car.
create object ob3.

write :/ 'CAR...'.
call method : ob3->setcar,
              ob3->display.

------------------------------------------------
Example 2:



REPORT ZRDD_OOPS26.


class abc definition.
  public section.
     methods m1 final.
endclass.


class abc implementation.
     method m1.
       write :/ 'inside m1 - super class'.
endmethod.


endclass.


class pqr definition inheriting from abc.
 * public section.
   * methods m1 redefinition.
endclass.


start-of-selection.
  data ob type ref to pqr.
  create object ob.
  call method ob->m1.





No comments: