Tuesday, April 9, 2013

OOP in ABAP (Part1 – Concepts)


Why to go for object oriented programming from traditional coding?
-          Faster development and maintenance
-          Simplified modeling of the process
-          Reusability of code

OO extension to ABAP is available from release 4.5.
Here the ABAP programs are organized as collections of objects. Objects are the instances of classes. Class is a template for set of objects which share common structure and common behavior.
Classes can be created in se24 which serves as a global class. Classes also can be created inside ABAP program which are local to the program. A class has mainly three sections namely private (accessible only within the class), public (can be accessed outside the class – global data) and protected (can only be accessed by itself and its subclass). Each section can have attributes (data), methods (behavior/functionality), events, exceptions.

Main OOPS concepts are:

1.       Abstraction :
Simplifying the complex reality by modeling in to classes appropriate to the problem.
Classes are to be created only with essential characteristics which distinguishes it from others.

2.       Encapsulation :
It’s a protective wrapper which prevents the code and data from arbitrary access by other code outside of wrapper. Objects are made up of attributes and methods. The publicly defined attributes and methods acts as an interface, which objects can be accessed outside. The private section is used to keep data and methods to keep it safe from random access. The actual implementation is encapsulated means invisible from outside and cannot be accessed directly; the public interface determines how other objects can interact with it.

3.       Inheritance:
It is the method by which a class can get the attributes and behavior from a predefined class.
The inherited subclass can also have its own attributes and behavior along with its parent super class’s attributes and behaviors.

4.       Polymorphism
Ability of objects belonging to different data types to respond to method calls of methods with same name, each one according to an appropriate type specific behavior.


Instance Attribute VS Static Attribute:
There exist only one instance attribute for each instance of a class and there exist one static attribute/class data per each class. Normally static attributes contains information which is applicable to all instances such as types, constants, administrative information, central data etc.

Instance Method, Static /Class Method and Functional Method:
Instance method can access both static and instance components whereas static method can only access static components.  Also for static method access there is no need of instance which means it can directly assess through the class. Method which has a returning parameter is termed as functional method. This can’t have exporting and changing parameter. It follows pass by value.

Constructor and Class Constructor:
Constructor is a special method whose name is always CONSTRUCTOR. It is used to instantiate the object. It is always defined in public section and it can have only importing parameters and exceptions. Whenever an object is instantiated using creates object statement, system creates an object and then it calls the CONSTRUCTOR. When exceptions are raised instances are not created so no memory space is allocated. Whereas class constructor is a static constructor which automatically gets accessed when the system accesses the class for the first time. Static constructor can’t have any parameters and exceptions. It can’t be called explicitly.

Instance Events and Class Events:
Both instance events and static events can be triggered in instance methods but static methods can only trigger static/class events. An event can be public/private/protected. The event handler can have the same visibility or restricted visibility than the event to which it refers. Whenever an event is triggered then the corresponding handler methods that are registered to it are called in sequence.

Abstract Class, Final class and Friend Class:
Abstract class can’t be instantiated. Abstract class should contain at least one abstract method (method without implementation). The subclass of abstract class can be instantiated if they are not abstract. Final class can’t be inherited further. All methods of final class are inherently final. Final method can’t be redefined further. If a method in a class is final then that class can be inherited but the method can’t be redefined. Therefore to restrict the functionalities from changes the method can be made as final. To access the public and protected attributes/methods of one class without inheritance , FRIENDS addition can be used for the class definition of a class  with the name of the other class whose components needs to be accessed. An interface can also be made friend to a class. The inherited subclass of a class which is used as friend for another class, are by default becomes friends of the class.

Narrowing Cast VS Widening Cast:
Narrowing cast/ Up cast refers to assigning the instance of a subclass back to the instance of a super class meaning we are going from a more specific view of an object to less specific view. The opposite is widening cast or down cast.

Exceptions Handling:
Exception refers to a situation that arises while a program is being executed, when there is no point in continuing to run the program in normal way. Class based exceptions are raised by RAISE EXCEPTION statement or by runtime environment. Whenever an exception is raised an exception object is created whose attribute hold the error situation. Try End try block is used to declare a protective section in the code where one can catch one or more exceptions using CATCH statement. When more than one exception is triggered they must be caught in specific generic class sequence order else system generates error message saying ‘Declare exceptions in ascending order’. All exceptions are inherited from class CX_ROOT which has certain predefined methods such as GET_TEXT, GET_SOURCE_POSITION which are inherited by all exception classes.
When an exception is raised the normal program flow is interrupted by system and it tries to navigate to suitable handle which if not found a runtime error occurs. Try- End try block can be nested. There is a CLEAN UP statement within this block which is used to remove the references before leaving the method.
CX_ROOT has three immediate subclass to one of which the exceptions normally refer. They are
1.       CX_STATIC_CHECK
This type of exception has to be explicitly defined in the method signature.  If not declared, it has to be caught else it will result in to run time error. Compiler can identify this type of exception so if declaration or catching is missed then it shows a warning.
2.       CX_DYNAMIC_CHECK
This type of exception has to be explicitly defined in the method signature.  If not declared, it won’t get checked in runtime.
3.       CX_NOCHECK
These must not be declared. If declared gives syntax error.

4 comments: