What is the difference between Abstract and Interface?

PARAMETERSABSTRACTINTERFACE
What is it?Super base class for all sub classes.It is the skeleton. It contains the method and variable definition.
How are the methods and variables defined?The methods and variables can be public, protected and private.The methods and variables are always public. It is explicitly defined in the beginning like below.
INTERFACE zif_interface1 PUBLIC.
How is the instance created?An instance is created through the sub class.
DATA lr_super_abstract_class TYPE REF TO zcl_super_class.
lr_super_abstract_class=
NEW
zcl_sub_class ( ).
Abstract classes cannot be instantiated.
An instance is created through the implemented class.
In the class ‘ZCL_CLASS1’, Interface ‘ZIF_INTERFACE1’ is implemented.
Data lr_interface TYPE REF TO zif_interface1.
DATA lr_interface = NEW zcl_class1().
Interfaces cannot be instantiated.
How are the methods implemented?The methods can be implemented in the same super base abstract class except the abstract methods.The methods can be implemented only in the implementing class. So here the methods are implicitly defined as abstract.
How is it inherited?Sub class instance is created, which inherits the methods and variables of the super abstract class. In the sub class definition, it is explicitly inherited with the keyword ‘INHERITING FROM’ CLASS zcl_sub_class DEFINITION PUBLIC INHERITING FROM zcl_super_class.The class implements the interface. Interfaces can only be mention in the public section.
CLASS zcl_super_class DEFINITION PUBLIC ABSTRACT.
PUBLIC SECTION.
INTERFACES zif_interface1.
Should all methods be implemented? Is there any excuse?All abstract methods must be redefined and implemented in the sub class. Otherwise it gives an error in the immediate subclass inheriting the super class. It cannot be implemented in the super class.All methods from the interfaces must be implemented in the implementing class. This implicitly means that all the methods in the interface are abstract. Otherwise a warning will pop up during syntax check of the implementing class.
Syntax of Method Implementation?METHOD m_method1.
ENDMETHOD.
METHOD zif_interface1~method2.
ENDMETHOD.
We can als use aliases in method definition.
ALIASES method2 FOR zif_interface1~method2.
So in this case method implementation is similar to normal method.
METHOD method2.
ENDMETHOD.

Note : Multiple Inheritance is not allowed in SAP ABAP. SO the subclass can have only one super class. Two sub classes can have the same super class. The multiple inheritance is achieved by implementing more than one interface in the class definition.

Below picture depicts a classic example of Abstract and interface concept

The parent will always provide all the essentials for the child. But the parent will not take back anything from the child. They don’t create the wealth for themselves. In such a perspective, the Abstract base super class cannot be instantiated. only the base class can instantiate. The child must also inherit the good values given by the parents. Abstract super class will implement the methods which can be inherited by the child sub class.

Each and every parent performs the common functionalities such as getting groceries, paying school fees, planning education and cooking food. The common functions can be grouped together. Similary the Interface provides the common functions which can be implemented by every class. Interface has only the definition. The parents will be able to implement these functions based on their financial capacity. So each parent super class will implement their own methods.

How to fully specify the type of a returning parameter in method

CLASS zcl_it_Tsble_returning DEFINITION

PUBLIC

FINAL

CREATE PUBLIC.

PUBLIC SECTION.

TYPES: BEGIN OF structure_local,

field1 TYPE string,

field2 TYPE i,

field3 TYPE float,

END OF structure_local.

TYPES: tt_table_local TYPE TABLE OF structure_local WITH KEY field2.

“ WITH KEY above shows that the local internal table type is fully specified. When it is declared

“without ‘WITH KEY’, then it is not fully specified we will recieve an error while checking the syntax.

DATA pt_Table_local TYPE tt_table_local.

METHODS export_local_table

RETURNING

VALUE(rt_table_local) TYPE tt_table_local.

PROTECTED SECTION.

PRIVATE SECTION.

ENDCLASS.

CLASS zcl_it_table_returning IMPLEMENTATION.

METHOD export_local_table.

rt_table_local = VALUE #( ( field1 = ´ Hundred´ field2 = 100 field3 = ´100.00´ )

(field1 = ´fifty´ field2 = 50 field3 = ´50.00´ ) ).

END METHOD.

ENDCLASS.

How to modify internal tables in SAP ABAP

In the following code snippet, you will understand how to modify internal table of type standard with non unique keys.

READ TABLE lt_internal_table WITH KEY field1 = field1_value ASSIGNING FIELD-SYMBOL(<ls_record>)

<ls_record>-field2 = `field2_value_modified´ .

Now the internal table will be updated with the modified value. So we have here modified the internal table without using modify statement.

How to create different SARS COVID variants in SAP ABAP

Required Elements in creating the classifications.

  • Structure – It includes the details such as variant id, variant name, amino acid changes, potential, spread.
  • Interface Definition – The variant details of the sars covid 2 will be handled here.
  • Abstract super class definition – This class implements the interface. The constructor is used to set the details of the variant.
  • Sars covid variant 1 sub class definition – It inherits the Abstract super class definition.
  • Report creation – It creates the instances of the sars covid 2 variant.

Structure definition:

Before creating the structure, let’s define the Data elements for the fields of the structure. All the fields of the structure can be defined with string type of data element. The variant can be identified with the key element as variant id.Let’s create a data element as ‚ZDE_Variant_string‘ with string type domain. Another Data element ZDE_VARIANT_ID with int4 type domain.

Let’s define the structure name ‚ZSVARIANT_DETAILS‘ with description

The fields of the structure are created as shown below.

  • Field = Variant_id , Data element = ZDE_VARIANT_ID, Domain type INT4
  • Field = Variant_name, Data element = ZDE_VARIANT_STRING, Domain type STRING
  • Field = amino acid changes, Data element = ZDE_VARIANT_STRING, Domain type STRING
  • Field = Potential, Data element = ZDE_VARIANT_STRING, Domain type STRING
  • Field = Spread, Data element = ZDE_VARIANT_STRING, Domain type STRING

Interface definition:

INTERFACE zif_sars_covid_2_variants

    PUBLIC.

METHODS get_variant_details EXPORTING es_variant_details TYPE zsvariant_details.

ENDINTERFACE.

Abstract super class definition and implementation:

CLASS zcl_sars_covid_2 DEFINITION.

    PUBLIC

    ABSTRACT

    CREATE PUBLIC.

     PUBLIC SECTION.

                INTERFACES zif_sars_covid_2_variants.

                METHODS constructor IMPORTING is_variant_details TYPE zsvariant_details.

    PROTECTED SECTION.

    PRIVATE SECTION.

                DATA ps_variant_details TYPE zsvariant_details.

ENDCLASS.

CLASS zcl_sars_covid_2 IMPLEMENTATION.

                METHOD zif_sars_covid_2_variants~get_variant_details.

                               Es_variant_details = ps_variant_details.

                ENDMETHOD.

                METHOD constructor.

                               Ps_variant_details = is_variant_details.

                ENDMETHOD.

ENDCLASS.

Sars covid variant 1 sub class definition

CLASS zcl_sars_covid_variant1  DEFINITION

PUBLIC

INHERITING FROM zcl_sars_covid_2

FINAL

CREATE PUBLIC.

PUBLIC SECTION.

PROTECTED SECTION.

PRIVATE SECTION.

ENDCLASS.

CLASS zcl_sars_covid_variant1 IMPLEMENTATION.

ENDCLASS.

REPORT TO CREATE INSTANCES OF THE COVID 2 VARIANTS.

REPORT zpr_sars_covid_program.

DATA lr_variant1 TYPE REF TO zcl_sars_covid_variant1.

DATA lr_variant2 TYPE REF TO zcl_sars_covid_variant1.

DATA lr_variant3 TYPE REF TO zcl_sars_covid_variant1.

DATA lt_variant_details TYPE TABLE OF ztvariant_details.

DATA ls_variant_details TYPE zsvariant_details.

DATA ls_print_variant_details TYPE zsvariant_details.

FIELD-SYMBOLS <lv_field> TYPE any.

Ls_variant_details-variant_id = 001.

Ls_variant_details-variant_name = ‚VOC 202012/01‘.

Ls_variant_details-amino_acid_changes = ‚ S: del 69-70, del 144, N501Y, A570D, P681H, T716I, S982A, D1118H‘‘.

Ls_variant_details-potential = ‚ increased transmissibility‘

Ls_variant_details-spread = ‚ UK and other european countries‘.

Lr_variant1 = NEW zcl_sars_covid_variant1( is_variant_details = ls_variant_details ).

Lr_variant1->zif_sars_covid_2_variants~get_variant_details( IMPORTING

                                                                                              Es_variant_details = ls_print_variant_details ).

WRITE: / ‚Variant ID:‘, ls_print_variant_details-Variant_id.

WRITE:/ ,Variant name:‘, ls_print_variant_details-variant_name.

WRITE:/ ,Amino acid changes:‘, ls_print_variant_details-amino_acid_changes.

WRITE:/ ,Potential:‘, ls_print_variant_details-potential.

WRITE:/ ,Spread:‘, ls_print_variant_details-spread.

Ls_variant_details-variant_id = 002.

Ls_variant_details-variant_name = ‚ 501.V2‘.

Ls_variant_details-amino_acid_changes = ‚S: D80A, D215G, E484K, N501Y and A701V‘.

Ls_variant_details-potential = ,increased transmissibility‘

Ls_variant_details-spread = ‚ South Africa, two cases recently detected in the UK‘.

Lr_variant2 = NEW zcl_sars_covid_variant1( is_variant_details = ls_variant_details ).

Lr_variant2->zif_sars_covid_2_variants~get_variant_details( IMPORTING

                                                                                              Es_variant_details = ls_print_variant_details ).

Ls_variant_details-variant_id = 003.

Ls_variant_details-variant_name = ‚Danish mink variant‘.

Ls_variant_details-amino_acid_changes = ‚ S: del 69-70, Y453F‘.

Ls_variant_details-potential = , Transmission from mink to humans and community spread confirmed without any change in transmissibility‘

Ls_variant_details-spread = ‚ Denmark‘.

Lr_variant3 = NEW zcl_sars_covid_variant1( is_variant_details = ls_variant_details ).

Lr_variant3->zif_sars_covid_2_variants~get_variant_details( IMPORTING

                                                                                              Es_variant_details = ls_print_variant_details ).

WRITE: / ‚Variant ID:‘, ls_print_variant_details-Variant_id.

WRITE:/ ,Variant name:‘, ls_print_variant_details-variant_name.

WRITE:/ ,Amino acid changes:‘, ls_print_variant_details-amino_acid_changes.

WRITE:/ ,Potential:‘, ls_print_variant_details-potential.

WRITE:/ ,Spread:‘, ls_print_variant_details-spread.

References: Covid variant details are taken from below report.

European centre for Disease Prevention and Control: Risk related to spread of new SARSCoV-2 variants of concern in the EU/EEA

How to do encapsulation in SAP ABAP

CLASS big_boss DEFINITION.

   PUBLIC SECTION.

   METHODS set_name IMPORTING i_name TYPE string.

   METHODS get_name EXPORTING e_name TYPE string.

   METHODS set_age IMPORTING i_age TYPE int4.

   METHODS get_age EXPORTING e_age TYPE int4.

   METHODS set_profession IMPORTING i_profession TYPE string.

   METHODS get_profession EXPORTING e_profession TYPE string.

   PROTECTED SECTION.

   PRIVATE SECTION.

   DATA p_name TYPE string.

   DATA p_age TYPE int4.

   DATA p_profession TYPE string.

ENDCLASS.

CLASS big_boss IMPLEMENTATION.

METHOD set_name.

  p_name = i_name.

ENDMETHOD.

METHOD get_name.

  e_name = p_name.

ENDMETHOD.

METHOD set_age.

   P_age = i_age.

ENDMETHOD.

METHOD get_age.

   E_age = p_age.

ENDMETHOD.

METHOD set_profession.

   P_profession = i_profession.

ENDMETHOD.

METHOD get_profession.

   E_profession = p_profession.

ENDMETHOD.

ENDCLASS.

REPORT TO ACCESS GETTER AND SETTER METHODS.

DATA lr_bigboss TYPE REF TO bigboss.

DATA l_name TYPE string.

DATA l_age TYPE int4.

DATA l_profession TYPE string.

Lr_bigboss = NEW bigboss( ).

Lr_bigboss->set_name(  EXPORTING i_name = ‚Bala‘ ).

Lr_bigboss->set_age( EXPORTING i_age = 24 ).

Lr_bigboss->set_profession( EXPORTING i_profession = ‚Model and Mr. India‘ ).

 Lr_bigboss->get_name( IMPORTING e_name = l_name ).

Lr_bigboss->get_age( IMPORTING e_age = l_age ).

Lr_bigboss->get_profession( IMPORTING e_profession = l_profession ).

WRITE: / ‚NAME OF THE CONTESTANT:‘ , l_name.

WRITE:/ ,AGE OF THE CONTESTANT:‘; l_age.

WRITE:/ ,PROFESSION OF THE CONTESTANT:‘, L_PROFESSION.

Beispiel für Performance optimierten Code

  • Wichtige Felder selektieren ( Select * vermeiden ).

DATA ls_corona TYPE corona.

SELECT person_id  first_name FROM corona INTO  ls_corona.

                WRITE: / ls_corona-person_id, ls_corona-first_name.

ENDSELECT.

  • Statt IF-Anweisung WHERE Clause nutzen.

SELECT person_id first_name FROM corona INTO ls_corona WHERE person_id = 100.

                WRITE: / ls_corona-person_id, ls_corona-first_name.

ENDSELECT.

  • Geschachtelter SELECTs vermeiden.

Beispiel für schlechten Code.

SELECT person_id FROM corona_table_1 INTO ls_corona_table_1.

SELECT city_id FROM corona_table_2 INTO ls_corona_table_2 WHERE postal_code = ls_corona_table_1-postal_code.

ENDSELECT.

ENDSELECT.

Beispiel für besseren Code.

SELECT person_id city_id FROM corona_table_1 INNER JOIN corona_table_2 ON

corona_table_1~postal_code = corona_table_2~postal_code.

ENDSELECT.

Last auf dem DatenbankserverLast auf dem Anwendungsserver
SELECT * FROM corona_table_1 INTO TABLE itab ORDER BY person_idSELECT * FROM corona_table INTO TABLE itab. SORT itab by person_id.