Vous êtes sur la page 1sur 4

SAP R/3 Release ECC 5.

Exercises: ABAP Objects Advanced

SAP Development ABAP Training

Exercise 5:
Objectives:
Define Sub Classes
Redefine Super Class methods in Sub Classes
Steps:
1. Copy include program YOOPI_XX_LCL_AIRPLANE_C to
YOOPI_XX_LCL_AIRPLANE_INH.
2. Make both instance attributes in class lcl_airplane visible to their subclasses (Move them
from PRIVATE SECTION to PROTECTED SECTION).
3. Create subclass lcl_passenger_airplane for class lcl_airplane. Create the include program
YOOPI_XX_LCL_PASS_PLANE_INH for class lcl_passenger_airplane.
4. In this class declare a private instance attribute n_o_seats that has the same type as table
field sflight-seatsmax.
5. A public constructor is defined and implemented in this sub-class. This constructor provides
all instance attributes in the class with values.
6. Redefine method display_attributes of class lcl_airplane, so that, using the redefined
method, the WRITE statement displays all instance attributes.
7. Create subclass lcl_cargo_airplane for class lcl_airplane. Create the include program
YOOPI_XX_LCL_CAR_PLANE_INH for class lcl_cargo_airplane.
8. In this class declare a private instance attribute cargo_max that has the same type as table
field scplane-cagomax.
9. A public constructor has been defined and implemented in this sub-class. This constructor
provides all instance attributes in the class with values.
10. Redefine method display_attributes of class lcl_airplane, so that, using the redefined
method, the WRITE statement displays all instance attributes.
11. Create program YOOPR_XX_LCL_AIRPLANES_INH.
12. Use the INCLUDE statement to include the following programs
YOOPI_XX_LCL_AIRPLANE_INH
YOOPI_XX_LCL_PASS_PLANE_INH
YOOPI_XX_LCL_CAR_PLANE_INH
13. Use the DATA statement to create a reference for each subclass (lcl_passenger_airplane,
lcl_cargo_airplane).
14. Call the static method display_n_o_airplanes (before instantiating any objects).
15. Use the references in Step 13 to create one instance in each of the subclasses
lcl_passenger_airplane and lcl_cargo_airplane. Decide for yourself how to fill the
attributes.
16. Call the display_attributes method for each of the instances.
17. Call the static method display_n_o_airplanes again.

July 2007
Page 1

SAP R/3 Release ECC 5.0

Exercises: ABAP Objects Advanced

SAP Development ABAP Training

Exercise 6:
Objectives:
In this exercise you will demonstrate Polymorphism through Inheritance.
Steps:
1. Create a class definition for an abstract class named SHAPE.
2. Define and abstract method GET_AREA.
3. Define a method GET_NAME.
4. Create a class implementation for class SHAPE with the code for method GET_NAME.
5. Create three subclasses of super class SHAPE and name them : TRIANGLE, RECTANGLE,
SQUARE.
6. Create a constructor method to instantiate each sub-class. Define IMPORT/EXPORT
parameters as required.
7. Redefine the method GET_AREA and IMPORT the following parameters to calculate the area
as follows:
Triangle: Import the BASE and HEIGHT.
Square : Import the EDGE.
Circle : Import the RADIUS.
8. Apply the following formula:
Area of a Triangle = BASE * HEIGHT / 2
Area of a Square = EDGE2
Area of a Circle = Pi * r2 (where Pi = 22 / 7 and r is the radius of the circle).
9. Define global reference variables for each of the 3 sub-classes using the TYPE REF TO
statement.
10. Declare the START-OF-SELECTION event.
7. Instantiate an object for each sub-class using the CREATE OBJECT statement.
8. Call the method GET_NAME which is inherited from the parent class SHAPE..
9. Call the instance method to calculate the area for each of the objects created. Pass the
required
EXPORT parameters to the GET_AREA method.
11. Display the name and the corresponding area returned by the instance method.

July 2007
Page 2

SAP R/3 Release ECC 5.0

Exercises: ABAP Objects Advanced

SAP Development ABAP Training

Exercise 7:
Objectives:
In this exercise you will demonstrate Polymorphism through Interfaces.
Steps:
1. Modify program created in exercise 6 to define an interface COLOR and implement its
method APPLY_COLOR in class TRIANGLE, CIRCLE and SQUARE.
2. Create an interface definition named COLORS.
3. Define a method APPLY_COLOR for the interface with IMPORT parameter i_color.
4. Implement the method APPLY_COLOR in the class implementation of classes TRIANGLE,
CIRCLE and SQUARE in different ways.
E.g.
METHOD color~apply_color.
write: / 'CIRCLE'.
write: / 'Paint circle with color', i_color.
write: / 'Technique: USE THIN BRUSH'.
skip 2.
ENDMETHOD.
Note: Change the Technique for others, to make them different.
5. Instantiate and object from classes TRIANGLE, CIRCLE and SQUARE.
6. Call method APPLY_COLOR using the object reference variables of each object and display
the results.

Exercise 8:
Objectives:
In this exercise you will demonstrate Event handling.
An airline creates new flights and publicizes them in the media. Travel agencies can then
include these flights in their offerings. Senirio can be handled with Event.
Steps:
1. Create a class LCL_CARRIER and define an event flight_created,that you also trigger in the
class.
2. The event is a public event that has three transfer parameters: ex_carrid(type: sflightcarrid)
ex_connid (type: sflightconnid) and ex_fldate (type: sflight-fldate).
3. The event should be triggered in the existing create_a_new_flight method, after the APPEND
statement. Consider carefully how to pass the parameters.

July 2007
Page 3

SAP R/3 Release ECC 5.0

Exercises: ABAP Objects Advanced

SAP Development ABAP Training

4. Create a local class LCL_TRAVEL_AGENCY, write a handler method for the flight_created
event and register the travel agency to the flight_created event that are business partners of
the travel agency.
5. The flights created by these airlines should be saved in the travel agency in a flight list .
6. In class lcl_travel_agency, create an internal table list_of_flights as a public attribute.
7. Define a public instance method add_a_new_flight as a handler method for the flight_created
event in class lcl_plane.
8. Enter the flight type (ex_carrid) flight number (ex_connid) and the flight date (ex_fldate) and a
reference to the event trigger (sender) as IMPORTING parameters.
9. When implementing the add_a_new_flight method, enter the airline, the flight number and the
flight date in the list of flights (list_of_flights). To do this, create a table work area in the
method. This table work area must have the same structure as the internal tablelist_of_flights.
10. Use the APPEND statement to fill the table.
11. Define and implement the public instance method display_list_of_flights to display the flight
list list_of_flights. This method does not have any transfer parameters.

SourceCode.txt

July 2007
Page 4

Vous aimerez peut-être aussi