1.95k likes | 4.06k Views
PART 1 Basics of OO ABAP. Background ? HistoryObject ? Oriented ConceptsClassesObject VisibilityMethodsConstructorCreating and referencing objectsAccessing object componentsFrom Function Groups to ObjectsDefining and calling MethodsParameter interfaceMethod CallFunctional methodsDynamic
E N D
1. OO ABAP This presentation will give an over view on Object Oriented Concept and how ABAP uses the object oriented concept. This presentation will give an over view on Object Oriented Concept and how ABAP uses the object oriented concept.
2. PART 1Basics of OO ABAP Background History
Object Oriented Concepts
Classes
Object Visibility
Methods
Constructor
Creating and referencing objects
Accessing object components
From Function Groups to Objects
Defining and calling Methods
Parameter interface
Method Call
Functional methods
Dynamic method call
Instance and Static Constructors
3. ABAP Objects was introduced with SAP Basis, Release 4.5.
Classes
Interfaces
Events
ABAP Objects was completed with SAP Basis Release 4.6.
Inheritance
Compound interfaces
Dynamic Invoke
Some enhancements were added with SAP Web Application Server, Releases 6.10, 6.20, ...
Friends
Object Services
Shared Objects Background History
4. Object Oriented Concepts 1) In the past, information systems used to be defined primarily by their functionality: data and functions were kept separate and linked together by means of input and output relations.
2) The object-oriented approach, however, focuses on objects that represent abstract or concrete things of the real world. These objects are first defined by their character and their properties which are represented by their internal structure and their attributes (data). The behavior of these objects is described by methods (functionality). Objects form a capsule which combines the character to the respective behavior. 1) In the past, information systems used to be defined primarily by their functionality: data and functions were kept separate and linked together by means of input and output relations.
2) The object-oriented approach, however, focuses on objects that represent abstract or concrete things of the real world. These objects are first defined by their character and their properties which are represented by their internal structure and their attributes (data). The behavior of these objects is described by methods (functionality). Objects form a capsule which combines the character to the respective behavior.
5. Abstraction
This refers to the ability to reflect real-world process as realistically in the programming language as possible. These processes can be of a business or a technical nature.
Encapsulation
Objects restrict the visibility of their resources (attributes and methods) to other users. Every object has an interface, which determines how other objects can interact with it. Interface ensures that the abstract representation of an object is used only in accordance with its specification.
Inheritance
You can use an existing class to derive a new class. Derived classes inherit the data and methods of the superclass. However, they can overwrite existing methods, and also add new ones.
Polymorphism
Identical (identically-named) methods behave differently in different classes. In ABAP Objects, polymorphism is implemented by redefining methods during inheritance and by using constructs called interfaces.
Advantages of object-oriented programming:
Complex software systems become easier to understand, since object-oriented structuring provides a closer representation of reality than other programming techniques.
In a well-designed object-oriented system, it should be possible to implement changes at class level, without having to make alterations at other points in the system. This reduces the overall amount of maintenance required.
Through polymorphism and inheritance, object-oriented programming allows you to reuse individual components.
In an object-oriented system, the amount of work involved in revising and maintaining the system is reduced, since many problems can be detected and corrected in the design phase.Abstraction
This refers to the ability to reflect real-world process as realistically in the programming language as possible. These processes can be of a business or a technical nature.
Encapsulation
Objects restrict the visibility of their resources (attributes and methods) to other users. Every object has an interface, which determines how other objects can interact with it. Interface ensures that the abstract representation of an object is used only in accordance with its specification.
Inheritance
You can use an existing class to derive a new class. Derived classes inherit the data and methods of the superclass. However, they can overwrite existing methods, and also add new ones.
Polymorphism
Identical (identically-named) methods behave differently in different classes. In ABAP Objects, polymorphism is implemented by redefining methods during inheritance and by using constructs called interfaces.
Advantages of object-oriented programming:
Complex software systems become easier to understand, since object-oriented structuring provides a closer representation of reality than other programming techniques.
In a well-designed object-oriented system, it should be possible to implement changes at class level, without having to make alterations at other points in the system. This reduces the overall amount of maintenance required.
Through polymorphism and inheritance, object-oriented programming allows you to reuse individual components.
In an object-oriented system, the amount of work involved in revising and maintaining the system is reduced, since many problems can be detected and corrected in the design phase.
6. Classes Classes are the central element of object-orientation.
A Class is an abstract description of an object.
Classes are templates for objects.
The attributes of objects are defined by the components of the class, which describe the state and behavior of objects.
You define global classes and interfaces in the Class Builder (Transaction SE24) in the ABAP Workbench.
They are stored centrally in class pools in the class library in the R/3 Repository.
All of the ABAP programs in an R/3 System can access the global classes.
Local classes are defined within an ABAP program.
Local classes and interfaces can only be used in the program in which they are defined. Classes form the basis of any object oriented programming language. A class is the model or template for an object in the same way that a data type is the model for a data object. We can say that a classes is an object type. In order to work with objects in a program first define the class. Various objects can be created on the basis of the class, and their attributes can have the most diverse features.
Local classes can be created within any given ABAP program and are only visible there. Global classes can be created using the Class Builder tool of the ABAP workbench in the Class Library and are visible in any ABAP Program. Apart from the visibility, there is no difference between using a global class and using a local class.
When we use a class in a program, the system first searches in local class with the specified name. If it does not find one, then looks for a global class. Classes form the basis of any object oriented programming language. A class is the model or template for an object in the same way that a data type is the model for a data object. We can say that a classes is an object type. In order to work with objects in a program first define the class. Various objects can be created on the basis of the class, and their attributes can have the most diverse features.
Local classes can be created within any given ABAP program and are only visible there. Global classes can be created using the Class Builder tool of the ABAP workbench in the Class Library and are visible in any ABAP Program. Apart from the visibility, there is no difference between using a global class and using a local class.
When we use a class in a program, the system first searches in local class with the specified name. If it does not find one, then looks for a global class.
7. Classes Local classes consist of ABAP source code, enclosed in the ABAP statements CLASS - ENDCLASS. A complete class definition consists of a declaration part and an implementation part.
The declaration part contains the declaration for all components (attributes, methods, events) of the class. When you define local classes, the declaration part belongs to the global program data. You should therefore place it at the beginning of the program.
If you declare methods in the declaration part of a class, you must also write an implementation part for it. The implementation part of a class contains the implementation of all methods of the class. The implementation part of a local class is a processing block. Subsequent coding that is not itself part of a processing block is therefore not accessible. Local classes consist of ABAP source code, enclosed in the ABAP statements CLASS - ENDCLASS. A complete class definition consists of a declaration part and an implementation part.
The declaration part contains the declaration for all components (attributes, methods, events) of the class. When you define local classes, the declaration part belongs to the global program data. You should therefore place it at the beginning of the program.
If you declare methods in the declaration part of a class, you must also write an implementation part for it. The implementation part of a class contains the implementation of all methods of the class. The implementation part of a local class is a processing block. Subsequent coding that is not itself part of a processing block is therefore not accessible.
8. Structure of a Class The following statements define the structure of a class:
A class contains components
Each component is assigned to a visibility section
Classes implement methods.
The left-hand side of the illustration shows the declaration and implementation parts of a local class C1. The right-hand side illustrates the structure of the class with the components in their respective visibility areas (shown under public, protected and privat), and the implementation of the methods.
The public components of the class form the interface between the class and its users. The protected components are an interface to the subclasses of C1. The private components are not visible externally, and are fully encapsulated in the class. The methods in the implementation part have unrestricted access to all components of the class.The following statements define the structure of a class:
A class contains components
Each component is assigned to a visibility section
Classes implement methods.
The left-hand side of the illustration shows the declaration and implementation parts of a local class C1. The right-hand side illustrates the structure of the class with the components in their respective visibility areas (shown under public, protected and privat), and the implementation of the methods.
The public components of the class form the interface between the class and its users. The protected components are an interface to the subclasses of C1. The private components are not visible externally, and are fully encapsulated in the class. The methods in the implementation part have unrestricted access to all components of the class.
9. Object Visibility Public
All the components which are assigned to this section are public and can be addresses by all the users, methods of subclass, and the methods of the class itself. They form the external interface of the class.
Protected
The components of this section are protected and can be addressed by the methods of subclass and the methods of the class itself. They form the interface of the class to its subclass. Since inheritance is not active in Release 4.5B, the protected section currently has the same effect as the private section.
Private
Components of this section are private and can only be used in the methods on of the class itself. The private components are not part of the external interface of the class.
Public
All the components which are assigned to this section are public and can be addresses by all the users, methods of subclass, and the methods of the class itself. They form the external interface of the class.
Protected
The components of this section are protected and can be addressed by the methods of subclass and the methods of the class itself. They form the interface of the class to its subclass. Since inheritance is not active in Release 4.5B, the protected section currently has the same effect as the private section.
Private
Components of this section are private and can only be used in the methods on of the class itself. The private components are not part of the external interface of the class.
10. Attributes are the data objects within the class.
Reflects Objects State.
Instance Attributes
One per instance
Statement: DATA
Static Attributes
Only one per class
Statement: CLASS-DATA
Also known as class attributes
CONSTANTS statement is used to declare constants.
Self-defined types are declared with the TYPE statement.
Attributes are the data objects within a class. They reflect an objects state. As in classical ABAP, all data types of the ABAP hierarchy can be used for attributes. One kind of attribute is the reference variable. Reference variables allow you to create and address objects. Reference variables can be defined in classes, allowing you to access objects from within a class. An attribute can be an Instance attribute or an class-attribute (Static).
Instance Attributes
The contents of instance attributes define the instance-specific state of an object. You declare them using the DATA statement. Instance attributes are accessed using the object component selector ->.
Static Attributes
The contents of static attributes define the state of the class that is valid for all instances of the class. Static attributes exist once for each class. You declare them using the CLASS-DATA 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, the change is visible in all other objects in the class.
We use Class Component selector => to access a static component of the class. The label class of a class must be to the left of the class component selector.
The name comp of the component must be to the right of the object component selector. The class component selector can also be used to access the data types and constants of an interface.
A READ-ONLY addition can be used in the public section of the class. The effect of this addition is that an attribute declared with DATA can be externally read but only modified by the methods of the class.
Beside the data objects declared with DATA, we can also declare constants with the CONSTANTS statement. As constants must not be modified during compile time or at runtime, they are independent of instances and therefore comparable to static attributes.
Self-defined types within a class can be declared with the TYPES statement. Like, constants, data types are independent of instances. Attributes are the data objects within a class. They reflect an objects state. As in classical ABAP, all data types of the ABAP hierarchy can be used for attributes. One kind of attribute is the reference variable. Reference variables allow you to create and address objects. Reference variables can be defined in classes, allowing you to access objects from within a class. An attribute can be an Instance attribute or an class-attribute (Static).
Instance Attributes
The contents of instance attributes define the instance-specific state of an object. You declare them using the DATA statement. Instance attributes are accessed using the object component selector ->.
Static Attributes
The contents of static attributes define the state of the class that is valid for all instances of the class. Static attributes exist once for each class. You declare them using the CLASS-DATA 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, the change is visible in all other objects in the class.
We use Class Component selector => to access a static component of the class. The label class of a class must be to the left of the class component selector.
The name comp of the component must be to the right of the object component selector. The class component selector can also be used to access the data types and constants of an interface.
A READ-ONLY addition can be used in the public section of the class. The effect of this addition is that an attribute declared with DATA can be externally read but only modified by the methods of the class.
Beside the data objects declared with DATA, we can also declare constants with the CONSTANTS statement. As constants must not be modified during compile time or at runtime, they are independent of instances and therefore comparable to static attributes.
Self-defined types within a class can be declared with the TYPES statement. Like, constants, data types are independent of instances.
11. Methods
The way in which object behave is implemented in the methods of the class. They can access all of the attributes of a class. This allows them to change the data content of an object. They also have a parameter interface, with which users can supply them with values when calling them, and receive 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. You define a method in the definition part of a class and implement it in the implementation part .
We can relate IMPORTING, EXPORTING, CHANGING and EXCEPTION respectively to the IMPORTING, EXPORTING, CHANGING AND EXCEPTIONS of the function module.
You can declare local data types and objects in methods in the same way as in other ABAP procedures (subroutines and function modules). You call methods using the CALL METHOD statement.
Instance Methods
You declare instance methods using the METHODS statement. They can access all of the attributes of a class, and can trigger all of the events of the class.
Static Methods
You declare static methods using the CLASS-METHODS statement. You can access static components using the class name as well as the reference variable. It is also possible to address the static components of a class before an object has been created. Methods
The way in which object behave is implemented in the methods of the class. They can access all of the attributes of a class. This allows them to change the data content of an object. They also have a parameter interface, with which users can supply them with values when calling them, and receive 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. You define a method in the definition part of a class and implement it in the implementation part .
We can relate IMPORTING, EXPORTING, CHANGING and EXCEPTION respectively to the IMPORTING, EXPORTING, CHANGING AND EXCEPTIONS of the function module.
You can declare local data types and objects in methods in the same way as in other ABAP procedures (subroutines and function modules). You call methods using the CALL METHOD statement.
Instance Methods
You declare instance methods using the METHODS statement. They can access all of the attributes of a class, and can trigger all of the events of the class.
Static Methods
You declare static methods using the CLASS-METHODS statement. You can access static components using the class name as well as the reference variable. It is also possible to address the static components of a class before an object has been created.
12. In this example we used just 2 methods, one which imports parameters and the other exports the parameter. Both the methods are public methods, using these methods we are accessing the private attribute.
Methods set_obj_value is be used to set the private attribute object_value and the method get_obj_value is used to get the value of private attribute object_value.In this example we used just 2 methods, one which imports parameters and the other exports the parameter. Both the methods are public methods, using these methods we are accessing the private attribute.
Methods set_obj_value is be used to set the private attribute object_value and the method get_obj_value is used to get the value of private attribute object_value.
13. As well as normal methods, which you call using CALL METHOD, there are two special methods called constructor is implicitly called for each instantiation of a new object. Another method is class_constructor is called the first time a class is accessed.
A Constructor is a special method which is automatically executed by the runtime environment. Constructors are used to set an object dynamically to a defined initial value. A constructor cannot be called with the CALL METHOD statement, nor can the developer influence the call point.As well as normal methods, which you call using CALL METHOD, there are two special methods called constructor is implicitly called for each instantiation of a new object. Another method is class_constructor is called the first time a class is accessed.
A Constructor is a special method which is automatically executed by the runtime environment. Constructors are used to set an object dynamically to a defined initial value. A constructor cannot be called with the CALL METHOD statement, nor can the developer influence the call point.
14. Objects
Objects are instances of classes. Each object has a unique identity and its own attributes.
Object References
To access an object from an ABAP program, you use only object references. Object references is nothing more than the address of an object in the memory. In ABAP, they are always contained in object reference variables. The identity of an object depends on its object reference. The instance-dependent components of an object can only be addressed using reference variables that point to the object. A reference variable is either initial or points to a single object. It is possible for several reference variables to point to same object.
Data Types for References
The data type of an object reference variable determines how the program handles its value (that is, the object reference). You define class references using the DATA: <object> TYPE REF TO <class>.
Creating Objects
Once the Reference variable is defined, we can create an instance of the class using
CREATE OBJECT cref [TYPE class].
You do not need the TYPE class addition in this case. This addition is only important in the following two situations:
if you use inheritance and want to create an instance of a class <class> with a class reference variable cref which does not have the type of the class <class>, or
if you use interface and want to create an instance of a class <class> with an interface reference variable iref.
As with other variables, assignment can be made between reference variables using = or MOVE statement. However, the object exists once in the memory, and only the additional reference occupies memory space for the size that the reference takes up. We can equally use any reference variables which is pointing to an object to work with that object.Objects
Objects are instances of classes. Each object has a unique identity and its own attributes.
Object References
To access an object from an ABAP program, you use only object references. Object references is nothing more than the address of an object in the memory. In ABAP, they are always contained in object reference variables. The identity of an object depends on its object reference. The instance-dependent components of an object can only be addressed using reference variables that point to the object. A reference variable is either initial or points to a single object. It is possible for several reference variables to point to same object.
Data Types for References
The data type of an object reference variable determines how the program handles its value (that is, the object reference). You define class references using the DATA: <object> TYPE REF TO <class>.
Creating Objects
Once the Reference variable is defined, we can create an instance of the class using
CREATE OBJECT cref [TYPE class].
You do not need the TYPE class addition in this case. This addition is only important in the following two situations:
if you use inheritance and want to create an instance of a class <class> with a class reference variable cref which does not have the type of the class <class>, or
if you use interface and want to create an instance of a class <class> with an interface reference variable iref.
As with other variables, assignment can be made between reference variables using = or MOVE statement. However, the object exists once in the memory, and only the additional reference occupies memory space for the size that the reference takes up. We can equally use any reference variables which is pointing to an object to work with that object.
15. Output: In this example we created a local class called lcl_increment. This class has 2 methods, incr_cntr which increments the counter value and get_cntr which returns the counter value at that particular instant, and a constructor which sets the initial value of the counter when the object is initiated. We are creating 2 object ctr1 and ctr2 which refers to lcl_increment class. In this example we created a local class called lcl_increment. This class has 2 methods, incr_cntr which increments the counter value and get_cntr which returns the counter value at that particular instant, and a constructor which sets the initial value of the counter when the object is initiated. We are creating 2 object ctr1 and ctr2 which refers to lcl_increment class.
16. Object Component Selector
To access an object component we use operator ->
oref->comp
Class Component Selector
To access static components without creating object, we use operator =>
Class=>comp
Self-reference
If an object wants to access its own reference then use me
Working with objects is governed by the following process:
Declare reference variable
Create objects
use object components.
Component Selector
To access an object component, be it an attribute or a method, we always use the operator ->.
The syntax is oref->comp. In this case oref represents a reference variable, and comp is the name of the component to be addressed. The object component selector (->) allows access to the instance components and the static components of an object.
It is possible to access the static component of a class without creating an object, i.e. without an instance. In this case the class component selector => is used. The Syntax is class=>comp in this case class name class is used.
Self-Reference
Within a class a component can be addressed simply by its name, and it is not necessary to specify a reference variable. In some cases, however, it can be necessary for an object to know its own address, i.e. its own reference. The self-reference me variable allows attributes of its own class to be addressed.
Working with objects is governed by the following process:
Declare reference variable
Create objects
use object components.
Component Selector
To access an object component, be it an attribute or a method, we always use the operator ->.
The syntax is oref->comp. In this case oref represents a reference variable, and comp is the name of the component to be addressed. The object component selector (->) allows access to the instance components and the static components of an object.
It is possible to access the static component of a class without creating an object, i.e. without an instance. In this case the class component selector => is used. The Syntax is class=>comp in this case class name class is used.
Self-Reference
Within a class a component can be addressed simply by its name, and it is not necessary to specify a reference variable. In some cases, however, it can be necessary for an object to know its own address, i.e. its own reference. The self-reference me variable allows attributes of its own class to be addressed.
17. Output:
The program creates an object of the client class and calls its create_server method. The create_server method creates an object of the server class, then it calls acknowledge method in the object method in the object created and passes its self-reference to the creator parameter. The acknowledge method accesses the public attribute name of the client class through he reference passed and transfers its contents to its contents to its local variable name. In the WRITE statement the method then uses its self-reference to access the private attribute name of its own class which is hidden by the local variable.The program creates an object of the client class and calls its create_server method. The create_server method creates an object of the server class, then it calls acknowledge method in the object method in the object created and passes its self-reference to the creator parameter. The acknowledge method accesses the public attribute name of the client class through he reference passed and transfers its contents to its contents to its local variable name. In the WRITE statement the method then uses its self-reference to access the private attribute name of its own class which is hidden by the local variable.
18. Function Group
program can work with the instances of several function groups at the same time
cannot work with several instances of a single function group.
Objects
work with any number of instances (objects) based on the same template.
At the center of any object-oriented model are objects, which contain attributes (data) and methods (functions). Objects should enable programmers to map a real problem and its proposed software solution on a one-to-one basis. Typical objects in a business environment are, for example, Customer, Order, or Invoice.
Before R/3 Release 4.0, the nearest equivalent of objects in ABAP were function modules and function groups. Suppose we have a function group for processing orders. The attributes of an order correspond to the global data of the function group, while the individual function modules represent actions that manipulate that data (methods). This means that the actual order data is encapsulated in the function group, and is never directly addressed, but instead only through the function modules. In this way, the function modules can ensure that the data is consistent.
When you run an ABAP program, the system starts a new internal session. The internal session has a memory area that contains the ABAP program and its associated data. When you call a function module, an instance of its function group plus its data, is loaded into the memory area of the internal session. An ABAP program can load several instances by calling function modules from different function groups.
Suppose a program wanted to use several independent counters, or process several orders at the same time. In this case, you would have to adapt the function group to include instance administration, for example, by using numbers to differentiate between the instances. In practice, this is very awkward.
The main difference between real object orientation and function groups is that although a program can work with the instances of several function groups at the same time, it cannot work with several instances of a single function group. Suppose a program wanted to use several independent counters, or process several orders at the same time. In this case, you would have to adapt the function group to include instance administration, for example, by using numbers to differentiate between the instances. In practice, this is very awkward. Consequently, the data is usually stored in the calling program, and the function modules are called to work with it (structured programming).
ABAP Objects allows you to define data and functions in classes instead of function groups. Using classes, an ABAP program can work with any number of instances (objects) based on the same template. Instead of loading a single instance of a function group into memory implicitly when a function module is called, the ABAP program can now generate the instances of classes explicitly using the new ABAP statement CREATE OBJECT. The individual instances represent unique objects. You address these using object references. The object references allow the ABAP program to access the interfaces of the instaces.
At the center of any object-oriented model are objects, which contain attributes (data) and methods (functions). Objects should enable programmers to map a real problem and its proposed software solution on a one-to-one basis. Typical objects in a business environment are, for example, Customer, Order, or Invoice.
Before R/3 Release 4.0, the nearest equivalent of objects in ABAP were function modules and function groups. Suppose we have a function group for processing orders. The attributes of an order correspond to the global data of the function group, while the individual function modules represent actions that manipulate that data (methods). This means that the actual order data is encapsulated in the function group, and is never directly addressed, but instead only through the function modules. In this way, the function modules can ensure that the data is consistent.
When you run an ABAP program, the system starts a new internal session. The internal session has a memory area that contains the ABAP program and its associated data. When you call a function module, an instance of its function group plus its data, is loaded into the memory area of the internal session. An ABAP program can load several instances by calling function modules from different function groups.
Suppose a program wanted to use several independent counters, or process several orders at the same time. In this case, you would have to adapt the function group to include instance administration, for example, by using numbers to differentiate between the instances. In practice, this is very awkward.
The main difference between real object orientation and function groups is that although a program can work with the instances of several function groups at the same time, it cannot work with several instances of a single function group. Suppose a program wanted to use several independent counters, or process several orders at the same time. In this case, you would have to adapt the function group to include instance administration, for example, by using numbers to differentiate between the instances. In practice, this is very awkward. Consequently, the data is usually stored in the calling program, and the function modules are called to work with it (structured programming).
ABAP Objects allows you to define data and functions in classes instead of function groups. Using classes, an ABAP program can work with any number of instances (objects) based on the same template. Instead of loading a single instance of a function group into memory implicitly when a function module is called, the ABAP program can now generate the instances of classes explicitly using the new ABAP statement CREATE OBJECT. The individual instances represent unique objects. You address these using object references. The object references allow the ABAP program to access the interfaces of the instaces.
19.
Formal parameters
IMPORTING to specify one or more input parameters
EXPORTING to specify one are more output parameters
CHANGING to specify one or more parameters which are both input and output parameters.
Determine the attribute
Type of the parameter passing (pass by value and pass by reference)
Typing
Optional parameters/initial value The parameter interface of a method is defines by the addition to the METHODS statement in the declaration part of the class. The implementation part does not requires an details for the parameter interface (Shown in fig). The structure of the interface is similar to that of the function module. Compared with function modules, however, methods do not have a table parameter as this has become obsolete due to current typing options.
Formal Parameters
We can create the following formal parameters with the additions to METHODS statement:
Use IMPORTING to specify one or more input parameters
Use EXPORTING to specify one or more output parameters
Use Changing to specify one or more parameters which are both input and output parameters.
We can use other additions to determine the attributes of each formal parameters.
Type of parameter passing
The type of parameter passing determines whether a parameter is passed by reference or by value. Pass by reference is the default. We can use the optional addition VALUE to specify pass by value. Another option that can also be used is Explicit pass by reference using REFERENCE addition instead of VALUE.
Output parameters are not necessarily initial at the start of the method.
Changes to output parameters and input/output parameters are effective, even if the method terminates with an exception.
Input parameters that are passed by reference cannot be explicitly changed in the method. Their values may change, however, if they are linked to global actual parameters and if these parameters are changed during the method is executed.
Typing
We must type each parameter of a method wit the TYPE addition. Instead of TYPE we can use LIKE addition with which we can refer to the data type of data object visible at this point, such as class attribute already declared.
Optional parameters/initial value
The parameters marked with OPTIONAL addition do not need to be filled when the method is called. An optional parameter which is not filled during the call contains either the type-specific initial value or we determine an initial value with the DEFAULT addition.The parameter interface of a method is defines by the addition to the METHODS statement in the declaration part of the class. The implementation part does not requires an details for the parameter interface (Shown in fig). The structure of the interface is similar to that of the function module. Compared with function modules, however, methods do not have a table parameter as this has become obsolete due to current typing options.
Formal Parameters
We can create the following formal parameters with the additions to METHODS statement:
Use IMPORTING to specify one or more input parameters
Use EXPORTING to specify one or more output parameters
Use Changing to specify one or more parameters which are both input and output parameters.
We can use other additions to determine the attributes of each formal parameters.
Type of parameter passing
The type of parameter passing determines whether a parameter is passed by reference or by value. Pass by reference is the default. We can use the optional addition VALUE to specify pass by value. Another option that can also be used is Explicit pass by reference using REFERENCE addition instead of VALUE.
Output parameters are not necessarily initial at the start of the method.
Changes to output parameters and input/output parameters are effective, even if the method terminates with an exception.
Input parameters that are passed by reference cannot be explicitly changed in the method. Their values may change, however, if they are linked to global actual parameters and if these parameters are changed during the method is executed.
Typing
We must type each parameter of a method wit the TYPE addition. Instead of TYPE we can use LIKE addition with which we can refer to the data type of data object visible at this point, such as class attribute already declared.
Optional parameters/initial value
The parameters marked with OPTIONAL addition do not need to be filled when the method is called. An optional parameter which is not filled during the call contains either the type-specific initial value or we determine an initial value with the DEFAULT addition.
21. All methods can be called with he CALL METHOD statement. Each time the method is called, all the non-optional formal parameters of the method must be filled with type-specific actual parameters.
The assignment of formal parameters and actual parameters always follows paradigm:
Formal parameter = Actual parameter.
The equals sign should not be seen as an assignment operator, it indicates the relationship between the formal parameter and actual parameterThere is no difference in how the exceptional situations in a method are handled with the EXCEPTION addition compared with function module. The exceptions are triggered in the method by the RISE exception and MESSAGE RAISING statements. If an exception occurs within a method, the sy-subrc system field is filled with a value r1 defined during the call and can be accessed by the user of the method.
If a method has no, one, or more input parameters but not output parameters then we can use the short form of CALL METHOD statement.All methods can be called with he CALL METHOD statement. Each time the method is called, all the non-optional formal parameters of the method must be filled with type-specific actual parameters.
The assignment of formal parameters and actual parameters always follows paradigm:
Formal parameter = Actual parameter.
The equals sign should not be seen as an assignment operator, it indicates the relationship between the formal parameter and actual parameter
22. Declaration:
METHODS meth
IMPORTING .. {VALUE(i1)|i1} {TYPE type| LIKE dobj}
[OPTIONAL| INITIAL def1]
RETURNING VALUE(r) {TYPE type| LIKE dobj}.
Implementation:
CALL METHOD oref->meth
EXPORTING i1 = a1 ... in = an
RECIEVING r = a
Short Form
meth( ).
meth( f ).
meth( f1 = a1 fn = an ). In many other programming languages it is possible to perform function calls through expressions in operations instead of using special call statement. In ABAP Objects we can use functional method calls in all key statements.
A functional method can have any number of IMPORTING parameters and just one RETURNING parameter. This return value must be passed by value and be fully typed. If we use RETURNING, we cannot use EXPORTING or CHANGING parameters. The RECEIVING addition assigns the RETURNING parameter to an actual parameters. However, the functional method call is much more interesting with the short forms.
When statements with functional methods are executed, first the methods are called and then the RETURNING parameters are used directly as operands.In many other programming languages it is possible to perform function calls through expressions in operations instead of using special call statement. In ABAP Objects we can use functional method calls in all key statements.
A functional method can have any number of IMPORTING parameters and just one RETURNING parameter. This return value must be passed by value and be fully typed. If we use RETURNING, we cannot use EXPORTING or CHANGING parameters. The RECEIVING addition assigns the RETURNING parameter to an actual parameters. However, the functional method call is much more interesting with the short forms.
When statements with functional methods are executed, first the methods are called and then the RETURNING parameters are used directly as operands.
23. In this Example we used a functional method, get area, to calculate the area of a circle. We call this method once via CALL METHOD and once functionally.In this Example we used a functional method, get area, to calculate the area of a circle. We call this method once via CALL METHOD and once functionally.
24. Calling an instance method meth:
CALL METHOD ref->(f)
Calling a static method meth:
CALL METHOD class=>(f)CALL METHOD (c)=>methCALL METHOD (c)=>(f)
Calling a user-defined method meth:
CALL METHOD (f)CALL METHOD ME->(f) If, when creating a program, we do not know the name of the method which we call at a specific point? To, make such call to a method we go for Dynamic method call.
A dynamic method call can be achieved by inserting a filed in brackets containing the name of a method, instead of the method itself. This applies to instance methods, methods of its own class, and static methods. With static methods class name can also be dynamically specified via (C)=>. If we try to dynamically call a non-existent method, a catchable runtime error is triggered which we catch in CATCH ENDCATCH block.If, when creating a program, we do not know the name of the method which we call at a specific point? To, make such call to a method we go for Dynamic method call.
A dynamic method call can be achieved by inserting a filed in brackets containing the name of a method, instead of the method itself. This applies to instance methods, methods of its own class, and static methods. With static methods class name can also be dynamically specified via (C)=>. If we try to dynamically call a non-existent method, a catchable runtime error is triggered which we catch in CATCH ENDCATCH block.
26.
Instance Constructor
called once for each instance of a class.
must have predefined name constructor
must be declared in PUBLIC SECTION
Static Constructor
called in a program once for each class
called before the class is accessed for the first time
must be declared in PUBLIC SECTION
no interface parameter Instance Constructor
Instance constructors are called once per instance of the class after the complete creation on the instance with the CREATE OBJECT statement. Like conventional methods, the instance must be declared in the declaration part of the class and implemented in the implementation part of the class. Two rules apply here. Firstly, the method must have the predefined name constructor and secondly it must be declared in the PUBLIC SECTION.
Since the constructor is called implicitly during CREATE OBJECT statement, the formal parameters must be filled during this statement. The CRATE OBJECT statement therefore has the same EXPORTING IMPORTING additions as the CALL METHOD statement.
If an exception is triggered in constructor, the object created is deleted during the CREATE OBJECT statement, and corresponding reference variable is set to its initial value.
Static Constructor
The static constructor is called in a program for each class before the class is accessed for the first time. Like the instance constructor, the static constructor must also be declared in the PUBLIC SECTION. The name of the static constructor with which it is declared and implemented is class-constructor.
No interface parameters can be defined for the static constructor, like all static methods, static constructor can only access static attributes of their class.Instance Constructor
Instance constructors are called once per instance of the class after the complete creation on the instance with the CREATE OBJECT statement. Like conventional methods, the instance must be declared in the declaration part of the class and implemented in the implementation part of the class. Two rules apply here. Firstly, the method must have the predefined name constructor and secondly it must be declared in the PUBLIC SECTION.
Since the constructor is called implicitly during CREATE OBJECT statement, the formal parameters must be filled during this statement. The CRATE OBJECT statement therefore has the same EXPORTING IMPORTING additions as the CALL METHOD statement.
If an exception is triggered in constructor, the object created is deleted during the CREATE OBJECT statement, and corresponding reference variable is set to its initial value.
Static Constructor
The static constructor is called in a program for each class before the class is accessed for the first time. Like the instance constructor, the static constructor must also be declared in the PUBLIC SECTION. The name of the static constructor with which it is declared and implemented is class-constructor.
No interface parameters can be defined for the static constructor, like all static methods, static constructor can only access static attributes of their class.
29. PART 2Advanced Concepts in OO ABAP Inheritance
Interfaces
Polymorphism
Event Handling
30. In object orientation, inheritance refers to the specialization ofclasses by deriving subclasses from superclasses.
Inheritance allows you to derive a new class from an existing class. The new class subclass inherits all of the components of the existing class superclass. 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.
In inheritance, a subclass adopts all the components, i.e. attributes. Methods, and events of the superclass, and use them exactly like its own components. In each subclass, new elements can be added or methods can be redefined in order to specialize, without this having any impact on the superclass. Elements can only be added in subclasses. It would go against the inheritance concept to remove elements in a subclass.
The concept of simple inheritance is implemented in ABAP Objects. According to this concept, each class can have several subclasses but only one superclass.
All classes in ABAP Objects that do not explicitly inherit from another class are implicit direct subclasses of object.In object orientation, inheritance refers to the specialization ofclasses by deriving subclasses from superclasses.
Inheritance allows you to derive a new class from an existing class. The new class subclass inherits all of the components of the existing class superclass. 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.
In inheritance, a subclass adopts all the components, i.e. attributes. Methods, and events of the superclass, and use them exactly like its own components. In each subclass, new elements can be added or methods can be redefined in order to specialize, without this having any impact on the superclass. Elements can only be added in subclasses. It would go against the inheritance concept to remove elements in a subclass.
The concept of simple inheritance is implemented in ABAP Objects. According to this concept, each class can have several subclasses but only one superclass.
All classes in ABAP Objects that do not explicitly inherit from another class are implicit direct subclasses of object.
33.
PUBLIC SECTION
Contains all public components of all superclass and its own added public components.
PROTECTED
Contains all protected components of all superclass and its own added protected components.
PRIVATE
Contains only subclass private components.
34. Inherited methods can be redefined in the subclass
Redefined methods must be re-implemented in the subclass
The signature of the redefined methods cannot be changes
Can redefine only instance methods, not static methods
Using super pseudo reference we can access the original method of the superclass The effect of the REDEFINITION addition is to reimplement the method in the implementation part of the subclass. The parameter interface of the method is not specified again and therefore always remains the same for all the subclass. A redefined method in a subclass hides the corresponding method in the subclass. When the method is accessed within the subclass, it is always the redefined method which is executed. Like the subclass own methods, a redefined method accesses the private attributes of the subclass.
Through the super pseudo reference we can access the original method of the direct superclass within a redefined method of the subclass, thus getting round the fact that the superclass method is hidden by the redefined method.The effect of the REDEFINITION addition is to reimplement the method in the implementation part of the subclass. The parameter interface of the method is not specified again and therefore always remains the same for all the subclass. A redefined method in a subclass hides the corresponding method in the subclass. When the method is accessed within the subclass, it is always the redefined method which is executed. Like the subclass own methods, a redefined method accesses the private attributes of the subclass.
Through the super pseudo reference we can access the original method of the direct superclass within a redefined method of the subclass, thus getting round the fact that the superclass method is hidden by the redefined method.
36. When an instance constructor is redefined, the REDEFINITION addition is not specified.
The parameter interface of an instance constructor is defined in a completely new way during redefinition. The signature of the parameter interface of an instance constructor can therefore look different for each class of inheritance tree.
During implementation of the redefined instance constructor the instance constructor of the superclass must be called.
A static constructor does not have to call the static constructor of its superclass explicitly. Instead the runtime environment ensures that the static constructors are called in the right order.
When an instance constructor is redefined, the REDEFINITION addition is not specified.
The parameter interface of an instance constructor is defined in a completely new way during redefinition. The signature of the parameter interface of an instance constructor can therefore look different for each class of inheritance tree.
During implementation of the redefined instance constructor the instance constructor of the superclass must be called.
A static constructor does not have to call the static constructor of its superclass explicitly. Instead the runtime environment ensures that the static constructors are called in the right order.
37. Phase1
At this stage the preparation for calling the instance constructor of the supercalss are made, and actual parameters are determined for its interface. Only local data or static data can be addressed at this stage, not instance attributes of the class. The constructor therefore behaves initially like a static method
Phase2
At this stage the instance constructor of the subclass is called with CALL METHOD super->constructor.
Phase3
Execute the statements after the instance constructor of the superclass has been called. Now we can access the instance attributes of the class .Phase1
At this stage the preparation for calling the instance constructor of the supercalss are made, and actual parameters are determined for its interface. Only local data or static data can be addressed at this stage, not instance attributes of the class. The constructor therefore behaves initially like a static method
Phase2
At this stage the instance constructor of the subclass is called with CALL METHOD super->constructor.
Phase3
Execute the statements after the instance constructor of the superclass has been called. Now we can access the instance attributes of the class .
40. If we simple wish to use class as a model for the subclass and do not require any objects from the class, we can define it as an abstract class by using ABSTRACT addition to the CLASS statement. We cannot create objects with CREATE OBJECT of a class defined in this way, and consequently we cannot access the instance components.
Methods can also b defined as abstract, to do this we use ABSTRACT addition with METHODS when we declare the method. An abstract method cannot be implemented in its own class, but only in a non-abstract subclass. This is why a which contains one or more abstract methods must be defined overall as abstract. Otherwise, we could create an object with an addressable method but without any implementation.
Both non-abstract and abstract methods can be defined in an abstract class. Non-abstract methods are defined and implemented as usual. Non-abstract methods can even call abstract methods, as the names and interfaces fully known. The behavior of the abstract method is only determined when t he method is implemented in a subclass, and therefore can vary in different sbclasses.If we simple wish to use class as a model for the subclass and do not require any objects from the class, we can define it as an abstract class by using ABSTRACT addition to the CLASS statement. We cannot create objects with CREATE OBJECT of a class defined in this way, and consequently we cannot access the instance components.
Methods can also b defined as abstract, to do this we use ABSTRACT addition with METHODS when we declare the method. An abstract method cannot be implemented in its own class, but only in a non-abstract subclass. This is why a which contains one or more abstract methods must be defined overall as abstract. Otherwise, we could create an object with an addressable method but without any implementation.
Both non-abstract and abstract methods can be defined in an abstract class. Non-abstract methods are defined and implemented as usual. Non-abstract methods can even call abstract methods, as the names and interfaces fully known. The behavior of the abstract method is only determined when t he method is implemented in a subclass, and therefore can vary in different sbclasses.
41. There can be a situations in which we want to protect a whole class or an individual method from uncontrolled specialization. Use the FINAL statement instead of ABSTRACT in the CLASS or METHODS statement. No further subclass can be derived from the final class. A final class marks the definite end of a path in the inheritance hierarchy.
A final method doesn't exclude subclass, but cannot be redefined. A final method cannot be abstract at the same time. A class can be final and abstract at the same time, but then only its static components can be used.There can be a situations in which we want to protect a whole class or an individual method from uncontrolled specialization. Use the FINAL statement instead of ABSTRACT in the CLASS or METHODS statement. No further subclass can be derived from the final class. A final class marks the definite end of a path in the inheritance hierarchy.
A final method doesn't exclude subclass, but cannot be redefined. A final method cannot be abstract at the same time. A class can be final and abstract at the same time, but then only its static components can be used.
42. CREATE PUBLIC
A class with the CREATE PUBLIC addition can be instantiated anywhere that the class is visible.
CREATE PROTECTED
A class with the CREATE PROTECTED addition can only be instantiated in methods of the class itself and its subclasses.
CREATE PRIVATE
A class with the CREATE PRIVATE addition can only be instantiated in methods of the class itself.
Cannot be instantiated as an inherited component of subclasses.
Superclass with Public Instantiation
The instance constructor of the super class is publicly visible.
It inherits the public instantiation of the super class, can be changed.
Super class with Protected Instantiation
The instance constructor of the superclass is visible in the subclass.
It inherits the protected instantiation of the super class, can be changed.
Super class with Private Instantiation
The instance constructor of the superclass is visible only in the superclass. Superclass with public Instantiation
The instance constructor of the superclass is publicly visible. If the instantiability of the subclass is not explicitly specified, it inherits the public instantiation of the superclass. The subclass can control the visibility of its own instance constructor independently of the superclass.
Superclass with Protected Instantiation
The instance constructor of the superclass is visible in the subclass. If the instantiability of the subclass is not explicitly specified, it inherits the protected instantiation of the superclass. The subclass can control the visibility of its own instance constructor independently of the superclass and thus also publish the protected instance constructor of the superclass in the specified section.
Superclass with Private Instantiation
The instance constructor of the superclass is visible only in the superclass. There are two different scenario:
The subclass is not a friend of the superclass: - Because only the superclass itself can call its instance constructor, the subclass cannot be instantiated. Therefore, the subclass has an implicit addition, CREATE NON.
The subclass is a friend of the superclass: - If he instantiatiability of the subclass has not been explicitly specified, it inherits the private instantiation of the subclass. As a friend, subclass can publish the private constructor of the superclass in the specified section.Superclass with public Instantiation
The instance constructor of the superclass is publicly visible. If the instantiability of the subclass is not explicitly specified, it inherits the public instantiation of the superclass. The subclass can control the visibility of its own instance constructor independently of the superclass.
Superclass with Protected Instantiation
The instance constructor of the superclass is visible in the subclass. If the instantiability of the subclass is not explicitly specified, it inherits the protected instantiation of the superclass. The subclass can control the visibility of its own instance constructor independently of the superclass and thus also publish the protected instance constructor of the superclass in the specified section.
Superclass with Private Instantiation
The instance constructor of the superclass is visible only in the superclass. There are two different scenario:
The subclass is not a friend of the superclass: - Because only the superclass itself can call its instance constructor, the subclass cannot be instantiated. Therefore, the subclass has an implicit addition, CREATE NON.
The subclass is a friend of the superclass: - If he instantiatiability of the subclass has not been explicitly specified, it inherits the private instantiation of the subclass. As a friend, subclass can publish the private constructor of the superclass in the specified section.
43. Interfaces exclusively describe the external point of contact of a class, but they do not contain their own implementation part.
Allows users to address different classes across different inheritance trees via a universal point of contact.
The only part of a class that is relevant to a external user is its public interface that is made up of the components of its public visibility section. All other components are irrelevant to the users. This aspect becomes clear particularly when using abstract methods in abstract classes. Because ABAP objects does not support multiple inheritance, the usage of the abstract class for defining interface is restricted to their subclass. However, it is also desirable to be able to define generally valid interfaces that can equally be used in several classes.
Standalone interfaces are independently defied interfaces without implementation that can be integrated and implemented in classes. Standalone interfaces are used to achieve a looser coupling between the class and a user.The only part of a class that is relevant to a external user is its public interface that is made up of the components of its public visibility section. All other components are irrelevant to the users. This aspect becomes clear particularly when using abstract methods in abstract classes. Because ABAP objects does not support multiple inheritance, the usage of the abstract class for defining interface is restricted to their subclass. However, it is also desirable to be able to define generally valid interfaces that can equally be used in several classes.
Standalone interfaces are independently defied interfaces without implementation that can be integrated and implemented in classes. Standalone interfaces are used to achieve a looser coupling between the class and a user.
44. The declaration of a standalone interface hardly varies from the declaration of the class. The declaration of the interface corresponds to the declaration part of the class, where instead of CLASS-ENDCLASS, we simply use INTERFACE-ENDINTERFACE. Interface dont need to be divided into different visibility sections because interface components are always integrated in the public visibility section of the class.
Each class can implement one or more interfaces. The only requirement for the implementing an interface is that the interface is know to the implementing class. It must therefore be defined locally in the same program or globally in the class library. Interfaces are incorporated solely in the public visibility section of the class through the INTERFACES statement. This means that the public visibility section of the class is expanded by the interface components. Each comp component os an implemented interface (intf) becomes a full component of the class and is identified within the class by name intf~comp. A class can therefore have a component of the same name, or various implemented interfaces can contain components of the same name. They all lie within one name space and are differentiated within the class by various intf~ prefixes, therefore we call the ~ sign the interface component selector. The class must implement all the methods of all the incorporated interfaces in its implementation part.
The declaration of a standalone interface hardly varies from the declaration of the class. The declaration of the interface corresponds to the declaration part of the class, where instead of CLASS-ENDCLASS, we simply use INTERFACE-ENDINTERFACE. Interface dont need to be divided into different visibility sections because interface components are always integrated in the public visibility section of the class.
Each class can implement one or more interfaces. The only requirement for the implementing an interface is that the interface is know to the implementing class. It must therefore be defined locally in the same program or globally in the class library. Interfaces are incorporated solely in the public visibility section of the class through the INTERFACES statement. This means that the public visibility section of the class is expanded by the interface components. Each comp component os an implemented interface (intf) becomes a full component of the class and is identified within the class by name intf~comp. A class can therefore have a component of the same name, or various implemented interfaces can contain components of the same name. They all lie within one name space and are differentiated within the class by various intf~ prefixes, therefore we call the ~ sign the interface component selector. The class must implement all the methods of all the incorporated interfaces in its implementation part.
46. ABAP Objects allows us to compose a new interface from several existing ones. To do this, we use INTERFACE statement in an interface definition. Composition of interfaces can be useful when modeling complex applications. An interface which contains at least one interface is called a compound interface. An interface which is contained in another interface is called component interface. ABAP Objects allows us to compose a new interface from several existing ones. To do this, we use INTERFACE statement in an interface definition. Composition of interfaces can be useful when modeling complex applications. An interface which contains at least one interface is called a compound interface. An interface which is contained in another interface is called component interface.
48. The compound interface i3 has a component i2 which itself compound. Although it kooks here as if the composition of several interfaces has resulted in component hierarchy, this is in fact not the case. All component interfaces of a compound interface are on the same level. It is not possible to compose name such as i3~i2~i1.The compound interface i3 has a component i2 which itself compound. Although it kooks here as if the composition of several interfaces has resulted in component hierarchy, this is in fact not the case. All component interfaces of a compound interface are on the same level. It is not possible to compose name such as i3~i2~i1.
49. If a component interface implemented in a class, all the interface components behave as if their interface had been implemented only once. The interface components of the individual component interfaces extend the public interface of the class by its original name.If a component interface implemented in a class, all the interface components behave as if their interface had been implemented only once. The interface components of the individual component interfaces extend the public interface of the class by its original name.
50. Alias names in classes
Alias names belong to the namespace of the components of a class and, like other components, must be assigned to a visibility section. The visibility of an alias name outside the class is governed by its own visibility section and not the visibility section of the interface component assigned.
From the example we can see that the method still has to be implemented with its full name, despite he alias name. The advantage of the alias name lies with the user. The class can use alias names to publish its interface components to the user as class-specific components.
Alias names in compound interface
As it is not possible to chain names in compound interfaces, alias names in component interfaces are the only way o addressing such components which otherwise would not be available in the compound interface.Alias names in classes
Alias names belong to the namespace of the components of a class and, like other components, must be assigned to a visibility section. The visibility of an alias name outside the class is governed by its own visibility section and not the visibility section of the interface component assigned.
From the example we can see that the method still has to be implemented with its full name, despite he alias name. The advantage of the alias name lies with the user. The class can use alias names to publish its interface components to the user as class-specific components.
Alias names in compound interface
As it is not possible to chain names in compound interfaces, alias names in component interfaces are the only way o addressing such components which otherwise would not be available in the compound interface.
51. Interface reference variables, like class reference variables, can contain object references. Although there are no instances of interfaces to which interface references can point, an interface reference can point to the objects of all the classes which is implement the corresponding interface. The static type of an interface reference variable is now an interface and not an class. As before, the dynamic type is the class of the object to which the interface reference is pointing at that moment. An interface reference therefore allows exactly those components of an object to be addressed which have been added to its class through the implementation of the interface.
Only the target variable is an interface reference.
The class of the class reference or one of its superclass must implement the interface of the interface reference. This category includes the creation of an object via an interface reference.
CREATE OBJECT iref TYPE class.
Both reference variables are interface references
This assignment is possible if the static type of the target variable is the same interface as the static type of the source variable or if the static type of the source variable is a compound interface of the target variable as a component.
Only the source variable is an interface reference
In this case the static type of the target variable must be general class object. The dynamic type of the source variable can be any class which implements the interface, and except for the object it is impossible to check statically whether it is actually the same as or more special than the class of the target variable.Interface reference variables, like class reference variables, can contain object references. Although there are no instances of interfaces to which interface references can point, an interface reference can point to the objects of all the classes which is implement the corresponding interface. The static type of an interface reference variable is now an interface and not an class. As before, the dynamic type is the class of the object to which the interface reference is pointing at that moment. An interface reference therefore allows exactly those components of an object to be addressed which have been added to its class through the implementation of the interface.
Only the target variable is an interface reference.
The class of the class reference or one of its superclass must implement the interface of the interface reference. This category includes the creation of an object via an interface reference.
CREATE OBJECT iref TYPE class.
Both reference variables are interface references
This assignment is possible if the static type of the target variable is the same interface as the static type of the source variable or if the static type of the source variable is a compound interface of the target variable as a component.
Only the source variable is an interface reference
In this case the static type of the target variable must be general class object. The dynamic type of the source variable can be any class which implements the interface, and except for the object it is impossible to check statically whether it is actually the same as or more special than the class of the target variable.
54. The static type of a reference variable oref is the type that is specified after oref TYPE REF TO class|intf. The static type is fixed during the entire runtime of a program. For object reference variables, the object type class for class reference variable and intf for interface reference variable are possible as static types.
The dynamic type of a reference variable is the class of the object to which the reference variable is currently pointing. The dynamic type is not fixed during the program run time. A reference variable oref usually recieves its dynamic type via assignments, parameter transfer, or via CREATE OBJECT oref TYPE class.
The dynamic type determines the actual behavior of the object. The static type, on the other hand detrmines the user view of an object. An ABAP program or a procedure, works with object reference variables and usually only knows their static type via an object reference variable, those components of an object can be accessed using the object component selector (->) that are declared in the static type and visible to user. The static type thus determines the dynamic type in that all components of the static type must exist in dynamic type.
We can check the dynamic type of a reference variable in ABAP debugger at ant time by looking its value. There the name of the class of the current object is displayed.The static type of a reference variable oref is the type that is specified after oref TYPE REF TO class|intf. The static type is fixed during the entire runtime of a program. For object reference variables, the object type class for class reference variable and intf for interface reference variable are possible as static types.
The dynamic type of a reference variable is the class of the object to which the reference variable is currently pointing. The dynamic type is not fixed during the program run time. A reference variable oref usually recieves its dynamic type via assignments, parameter transfer, or via CREATE OBJECT oref TYPE class.
The dynamic type determines the actual behavior of the object. The static type, on the other hand detrmines the user view of an object. An ABAP program or a procedure, works with object reference variables and usually only knows their static type via an object reference variable, those components of an object can be accessed using the object component selector (->) that are declared in the static type and visible to user. The static type thus determines the dynamic type in that all components of the static type must exist in dynamic type.
We can check the dynamic type of a reference variable in ABAP debugger at ant time by looking its value. There the name of the class of the current object is displayed.
55. The syntax check and the ABAP runtime environment ensures that this rule is never broken. The static type can never be specific then the dynamic type. This means that all the components that can be addressed via reference variable definitely exist in the referenced object. Therefore, a user does not necessarily need to know the actual dynamic type of the object with which it is working.The syntax check and the ABAP runtime environment ensures that this rule is never broken. The static type can never be specific then the dynamic type. This means that all the components that can be addressed via reference variable definitely exist in the referenced object. Therefore, a user does not necessarily need to know the actual dynamic type of the object with which it is working.
56. An assignment between reference variables implies the passing of the object reference in source variable to a target variable. In this context, nothing more then the address of an object in the memory is passed. After assignment both point to the same object. This mans that the target variable takes on dynamic type of the source variable.An assignment between reference variables implies the passing of the object reference in source variable to a target variable. In this context, nothing more then the address of an object in the memory is passed. After assignment both point to the same object. This mans that the target variable takes on dynamic type of the source variable.
57. The rule for assignment between reference variables is, of course always satisfied when the static type of the target variable is more general than the static type of the source variable, as the dynamic type of the source variable at runtime can only be more specialized than its static type. He validity f such an assignment can be determined during the syntax check. We describe such an assignment as an narrowing cast or up cast.
The term narrowing cast expresses that you are changing from a more specialized view to a more general view. The target variable knows only the same or fewer components than the source variable.
The term up cast expresses that the static type of the target variable can only change to higher nodes from the static type of the source variable in the interface tree, but not vice versa.The rule for assignment between reference variables is, of course always satisfied when the static type of the target variable is more general than the static type of the source variable, as the dynamic type of the source variable at runtime can only be more specialized than its static type. He validity f such an assignment can be determined during the syntax check. We describe such an assignment as an narrowing cast or up cast.
The term narrowing cast expresses that you are changing from a more specialized view to a more general view. The target variable knows only the same or fewer components than the source variable.
The term up cast expresses that the static type of the target variable can only change to higher nodes from the static type of the source variable in the interface tree, but not vice versa.
58. There can be situations in which we want to change from a less detailed view to a more detailed one. For assignment this means that the static type of the target variable is more specialized then the static type of the source variable. We term such assignment as widening cast or down cast.
For widening a cast the basic rule applies that irrespective of the static type of the source variable, the static type of the target variable must be more general or the same as the dynamic type of the source variable. Or to put it differently; the object to which the source variable is referring must also contain all the components which the target variable wishes to see. However, as the actual dynamic type of a source variable is statically unknown, a static check is impossible.There can be situations in which we want to change from a less detailed view to a more detailed one. For assignment this means that the static type of the target variable is more specialized then the static type of the source variable. We term such assignment as widening cast or down cast.
For widening a cast the basic rule applies that irrespective of the static type of the source variable, the static type of the target variable must be more general or the same as the dynamic type of the source variable. Or to put it differently; the object to which the source variable is referring must also contain all the components which the target variable wishes to see. However, as the actual dynamic type of a source variable is statically unknown, a static check is impossible.
60. On board an airplane there are passengers, pilots, and the cabin crew. The pilots and the passengers can press a ,button that calls a flight attendant. As soon as the event call_attendant occurs, the flight attendant soon determines who pressed the button and performs an appropriate action. The instance of the passengers and pilots are able to trigger events in which the instances of the class flight attendant take interest in the event and react accordingly.
Why do we need to use Events instead of calling the method in the instance of the flight attendant?
When call_attendant is pressed, we dont know which flight attendant is going to react. In other words, the triggerof an event does not know the handler.
The flight attendant can decide whether the event is even acknowledged. For example, the call button is ignored during the take-off and landing. Therefore thetrigger of an event does not even know if the event will have any effect.
Both events cannot be implemented using normal method calls. To call a method, the address must be known and a called method must always react. Triggering events, however is an indirect method call.
The benefit of the event concept is the it can establish a loose coupling between objects and classes. This is also referred as Publish-and-Subscribe mechanism. An object or a class can trigger an event, and another object or class can be interested in the event. In ABAP Objects, the publish-and-subscribe mechanism takes place on two levels, i.e., statically at the time of the declaration and during the program execution.On board an airplane there are passengers, pilots, and the cabin crew. The pilots and the passengers can press a ,button that calls a flight attendant. As soon as the event call_attendant occurs, the flight attendant soon determines who pressed the button and performs an appropriate action. The instance of the passengers and pilots are able to trigger events in which the instances of the class flight attendant take interest in the event and react accordingly.
Why do we need to use Events instead of calling the method in the instance of the flight attendant?
When call_attendant is pressed, we dont know which flight attendant is going to react. In other words, the triggerof an event does not know the handler.
The flight attendant can decide whether the event is even acknowledged. For example, the call button is ignored during the take-off and landing. Therefore thetrigger of an event does not even know if the event will have any effect.
Both events cannot be implemented using normal method calls. To call a method, the address must be known and a called method must always react. Triggering events, however is an indirect method call.
The benefit of the event concept is the it can establish a loose coupling between objects and classes. This is also referred as Publish-and-Subscribe mechanism. An object or a class can trigger an event, and another object or class can be interested in the event. In ABAP Objects, the publish-and-subscribe mechanism takes place on two levels, i.e., statically at the time of the declaration and during the program execution.
62. Declaring and Triggering Events:
To couple objects trough events, we must program accordingly both the classes of the triggers as well as the classes of the handlers. We must declare each event which is to be triggered with the methods of the class in its declaration part. Like all components of a class, event must be assigned to one of the three visibility sections of a class. This determines which users can handle an event. Events are defined using EVENTS statement.
To trigger an event defined in this way you use the RAISE EVENT statement in the methods of the same class. In the instance method of the class all instance events and static events of the class can be triggered. In static methods only static events can be triggered, even no object of the class exists.
Handling Events:
In any class we can define event handler methods for the events of other classes or the class itself. Like any other methods, event handler methods are declared with the METHODS or CLASS-METHODS statements, whereby FOR EVENT addition determines their role as event handler. Besides the parameter declared specifically during the event, handler methods for instance events can also import a sender parameter. This parameter is always implicitly present when an instance event is declared. It is typed as a reference variable of the type of the class in which the event is declared. When the event is triggered with RAISE EVENT in an instance method, this parameter is implicitly filled with a reference to the triggering object. If a handler imports sender, it can access the public visibility section of the triggering object via this parameter.
Registering:
In order for an event handler method to respond to a triggered event, the triggered event, the trigger to which it is to respond must be determined at runtime. Through this dynamic part of the publish and subscribe mechanism we couple methods in the handler objects to triggering objects.Declaring and Triggering Events:
To couple objects trough events, we must program accordingly both the classes of the triggers as well as the classes of the handlers. We must declare each event which is to be triggered with the methods of the class in its declaration part. Like all components of a class, event must be assigned to one of the three visibility sections of a class. This determines which users can handle an event. Events are defined using EVENTS statement.
To trigger an event defined in this way you use the RAISE EVENT statement in the methods of the same class. In the instance method of the class all instance events and static events of the class can be triggered. In static methods only static events can be triggered, even no object of the class exists.
Handling Events:
In any class we can define event handler methods for the events of other classes or the class itself. Like any other methods, event handler methods are declared with the METHODS or CLASS-METHODS statements, whereby FOR EVENT addition determines their role as event handler. Besides the parameter declared specifically during the event, handler methods for instance events can also import a sender parameter. This parameter is always implicitly present when an instance event is declared. It is typed as a reference variable of the type of the class in which the event is declared. When the event is triggered with RAISE EVENT in an instance method, this parameter is implicitly filled with a reference to the triggering object. If a handler imports sender, it can access the public visibility section of the triggering object via this parameter.
Registering:
In order for an event handler method to respond to a triggered event, the triggered event, the trigger to which it is to respond must be determined at runtime. Through this dynamic part of the publish and subscribe mechanism we couple methods in the handler objects to triggering objects.
64. Thank You