Working with local classes Flashcards

1
Q

What are global classes in ABAP and how are they stored?

A

Global classes in ABAP are stored centrally in a class pool and contain reusable logic for other ABAP programs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are local classes in ABAP and where are they defined?

A

defined within a program or a global class, restricted to use within their defining scope.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How can you quickly create a new local class

A

typing “lcl” and then pressing strg+leertaste, adjust name of the class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the two parts of code of a class called

A

Definition and implementation. The definition part of a class is subdivided into up to three sections, called the visibility sections of the class (public, protected, private section).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does the TYPES statement allow within a class in ABAP?

A

defining types within a class, similarly to outside a class. If types are public, programs using the class can use these types for their variables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you declare attributes in an ABAP class?

A

Attributes in an ABAP class are declared using DATA for instance attributes and CLASS-DATA for static attributes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How are constants declared within ABAP classes?

A

Constants within ABAP classes are declared using CONSTANTS. They, along with types, are static components of the class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How are methods defined within ABAP classes?

A

METHODS for instance methods and CLASS-METHODS for static methods. The method’s name is followed by its signature, including the values exchanged with its caller and possible exceptions..

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you access the local classes within your globall class

A

on the bottom below the code editor are different tabs. One of them is called local types. Create a new local class by typing lcl and then strg+leer there. Remember to give it a name (“lcl_…”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Inside the class you work with attributes like with normal variables. How do you access static attributes outside though?

A

<class_name>=><static_attribute>
lcl_connection=>conn_counter = 5.
no blank spaces!

"=>" is called the static component selector
</static_attribute></class_name>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a reference variable

A

a special variable that is used to create, address and manage an object. It points at the instance of a class in the program memory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you declare a reference variable

A

DATA <referenceVariable> TYPE REF TO <className></className></referenceVariable>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you create a new instance of a class

A

With the NEW operator.
connection = NEW #( )
the hashtag means use the type of the variable before the equals sign, which is declared through the DATA … TYPE REF TO … part.
It is also possible to replace the hashtag with the name of the class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the difference between a normal and a static attribute

A

A normal attribute is unique for each single object. A static one is shared between each object. So if one object changes a static attribute, that value is simultaneously changed for all other objects of that type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Static components are adressed with the classname and “=>”. How are instance components attributes adressed?

A

With the instance component selector “->”:

<reference_variable>-><attribute>

connection->carrier_id = 'LH'.
</attribute></reference_variable>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the consequence if you code sth. like this
con = NEW #( ).
con2 = NEW #( ).

A

they both point to the same memory address. So if you change one of them, you “change the other one” as well

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What happens when you write:
con = NEW #( ).
con = NEW #( ).

A

The reference variable will no longer point to the first object, but rather to the second oe. Therefore, nothing points to the object generated during the first call. It will therefore be deleted by the garbage collector.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

How could you prevent the deletion of an object even though you use con = NEW #( ).
several times?

A

By saving it in an internal table

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

How do you create new methods?

A

Within the public/private/protected section of a class:
METHODS method_name
IMPORTING
i_helper TYPE i.
EXPORTING
e_helper TYPE i.

Importing is what variables are being fed to the method and export vice verca. These methods need an implementation in the IMPLEMENTATION section

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

When a piece of code is underlined in red squiggly lines, how do you access the quick fix menu for it

A

Strg + 1, or right click -> quick fix

21
Q

What kind of attributes are instance and static methods allowed to access

A

instance methods can access instance and static attributes.
Static methods can only access static attributes.

22
Q

Within the implementation of instance methods, ABAP offers the built in variable “ME”. Describe its purpose

A

ME is a reference variable, typed with the current class and filled at runtime with the address of the current instance. The use of ME is optional and should be avoided, unless the name of an attribute collides with the name of another data object, for example a parameter name.

Example:
Definition part of the class:
DATA carrier_id TYPE i.

METHODS set_attributes
IMPORTING
carrier_id TYPE i

now we have the case, where the parameter carrier_id in METHODS has the same name as the attribute carrier_id. The name itself would trherefore denote the parameter. To access the attribute of the current instance, we need “me->”:

Implementation part of the class:
METHOD set_attributes.
me->carrier_id = carrier_id.

23
Q

How can a method raise an exception

A

Within the method body, you can type:
RAISE EXCEPTION TYPE <exception_class>.
Execution will be terminated and control returned to method caller, if possible (raising clause, try endtry and catch block need to exist. quickfix can help with that)</exception_class>

24
Q

How are instance and static methods called

A

Instance methods:

<reference_variable>-><method_name>(
EXPORTING
IMPORTING
CHANGING
).

Static methods:
<class_name>=><static_method>(
...
).
</static_method></class_name></method_name></reference_variable>

25
Q

What difference does the keyword VALUE make here:

METHODS get_output
RETURNING
VALUE(r_output) TYPE STRING_TABLE.

A

By adding VALUE, the value itself will be passed and can be used in a variable. If we would not use VALUE, we would only pass the parameter by reference, basically just the pointer to the memory location. This means any changes made to the parameter within the method will affect the original data outside the method.

26
Q

What are the components of a method’s signature?

A

The components of a method’s signature include parameters and exceptions. Each parameter in the signature consists of a name and a type.

27
Q

What are the different types of parameters in a method, and how are they defined?

A

Importing Parameters: These are values received by the method from the caller. They can be made optional using the OPTIONAL addition or assigned a default value using the DEFAULT <val> addition.</val>

Exporting Parameters: Results returned by the method, optional for the caller.

Changing Parameters: Received values that can be altered and returned.

Returning Parameters: This is a method result that can be directly used in an expression. Only one returning parameter is allowed per method, and it uses pass-by-value parameter passing, denoted by surrounding the parameter name in brackets and preceding it with the keyword VALUE.

Exceptions (RAISING): Indicates potential errors for the calling program to handle.

28
Q

How are importing, exporting, and changing parameters defined when CALLING a method?

A

Importing Parameters: Listed after the keyword EXPORTING, these values are provided by the calling program and are essential for the method. The parameter names are on the left side of an equals sign (=), with an expression on the right side, matching the parameter’s type.

Exporting Parameters: Listed after the keyword IMPORTING, these results are returned by the method to the calling program. The parameter name is on the left side of an equals sign (=), and the right side variable must match the parameter’s type.

Changing Parameters: Listed after the keyword CHANGING, these values are passed by the calling program and can be modified by the method. The type of the variable on the right side must match the parameter’s type.

Example for Method with importing parameters:
connection->set_attributes(
EXPORTING
i_carrier_id = ‘LH’).

29
Q

How can you generate a method call, including parameter passing, using code completion in ADT?

A

Type in the reference variable or class name followed by the component selector (->/=>).

Press Ctrl + Space to display a list of components available.

Select a method from the list and press Shift + Enter to insert the method’s name and full signature into your code, including optional parameters listed in comment lines.

30
Q

What is the process for generating a method call, including parameter passing, using code completion in ADT?

A

Type in the reference variable or class name followed by the component selector (->).

Press Ctrl + Space to display a list of available attributes and methods.

Choose the desired method from the list.

Press Shift + Enter to insert the full signature of the method, including parameters.

31
Q

What does an object represent in object-oriented programming?

A

An object represents a real-life entity and contains attributes defining its characteristics, such as carrier ID and flight number for a flight connection.

32
Q

What is data encapsulation in object-oriented programming?

A

Data encapsulation restricts direct access to an object’s internal state, requiring operations to be performed through predefined methods, enhancing data integrity and control.

33
Q

Why is data encapsulation advantageous in object-oriented programming?

A

Data encapsulation enhances data integrity, reduces the risk of unintended changes, and promotes modular design, leading to more robust and maintainable code.

34
Q

How can attribute access be restricted in object-oriented programming?

A

Attribute access can be restricted by adding READ-ONLY to public section attributes or by moving them to a different section like private.

By making your attributes private - or read-only, at least - you can ensure that the client program uses the available set_attributes( ) method to set the values for attributes carrier_id and connection_id.

35
Q

How can you quickly change attribute visibility in ADT?

A

Place the cursor on the attribute, press Ctrl + 1, and select “Make <attribute> private" from the options.</attribute>

36
Q

How can inconsistencies in attribute initialization and modification be addressed in object-oriented programming?

A

By using constructor methods, which are automatically called by the runtime system when creating a new instance of a class. Constructors enforce non-initial values during instantiation and prevent later changes to attributes.

37
Q

What are the properties of a constructor method in object-oriented programming?

A

A constructor method is a public instance method with the reserved name “constructor.” It may have importing parameters to obtain starting values for attributes and may raise exceptions.

38
Q

How can you generate a constructor method in ABAP Development Tools (ADT)?

A

Place the cursor on the class name, press Ctrl + 1, and choose “Generate constructor” from the quick fixes. Then select the attributes to initialize with the constructor and adjust its definition and implementation as needed.

39
Q

What is a static constructor, also known as a class constructor, in ABAP?

A

A static constructor is a public static method with the reserved name class_constructor. It is called once when the class is first addressed during program execution and is typically used for dynamic initialization of static attributes.

40
Q

When is a static constructor called in ABAP?

A

The static constructor is called once when the class is first addressed during program execution, which could be during the first instantiation of the class, the first call of a static method, or the first access to a public static attribute.

41
Q

What are the properties of a static constructor method in ABAP?

A

A static constructor method has the reserved name class_constructor, is public and static, and does not have any parameters or exceptions. It is automatically invoked by the runtime system when the class is first addressed during program execution.

42
Q

In which order do you define the visibility sections of a class?

A

public section, protected section, private section

43
Q

A functional method must have:
A Exactly one returning parameter

B No changing parameters

C Exactly one exporting parameter

D Exactly one importing parameter

A

A: exactly one returning parameter

44
Q

The static constructor is executed when the class is addressed for the first time. When might this be?

A When you call a static method of the class.

B At the beginning of the program.

C When you instantiate the class.

A

A When you call a static method of the class.

C When you instantiate the class.

45
Q

You have declared two reference variables, ref1 and ref2. ref1 points to an object. You now execute the statement
ref2 = ref1. What happens?

A

The ABAP system assigns the address of the object to which ref1is pointing to reference variable ref2. There is only one
object.

B

The ABAP system creates a copy of the object to which ref1 is pointing and assigns its address to ref2. There are now
two objects.

A

A

The ABAP system assigns the address of the object to which ref1is pointing to reference variable ref2. There is only one
object.

46
Q

Q5. A class my_class contains the public static method my_method. What is the correct code to call this method?

A

my_class=>my_method().

B

my_class=>my_method().

C

my_class->my_method().

D

my_class->my_method().

A

B

my_class=>my_method().

47
Q

Q6. You have defined a class containing instance attributes and static attributes. You have also declared a reference
variable but not yet created an instance of the class. Which components of the class can you access at this point,
and how?

A

Instance components using the reference variable.

B

Static components using the reference variable.

C

Static components using the name of the class.

D

Instance components using the name of the class.

A

C

Static components using the name of the class.

48
Q

Q7. Which of the following signature elements may an instance constructor have?

A

Exporting parameters

B

Changing parameters

C

Importing parameters

D

Exceptions

A

C

Importing parameters

D

Exceptions

49
Q

Q8. Your class contains a public instance attribute attr. How could you ensure that its value can only be changed within
the class?

A

Convert it into a constant.

B

Make it a private attribute.

c

Move it to a different local class.

D

Leave it in the public section but use the READ-ONLY addition.

A

B

Make it a private attribute.

D

Leave it in the public section but use the READ-ONLY addition.