ABAP Introduction Course Training Exercises
ABAP Introduction Course Training Exercises
Information
Please create a package in which all your development of this workshop will be collected or
create everything as local development object.
1/8
Workshops advanced ABAP OO
Workshop 8: Inheritance
Workshop content
Create some vehicle-subclasses, and make use of redefining.
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.
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).
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 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.
3/8
Workshops advanced ABAP OO
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.
This class must contain a protected attribute MONTHLY_SALARY of the type integer. The
attribute should have a default value (ex. 2000).
This interface must contain the method GET_MONTHLY_COST with a returning parameter
RV_MONTHLY_COST
Implement the method GET_MONTHLY_COST of the interface: return the value of the
monthly_cost-attribute
Implement the method GET_MONTHLY_COST of the interface: return the value of the
monthly_salary-attribute
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 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.
5/8
Workshops advanced ABAP OO
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.
This exception class should contain 1 attribute MAX_NUMBER_VEHICLES that can be shown as
part of the message text.
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).
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.
6/8
Workshops advanced ABAP OO
Workshop content
Create a new BADI definition, 2 implementations and call the BADI-methods in a test method.
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).
The method AFTER_CREATE should show ‘Person or organization created’ on the screen.
7/8
Workshops advanced ABAP OO
Workshop content
In this workshop we are going to experiment with event-triggering and -handling.
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 1 company object and 1 or more employee-objects. Print the number of created
employees at the end of the program.
8/8