SAP ABAP : OOPS Concept - Method
A method is a collection of statements that perform some specific task and return the result to the caller. A method can perform some specific task without returning anything. Methods allow us to reuse the code without retyping the code.
Method Declaration
In general, method declarations has six components :
- Modifier-: Defines access type of the method i.e. from where it can be accessed in your application. In ABAP, there 3 type of the access specifiers.
- Public: accessible in all class in your application.
- Protected: accessible within the class in which it is defined and in its subclass(es).
- Private: accessible only within the class in which it is defined.
- The return type : If we declare returning type with keyword RETURNING VALUE in method than it return value otherwise no value will be returned .
- Method Name : local method declaration can start with m_ and any meaning ful name.
- Parameter list : Mostly in ABAP we use IMPORTING and EXPORTING keyword to pass the value or get the results respectively .
- Exception list : The exceptions you expect by the method can throw, you can specify these exception(s). ABAP use EXCEPTION keyword to define the exception .
- Method body : it is enclosed between method and endmethod .
Calling a method
The method needs to be called for using its functionality. There can be three situations when a method is called:
A method returns to the code that invoked it when:
- It completes all the statements in the method
- It reaches a return statement
- Throws an exception
Example :
REPORT ztest_demo_sup.
class lcl_animal DEFINITION. "class declaration
PUBLIC SECTION. "access modifier
DATA name TYPE char10."attributes
METHODS constructor IMPORTING name1 TYPE char10.
METHODS m_eat . "behaviours
ENDCLASS.
DATA lv_dog TYPE REF TO lcl_animal. "declaring object of class
START-OF-SELECTION.
"there is two way to create object of class after release 7.4
CREATE OBJECT lv_dog EXPORTING name1 = 'Milo'. "or
DATA(lv_cat) = NEW lcl_animal( name1 = 'Kitty')."incline declaration with New keyword "supported only release after 7.4 ve
lv_dog->m_eat( ).
lv_cat->m_eat( ).
CLASS lcl_animal IMPLEMENTATION.
METHOD constructor.
name = name1.
WRITE : / name.
ENDMETHOD.
METHOD m_eat.
WRITE : / name ,'is eating'.
ENDMETHOD.
ENDCLASS.
Instance method and Static Method
Example :
REPORT ztest_demo_sup.
class lcl_animal DEFINITION. "class declaration
PUBLIC SECTION. "access modifier
DATA name TYPE char10."attributes
METHODS constructor IMPORTING name1 TYPE char10.
METHODS m_eat . "behaviours instance method
CLASS-METHODS: m_bark. "static method is declared using keyword class data
ENDCLASS.
DATA lv_dog TYPE REF TO lcl_animal. "declaring object of class
START-OF-SELECTION.
"there is two way to create object of class after release 7.4
CREATE OBJECT lv_dog EXPORTING name1 = 'Milo'. "or
DATA(lv_cat) = NEW lcl_animal( name1 = 'Kitty')."incline declaration with New keyword "supported only release after 7.4 ve
lv_dog->m_eat( ).
lv_cat-m_eat( ).
"to access instance method we have to create the object of the class
lcl_animal=>m_bark( ).
"To call static method we use => access modifier and all the static methods can be called without creating object of the class
CLASS lcl_animal IMPLEMENTATION.
METHOD constructor.
name = name1.
WRITE : / name.
ENDMETHOD.
METHOD m_eat.
WRITE : / name ,'is eating'.
ENDMETHOD.
METHOD m_bark.
WRITE : / name ,'is barking'.
ENDMETHOD.
ENDCLASS.
METHOD constructor.
name = name1.
WRITE : / name.
ENDMETHOD.
METHOD m_eat.
WRITE : / name ,'is eating'.
ENDMETHOD.
METHOD m_bark.
WRITE : / name ,'is barking'.
ENDMETHOD.
ENDCLASS.
Memory allocation for methods calls
Memory for this instance is allocated with the instance creation (CREATE OBJECT). Afterwards, the instance attribute is present in memory.
Key Points :
- ABAP doesn't support Overloading. Only exception is the CONSTRUCTOR method. Since we can create CONSTRUCTOR method in each subclass, we can change the signature of this method.
Method Overriding
- The Process of overriding the super class method inside the subclass is called as Method Overriding.
- In case of Local class in order to Override the super class method inside the subclass, the must redeclare the super class method by Using Redefinition Keyword.
- A sub class can redefine only the instance public and Instance Protected methods of the super class.
- When the subclass redefine the super class methods the subclass can’t change visibility of redefined methods.
- Whenever a subclass redefine the super class methods its recommended to call the super class method implementations inside the subclass. This is done by using Super Keyword.
- A super class instance public or Instance Protected method declare as final cant be redefine the subclass.
Key Points :
- Static method cannot be redefined or Overloaded or override in subclass .
- You cannot change the signature of method or attributes in subclass .
- An instance method cannot override a static method, and a static method cannot hide an instance method.
REPORT ztest_demo_sup.
class lcl_animal DEFINITION. "class declaration
PUBLIC SECTION. "access modifier
DATA name TYPE char10."attributes
METHODS constructor IMPORTING name1 TYPE char10.
METHODS m_eat . "behaviours instance method
METHODS: m_bark.
ENDCLASS.
CLASS lcl_pets DEFINITION INHERITING FROM lcl_animal.
PUBLIC SECTION.
DATA pet_name TYPE char10.
METHODS constructor IMPORTING name TYPE char10.
METHODS m_eat REDEFINITION.
ENDCLASS.
DATA lv_dog TYPE REF TO lcl_animal. "declaring object of class
START-OF-SELECTION.
"there is two way to create object of class after release 7.4
CREATE OBJECT lv_dog EXPORTING name1 = 'Milo'. "or
DATA(lv_cat) = NEW lcl_animal( name1 = 'Kitty')."incline declaration with New keyword "supported only release after 7.4 ve
DATA(lv_snake) = NEW lcl_pets( name = 'Cobra' ).
"Note : Just for fun I have used pet as snake cobra :P :)
lv_dog->m_eat( ).
lv_cat->m_eat( )."to access instance method we have to create the object of the class
lv_snake->m_eat( )."child class
CLASS lcl_animal IMPLEMENTATION.
METHOD constructor.
name = name1.
WRITE : / name.
ENDMETHOD.
METHOD m_eat.
* WRITE : / name ,'is eating'.
ENDMETHOD.
METHOD m_bark.
* WRITE : / name ,'is barking'.
ENDMETHOD.
ENDCLASS.
CLASS lcl_pets IMPLEMENTATION.
METHOD constructor.
super->constructor( name1 = name ).
pet_name = name.
ENDMETHOD.
METHOD m_eat.
WRITE : / name ,'is eating'.
ENDMETHOD.
ENDCLASS.
Comments
Post a Comment