OOPs ABAP – All At One Glance

Ready Reckoner for Object Oriented Programming in ABAP

Technology is changing at a rapid speed – sooner or later ABAP will be converted into OOPs ABAP. In fact, SAP has been promoting Object Oriented Programming for almost two decades, but the lazy ABAPers are still reluctant to learn this style of programming. Many of us know OOPs ABAP, but we do not want to come out of our comfort zone to implement our Object Oriented knowledge in the actual Projects. We still fear the unknown of OOPs.

BUT, FRIENDS, IF WE DO NOT GET WET, HOW WOULD WE LEARN TO SWIM?

Let’s not procrastinate further. Tighten your seat belts and let’s know everything about OOPs ABAP at one glance. Once concept is clear in our temples, our hands will automatically play on the keyboard and OOPs programs will be generated!

Let’s get started!

  1. Why ABAP OOPs is needed – It is because of its advantages.
    • Abstraction – Ability to reflect real world process
    • Encapsulation – Implementation details are hidden
    • Inheritance – Deriving the genetics , more reusability
    • Polymorphism – One object, multiple shapes
  2. Class – It is a blue print or model or template. Two types of class:
    • Local Class – can be created in se38
    • Global class – created in se24

This model/template/blue print is accessed by an Object and it consists of attributes, methods and events.

3. Class has following visibility:

  • Public – Components can be accessed by subclasses and other external classes.
  • Protected – Components can be accessed by class itself and subclasses.
  • Private – Can only be used in methods of the class itself.
CLASS class_name DEFINITION.
                                PUBLIC SECTION.
                                                ....
                                PROTECTED SECTION.
                                                ....
                                PRIVATE SECTION.
                                                ....
                ENDCLASS.

4. Data objects within the class –

  • DATA Statement used for instance attributes- can be changed per instance
  • CLASS-DATA for static attribute – global throughout the class.
CLASS attributes DEFINITION.
                                PRIVATE SECTION.
                                DATA objectValue TYPE i.
                                CLASS-DATA objectCount TYPE i.
ENDCLASS.

5. Methods – To achieve a particular functionality – Contains IMPORTING, EXPORTING,CHANGING and EXCEPTIONS

  • Instance Methods – Invoked by an object and can access all attributes and events. Accessed by objects using single arrow. Lo_obj->method( ).
CLASS class_name DEFINITION.
                PUBIC SECTION.
                                METHODS meth
                                                IMPORTING   .. Ii TYPE type
                                                EXPORTING   .. Ei TYPE type ..
                                                CHANGING    .. Ci TYPE type ..
                                                EXCETPIONS .. Ei
ENDCLASS.
  • Static methods – Invoked by a class and can access only static attributes and events. Accessed using class name and double arrow. CLASS=>method( ).
CLASS class_name DEFINITION.
                PUBIC SECTION.
                                CLASS-METHODS meth
                                                IMPORTING   .. Ii TYPE type
                                                EXPORTING   .. Ei TYPE type ..
                                                CHANGING     .. Ci TYPE type ..
                                                EXCETPIONS .. Ei
 ENDCLASS.
  • Constructor – Implicitly called when each object of a class is created
  • Class Constructor – Called when first time a class is accessed.

6. Constructor: Few important points worth noting:

  • Implicitly called for each instantiation of a new object
  • Each class have no more than one instance constructor
  • Signature – only importing parameters and exceptions
  • When exceptions are raised , instances are not created, no main memory is occupied
  • Must be defined in public area
  • Execute by runtime environment.

7. Functional Method – An important method provided by OOPs

  • Method with any number of input parameters but only one return value.
  • It can be used in operand positions for functions and expressions.
  • They neither have an exporting or changing parameter.
  • The returning parameter must always be passed using the value addition that is , passed by value.

Above were the basics of OOPS. Now, welcome to the advance concepts of OOPs

8. Inheritance – As the name suggests, this feature transcends the features of super class to sub classes.

  • Common components can be maintained centrally.
  • Components in the super class are automatically available in subclass.
  • Subclass contain extensions/changes.
  • Subclasses are dependant on superclasses.
  • Super class is generalized and subclass is specialized.
  • Redefinition – Signature is same and implementation is changed in subclass.
  • Redefinition is not possible with private methods.

9. Interfaces: Interface helps in achieving multiple inheritances in ABAP

  • Contains only definition of methods and no implementations.
  • Helps achieving polymorphism.
  • Multiple classes can implement methods of Interface.
  • All components of an interface are public.
  • Interface methods must be implemented in class.
  • Methods are called by interface name ~ component name.

10. Abstract Class: Abstract classes are actually dead classes which need to get alive.

  • Prevent Instantiation
  • Objects of abstract class cannot be created.
  • Abstract methods are not implemented in the class.
  • Abstract methods are redefined in subclass.
  • Static methods cannot be abstract as they cannot be redefined.

11. Final Classes: Final Keyword is used to stop hierarchy.

  • Prevents class from being inherited.
  • When used with methods, it prevents the method to be redefined.