Tuesday, June 26, 2012

Overview of OOP's in ABAP/4

OBJECT ORIENTED PROGRAMMING IN ABAP/4

Class : A Class is a user defined data type with attributes,method types and events for a particular entity or business scenario or business application.
Class creation is of 2 step process :
1) class definition 2) class implementation

Object : Objects are instances of classes
Each object has unique identity i.e, memory and its own attributes.
Once an object is created,we have an instance of class with separate memory

Object creation is also of 2 step process.
1) Object/Instance declaration 2) Object creation

Components of a Class :
Attributes : Any data,variables,constants declared within a class are called as attributes of a class
Methods : Methods contain a block of code,providing some functionality for a class. These are similar to function modules in ABAP. Methods can access all the attributes of a class.
Events : Event is a mechanism by which method of one class can raise method of another class, without the hazard of instantiating that class.
Interfaces : Interfaces are similar to classes which contain methods. These are mainly used to extend the scope or functionality of the class.
Interfaces contains methods without any implementation.
where as a class contains methods with implementation.
Interface can be used by 'n' no of classes to extend the functionality of the class.
All the interface methods will be automatically copied to the classes in a particular class without effecting the other classes.

Instance and Static Components :
Instance Component : These components exists separately in each instance(Object) of the class and are referred using instance components selector using '->'.

Static Component: These components exists globally for a class and are referred to using static component selector '=>'.

Visibility components :
Each class component has a visibility
In ABAP Objects,the whole class definition is separated into three visibility section.
PUBLIC : Data declared in public section can be accessed by the class itself, by its subclasses as well as by other users outside the class.
PROTECTED : Data declared in the protected section can be accessed by the class itself, and also by its subclasses but not by external users outside the class.
PRIVATE : Data declared in the private section can be accessed by the class only, but not by its subclasses and by external users outside the class.

Global Class and Local Class :
Classes in ABAP Objects can be declared either globally or locally.

Global Class : Global classes and Interfaces are defined in class builder using SE24 in ABAP workbench.
All ABAP programs in an R/3 system can access the global classes.

Local Class : Local classes are defined in an ABAP program (SE38) and can only be used in the program in which they are defined.

Constructors :
These are special type of methods.
Constructor method is executed automatically whenever a object is created or instantiated.
These methods are mainly used to set default values in a class.
constructor method is called for each instance is created.

Instance Constructor : The name of the instance constructor method is 'constructor'.
These methods have only importing parameters.
There are no exporting parameters.

Static Constructor : this method is executed automatically whenever a first call to the class is made.
The static constructors will not have any importing or exporting parameters.
Name of the static constructor is 'class_constructor'.

FINAL and FRIENDS:

FINAL: It is a keyword which specifies whether inheritance is possible or not.
If FINAL option is selected, we cannot create inheritance (or) the class cannot be inherited.
If ‘FINAL’ option is not selected, we can create inheritance.

FRIENDS: By default , the child class cannot access the private variables of a SUPER class.
It can access only PUBLIC&PROTECTED variables.
FRIEND’ is a concept which is used to access the PRIVATE variables of a SUPER class.
So, To access the PRIVATE variables of a super class, the SUPER class has to treat the child as a friend.

OOP's Concepts :

There are 4 OOP's concepts.

1)Inheritance : In OO ABAP we use an existing class to derive a new class (child class). The new class contains the attributes of the parent class (derives according to the visibility of attributes and methods) in addition to new attributes and methods added to it. The child class derives all the attributes and methods declared in parent class as public visibility. The child class can not inherit private members. The protected members in parent class are derived in child class but their visibility changes to private.

2)Abstraction : It is a class which contains methods with implementation as well as methods without implementation.

we cannot create an object to the abstract class instead create an object to the child class and call the methods(Hiding the main class).

3)Encapsulation : Through encapsulation we restrict the visibility of attributes and methods in the object.

4) Polymorphism : It is a concept by which the same method names will behave differently in different classes i.e each method will have its own implementation in different different classes but with the same name.