Saturday, June 1, 2013

OOP in ABAP Part 2 (Examples on Constructor, Class Constructor, Inheritance & Interface )

 1.       Constructor
Constructor is a special instance method and always named CONSTRUCTOR. It is automatically called at runtime with CREATE OBJECT statement. It must be defined in public area. It can have only importing parameters and exceptions. On the other hand static constructor or class constructor is always named CLASS_CONSTRUCTOR. It is called automatically when the class is accessed for the first time. It can’t have parameters or exceptions. It can’t be called explicitly.

Example on constructor:

2.       Inheritance
A class can inherits all properties from another class termed as super class. The subclass can add new components and can replace the implementation of inherited methods. Here the relationship is IS A relation and also termed as specialization. In ABAP there is no multiple inheritance as only one superclass can be specified directly above a class. A superclass is a generalization of subclass. Inheritance is a one sided relationship i.e. the subclass can recognize the superclass but the parent class can’t recognize the child class. During redefinition the signature of superclass method can’t be changed. To access the original components of the parent class subclass can use SUPER. For constructor both the signature and implementation can be changed. Constructor of direct superclass must be called within the constructor of subclass.
Only protected and public components are visible in subclass. Static methods of superclass can’t be redefined. Private components of super class only are accessed via public and protected methods.
Assignments of instance of subclass back to superclass instance is called narrowing cast or up cast and the reverse is called widening cast or downcast.in downcast the instance can only access the shared components. For widening cast error CX_SY_MOSVE_CAST_ERROR is triggered when there is a mismatch of content requirement.

Example on inheritance:

3.       Interface & Abstract Class
In simple words interface can be referred as superclass which can’t be instantiated and have no implementation part with only public section.  So interface has no implementation. It has only empty method declaration and its components are always public. But the visibility of components can be changed within implementing class using aliases.
If a new method is added to the interface then all the classes which implement the interface have to implement the new method else it will throw an error.

Abstract class is a special kind of class which can’t be instantiated .Abstract class should contain at least one abstract method (method without implementation) ,it can have methods with implementation. The subclass of abstract class can be instantiated if they are not abstract.
Unlike interface when a new non abstract method is added to class there is no need of redefining the same in subclasses.

Interface should be used when working with a wide range of objects which enforces no default behavior as it has no implementation and Abstract class is used when there is a need of closely related classes with partial default implementation.

Example on interface:

4.       Multiple Inheritance
Although a class can’t get inherited from two different classes but it can have more than one interface in its definition. The procedure to achieve multiple inheritance is shown pictorially below.

Pictorial View