Google

OOPS IN ABAP

(Advanced Business Application Programming) is a programming language for developing applications for the SAP R/3 system, a widely-installed business application subsystem. The latest version, ABAP Objects, is object-oriented programming. SAP will run applications written using ABAP/4, the earlier ABAP version, as well as applications using ABAP Objects. SAP's original business model for R/3 was developed before the idea of an object-oriented model was widespread. The transition to the object-oriented model reflects an increased customer demand for it. ABAP Objects uses a single inheritance model and full support for object features such as encapsulation, polymorphism, and persistence.

ABAP OBJECTS- OOPS

Object Orientation
A programming technique in which solutions reflect real world objects.Object-oriented techniques have been used exclusively in system design. The ABAP language did not support these techniques earlier.The Object Orientation (OO), also known as the object-oriented paradigm, is a programming model that unites data and functions in objects. Moreover, we should know that the object-oriented enhancement of ABAP is based on the models of Java and C++. It is compatible with external object interfaces such as DCOM and CORBA. The implementation of object-oriented elements in the kernel of the ABAP language has considerably increased response times when you work with ABAP Objects. Some other objects, such as SAP Business Objects and GUI objects, which are already object-oriented by themselves, are also benefiting from being incorporated into ABAP Objects.
What are objects?
An object is an instantiation of a class. E.g. If “Animal” is a class, A cat can be an object of that class. With respect to code, Object refers to a set of services (methods/attributes) and can contain data.
Advantages of Object Orientated approach

  1. Easier to understand when the system is complex
  2. Easy to make changes

what are classes?

A class defines the properties of an object. A class can be instantiated as many number of times.
The classes are templates for objects. An abstract description of an object is the class. You could say it is a set of instructions for building an object. The attributes of objects are defined by the components of the class, which describe the state and behavior of objects.

Classes in abap

Classes in ABAP are either local or global

  1. Global classes are declared in class builder (SE24 )
  2. Local classes are declared within programs
Components of a class
Attributes : The attributes are internal data fields that can have any ABAP data type within a class. The state of an object is determined by the contents of its attributes. One kind of attribute is the reference variable. The reference variables are also very important as they allow you to create and address objects. Reference variables can be defined in classes, which allow you to access objects from within a class.
Attributes can be either instance attributes – specific to each instance of the class ( object ) or static attributes which are common to all instances
Instance Attributes:
The instance-specific state of an object is defined by the contents of instance attributes. They can be declared by using the DATA statement.
Static Attributes:
The contents of static attributes define the state of the class that is valid for all instances of the class. The static attributes exist once for each class. It is declared using the CLASSDATA statement. They are accessible for the entire runtime of the class. All of the objects in a class can access its static attributes. If you change a static attribute in an object, then the change is visible in all other objects of the class.

Methods :The methods are internal procedures that define the behavior of an object in a class. They can access all the attributes of a class. This allows them to change the data content of an object. They also have a "parameter interface," whose role is to help the users by supplying them with values when calling them and receiving values back from them. The private attributes of a class can only be changed by methods in the same class. The definition and parameter interface of a method is similar to that of function modules.
Methods can also be instance methods or static methods
Instance Method
You can declare instance methods by using the METHODS statement. They play a very important role as they can access all of the attributes of a class and can trigger all of the events of the class.
Static Methods
You can declare static methods by using the CLASS-METHODS statement. They are important and can only access static attributes and trigger static events.
Special Methods
Apart from normal methods, there are two special methods which you call using CALL METHOD. These are called CONSTRUCTOR and CLASS _CONSTRUCTOR. They are automatically called when you create an object (Constructor) or when you first access the components of a class (CLASS _CONSTRUCTOR).

Events
Objects or classes can use events to trigger event handler methods in other objects or classes. One method can be called by any number of users in a normal method call. When an event is triggered, any number of event handler methods can be called. Until runtime, the link between the trigger and the handler is not established. The calling program determines the methods that it wants to call in a normal Method call and it must exist. With events, the handler determines the events to which it wants to react. It is not necessary that for every event there has to be a handler method registered.With the help of RAISE EVENT statement, the events of a class can be triggered in the methods of the same class. You can declare a method of the same or a different class as an event handler method for the event of class by using the addition FOR EVENT OF . Events have a similar parameter interface to methods, but only have output parameters. These parameters are passed by the trigger (RAISE EVENT statement) to the event handler method, which receives them as input parameters.Using the SET HANDLER statement, the link between trigger and handler is established dynamically in a program. The trigger and handlers can be objects or classes, depending on whether you have instance or static events and event handler methods. When an event is triggered, the corresponding event handler methods are executed in all registered handling classes.

Encapsulation:
Encapsulation is obtained through the restriction in visibility of attributes / methods attained through the definition of Public, Private and Protected section of a class.
You should take great care in designing the public components and try to declare as few public components as possible when you define a class. Once you have released the class, the public components of global classes may not be changed.Public attributes are visible externally and form a part of the interface between an object and its users. If you want to encapsulate the state of an object fully, then you cannot declare any public attributes. Apart from defining the visibility of an attribute, you can also protect it from changes using the READ-ONLY addition.
Public Section
All of the components declared in the public section are accessible to all users of the class, and to the methods of the class and any classes that inherit from it. The public components of the class form the interface between the class and its users.
Protected Section
All of the components declared in the protected section are accessible to all methods of the class and of classes that inherit from it.
Private Section
Components that you declare in the private section are only visible in the methods of the same class.

Inheritance:Inheritance allows you to derive a class based on an already existing class.
It is done by using the INHERITING FROM addition in the statement:CLASS DEFINITION INHERITING FROM All of the components of the existing class are inherited by the new class . The new class is called the subclass of the class from which it is derived. The original class is called the superclass of the new class. It contains the same components as the superclass if you do not add any new declarations to the subclass. However, in the subclass only the public and protected components of the superclass are visible. The private components of the superclass are not visible though they exist in the subclass. You can declare private components in a subclass that have the same names as private components of the superclass. It is seen that each class works with its own private components. The another point that we note is that methods which a subclass inherits from a superclass use the private attributes of the superclass and not any private components of the subclass with the same names.The subclass is an exact replica of the superclass if the superclass does not have any private visibility section. However, you can add a new component to the subclass because it allows you to turn the subclass into a specialized version of the superclass. If a subclass is itself the superclass of further classes, then you can introduce a new level of specialization.

CLASS DEFINITION INHERITING FROM .
…..
ENDCLASS.
CLASS IMPLEMENTATION.
……
ENDCLASS.

All attributes / methods of super class become the property of the subclass too. Only public and protected attributes / methods are visible in the subclass.

Polymorphism: Identically named methods behave differently in different classes. Polymorphism and inheritance lead to code reuse. when methods with same name perform differently under different circumstances we call it polymorphism.

  • Methods redefined in a subclass is an example for Polymorphism.

Reference variables are defined with reference to a superclass or an interface defined with reference to it can also contain references to any of its subclasses. A reference variable defined with reference to a superclass or an interface implemented by a superclass can contain references to instances of any of its subclasses, since subclasses contain all of the components of all of their superclasses and also convey that the interfaces of methods cannot be changed. In particular, you can define the target variable with reference to the generic class OBJECT.Using the CREATE OBJECT statement, when you create an object and a reference variable typed with reference to a subclass then you can use the TY PE addition to which the reference in the reference variable will point.A reference variable can be used by a static user to address the components visible to it in the superclass to which the reference variable refers. However, any specialization implemented in the subclass cannot be addressed by it.Depending on the position in the inheritance tree at which the referenced object occurs, you can use a single reference variable to call different implementations of the method. This is possible only if you redefine an instance method in one or more subclasses. This concept is called polymorphism, in which different classes can have the same interface and, therefore, be addressed using reference variables with a single type.

Interfaces:
Interfaces are used to define the model of a class.
They also like classes can be either local or global.
Global interfaces are defined through SE24 and local interfaces are defined in program.