0% found this document useful (0 votes)
30 views

ABAP Introduction Course Training Exercises

The document describes a series of workshops on object-oriented ABAP. It outlines tasks to create classes that inherit from each other and implement interfaces. It also covers exception handling, polymorphism, and BADI implementations.

Uploaded by

Thierry Kemp
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

ABAP Introduction Course Training Exercises

The document describes a series of workshops on object-oriented ABAP. It outlines tasks to create classes that inherit from each other and implement interfaces. It also covers exception handling, polymorphism, and BADI implementations.

Uploaded by

Thierry Kemp
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Workshops advanced ABAP OO

Information
Please create a package in which all your development of this workshop will be collected or
create everything as local development object.

Replace all “XX”-marks by the sequence-number assigned to you.

To be executed at the beginning


Copy the existing classes ZCL_YY_ENGINE and ZCL_YY_VEHICLE to the new classes
ZCL_XX_ENGINE and ZCL_XX_VEHICLE respectively.

1/8
Workshops advanced ABAP OO

Workshop 8: Inheritance

Workshop content
Create some vehicle-subclasses, and make use of redefining.

Add attributes to the Vehicle-class


Add the protected attributes “FUEL_CONSUMPTION” and “FUEL_LEVEL” to your Vehicle-class.
Assign a default value to these attributes.

Create a calculation method in the Vehicle-class


Create a method “ESTIMATE_KM_LEFT” to your Vehicle-class, which will calculate the
remaining kilometers with the current fuel level. The result should be given back as a return
parameter.

Calculation will be something like ( fuel level / consumption ) * 100.

Create a Car-class
Create the class ZCL_XX_CAR as a subclass for your Vehicle-class.

Create a Bus-class
Create the class ZCL_XX_BUS as a subclass for your Vehicle-class.

This subclass should have an extra private attribute “N_O_PASSENGERS” (number of


passengers) of the type integer with default value 0.

When creating a Bus-object, it should be possible to pass the number of passengers. You will
have to provide a constructor for this. Make sure that the constructor of the Vehicle-class is also
called!

You should also redefine the method “ESTIMATE_KM_LEFT”: the difference with the original
calculation method is that the passengers have impact on the fuel consumption (for each
passenger in the bus, the consumption will be 1 liter/100 km higher).

Create the method for the test scenario of workshop 8


Within the class ZCL_XX_WS_ABAPOO create a static method START_WS8 and implement the
following scenario:

Create a Car-object and calculate the kilometers left.

Create a Bus-object and calculate the kilometers left.

Debug to see what happens

2/8
Workshops advanced ABAP OO

Workshop 9: Casting

Workshop content
In this workshop we are going to experiment with narrowing and widening cast and
polymorphism. Objects from different types will be put in he same table and will be processed
directly from that table.

Create the method for the test scenario of workshop 9


Within the class ZCL_XX_WS_ABAPOO create a static method START_WS9 and implement the
following scenario:

Create a vehicle, bus and car object with 3 different colors. Put these objects in 1 table.

After that, you have to loop over the table and call the “get_color”-method and the
“estimate_km_left”-method for each object.

Try also to call the “get_n_o_passengers”-method which is specific for the bus-object. Don’t try
too long, because it will not work.

At the end of the loop try to convert the object back to its original type. When the bus object is
back in its original state, you can again call the “get_n_o_passengers”-method.

Debug to see what happens

3/8
Workshops advanced ABAP OO

Workshop 10: Interfaces

Workshop content
In this workshop we are going to experiment with interfaces and casting.

We are going to create a company- and employee-class. This company wants to know every
month what the total costs of the employees and used vehicles is.

Employee and vehicle are both different classes with no relation with each other, but we want to
process them both as a cost. So, therefore we will also create an interface and implement it in
both classes.

Create an Employee class


Create a class with the name ZCL_XX_EMPLOYEE.

This class must contain a protected attribute MONTHLY_SALARY of the type integer. The
attribute should have a default value (ex. 2000).

Create the interface Cost


Create an interface with the name ZIF_XX_COST.

This interface must contain the method GET_MONTHLY_COST with a returning parameter
RV_MONTHLY_COST

Add a monthly cost attribute to the Vehicle class


Add a protected attribute MONTHLY_COST to the class ZCL_XX_VEHICLE. This attribute must
be of the type integer, and it should contain a default value (ex. 150).

Implement the interface to the Vehicle class


Add the interface ZIF_XX_COST to ZCL_XX_VEHICLE.

Implement the method GET_MONTHLY_COST of the interface: return the value of the
monthly_cost-attribute

Implement the interface to the Employee class


Add the interface ZIF_XX_COST to ZCL_XX_EMPLOYEE.

Implement the method GET_MONTHLY_COST of the interface: return the value of the
monthly_salary-attribute

Create a Company class


Create a class with the name ZCL_XX_COMPANY.

This class must contain a protected attribute called TOTAL_MONTHLY_COST of the type
integer. It should contain a default value (ex. 0). Create also a getter-method for this attribute.

4/8
Workshops advanced ABAP OO

Create a method ADD_TO_TOTAL_MONTHLY_COST. The method should have an import


parameter of the type of your created interface.
The method should use the GET_MONTHLY_COST-method of the interface to get the cost of the
object in the parameter. This cost should be added to the total_monthly_cost-attribute.

Create the method for the test scenario


Within the class ZCL_XX_WSOO create a static method WS10.

Create an object of the type vehicle and an object of the type employee. Put these 2 objects in 1
table that will contain reference variables of the type of your created interface.

Create also a company object that will be used to calculate the total monthly cost.

Loop over the table and for each item in the table the following actions should be executed:
• Execute the method of the company object to add the cost of the item to the total cost (by
sending the item as a parameter)

After the loop put the total cost of the company in a variable.

Debug to see what happens

5/8
Workshops advanced ABAP OO

Workshop 11: Exception handling

Workshop content
In this workshop we are going to experiment with exception handling

We are going to create our own exception class “maximum number vehicles reached”. We will
raise this exception in one of our methods to the calling method. In this calling method, the
exception will be handled.

Create an Exception class


Create an exception class with the name ZCX_XX_MAX_VEHICLES_REACHED.

This exception class should contain 1 attribute MAX_NUMBER_VEHICLES that can be shown as
part of the message text.

When the exception is raised, the message should be something like:


“Maximum number vehicles (max_number_vehicles) reached”.

Change constructor of the Vehicle class


Add an exception parameter to the constructor of the Vehicle class (so that the exception can be
raised to the calling method). This exception parameter should be of the type you just created.

Add the following check in your constructor: when n_o_vehicles becomes larger then the content
of the attribute MAX_VEHICLES, you should raise the exception (and give the max_vehicles
value as a parameter to the exception, so that it can be used in the error message).

Create the method for the test scenario


Within the class ZCL_XX_WSOO create a static method WS11.

Put the max_vehicles-attribute of the Vehicle class on a low value (ex. 3).

Create a loop that will be executed as long as the maximum number of vehicles is not reached.
In this loop you have to create an instance of the vehicle class. When the constructor of the
vehicle class raises the max_number_vehicles_reached-exception, this exception should be
catched here, the error message should be put in a variable, and the loop should be stopped.

Debug to see what happens

6/8
Workshops advanced ABAP OO

Workshop 12: BADI’s

Workshop content
Create a new BADI definition, 2 implementations and call the BADI-methods in a test method.

Create the BADI definition


Create a new BADI definition ZXX_WSOO.

It should be possible to create more than 1 implementation for this BADI.

Create also a filter of type “BU_TYPE” (BU_TYPE = business partner type. ‘1’ = person, ‘2’ =
organization) that will be used to determine which BADI implementation should be executed.

The BADI should have a method called “AFTER_CREATE” (do not define parameters yourself).

Create a BADI implementation ZXX_WSOO_PER


Create a BADI implementation that will only be executed when the BADI is called for a business
partner of the type ‘person’. Make use of the filter to let this work.

The method AFTER_CREATE should show ‘Person created’ on the screen.

Create a BADI implementation ZXX_WSOO_BP


Create a BADI implementation that will be executed when the BADI is called for a business
partner of the type ‘person’ and ‘organization’. Make use of the filter to let this work.

The method AFTER_CREATE should show ‘Person or organization created’ on the screen.

Create the method for the test scenario


Within the class ZCL_XX_WSOO create a static method WS12.

Create an instance of your BADI.

Call the ‘AFTER_CREATE’-method 3 times:


• 1 time for a business partner of type ‘3’ (=group)
• 1 time for a business partner of type ‘1’ (=person)
• 1 time for a business partner of type ‘2’ (=organization)

Debug to see what happens

7/8
Workshops advanced ABAP OO

Workshop 13: Events

Workshop content
In this workshop we are going to experiment with event-triggering and -handling.

When an employee is created a counter needs to be incremented in the company-object.

Create event in Employee class


Create an event “EV_EMPLOYEE_CREATED” in the Employee class. No parameters are
needed.

Trigger event when Employee is created


Make sure that when an employee is created, that the event is triggered.

Create counter attribute in Company class


Create a public attribute “N_O_EMPLOYEES” (number of employees) of the type integer in the
Company class with default value 0.

Handle event
Create an event-handler in the Company class which is triggered by the Employee-event. The
number of employees-attribute should be incremented by 1, each time the event is triggered.

Create the method for the test scenario


Within the class ZCL_XX_WSOO create a static method WS13.

Create 1 company object and 1 or more employee-objects. Print the number of created
employees at the end of the program.

Debug to see what happens

8/8

You might also like