Sub Classes Superclass LCL - AIRPLANE

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

SUB CLASSES Superclass LCL_AIRPLANE

***INCLUDE ZBC404_HF_LCL_AIRPLANE . ****************************************** * Definition part ****************************************** CLASS lcl_airplane DEFINITION. *-----------------------------* Public section *-----------------------------PUBLIC SECTION. TYPES: t_name(25) TYPE c. METHODS: constructor IMPORTING im_name TYPE t_name im_planetype TYPE saplane-planetype, display_attributes. * Static methods CLASS-METHODS: display_n_o_airplanes. *-----------------------------* Protected section *-----------------------------PROTECTED SECTION. * Private attributes DATA: name(25) TYPE c, planetype TYPE saplane-planetype. * Private static attribute CLASS-DATA n_o_airplanes TYPE i. ENDCLASS.

****************************************** * Implementation part ****************************************** CLASS lcl_airplane IMPLEMENTATION. METHOD constructor. name = im_name. planetype = im_planetype. * Counts number of instances n_o_airplanes = n_o_airplanes + 1. ENDMETHOD. METHOD display_attributes. WRITE:/ 'Name:', name, 'Planetype:', planetype. ENDMETHOD. METHOD display_n_o_airplanes. WRITE: / 'No. planes:', n_o_airplanes. ENDMETHOD. ENDCLASS.

Sub class LCL_PASSENGER_AIRPLANE


***INCLUDE ZBC404_HF_LCL_PASSENGER_PLANE . ******************************************************************* * This is a subclass of class lcl_airplane ******************************************************************* CLASS lcl_passenger_airplane DEFINITION INHERITING FROM lcl_airplane. PUBLIC SECTION. * The constructor contains the parameters from the superclass * plus the parameters from the subclass METHODS: constructor IMPORTING im_name TYPE t_name im_planetype TYPE saplane-planetype im_n_o_seats TYPE sflight-seatsmax, * Redefinition of superclass method display_attributes display_attributes REDEFINITION.

PRIVATE SECTION. DATA: n_o_seats TYPE sflight-seatsmax. ENDCLASS. CLASS lcl_passenger_airplane IMPLEMENTATION. METHOD constructor. * The constructor method of the superclass MUST be called withing the * construtor CALL METHOD super->constructor EXPORTING im_name n_o_seats = im_n_o_seats. ENDMETHOD. * The redefined display_attributes method METHOD display_attributes. CALL METHOD super->display_attributes. WRITE: / 'No. seats:', n_o_seats. ENDMETHOD. ENDCLASS. = im_name im_planetype = im_planetype.

Sub class LCL_CARGO_PLANE


***INCLUDE ZBC404_HF_LCL_CARGO_PLANE . ******************************************************************* * This is a subclass of class lcl_airplane ******************************************************************* CLASS lcl_cargo_plane DEFINITION INHERITING FROM lcl_airplane. PUBLIC SECTION. METHODS: * * The constructor contains the parameters from the superclass plus the parameters from the subclass constructor IMPORTING im_name TYPE t_name

im_planetype TYPE saplane-planetype im_cargomax type scplane-cargomax,

Redefinition of superclass method display_attributes display_attributes REDEFINITION. PRIVATE SECTION.

DATA:cargomax TYPE scplane-cargomax. ENDCLASS. CLASS lcl_cargo_plane IMPLEMENTATION. METHOD constructor. * The constructor method of the superclass MUST be called withing the * constructor CALL METHOD super->constructor EXPORTING im_name cargomax = im_cargomax. ENDMETHOD. METHOD display_attributes. * The redefined display_attributes method CALL METHOD super->display_attributes. WRITE: / 'Cargo max:', cargomax. ENDMETHOD. ENDCLASS. = im_name im_planetype = im_planetype.

The Main program that uses the classes


REPORT zbc404_hf_main . * Super class INCLUDE zbc404_hf_lcl_airplane. * Sub classes INCLUDE zbc404_hf_lcl_passenger_plane. INCLUDE zbc404_hf_lcl_cargo_plane. DATA: * Type ref to sub classes. Note: It is not necesssary to make typeref to the superclass o_passenger_airplane TYPE REF TO lcl_passenger_airplane, o_cargo_plane TYPE REF TO lcl_cargo_plane. START-OF-SELECTION. * Display initial number of instances = 0

CALL METHOD lcl_airplane=>display_n_o_airplanes. * Create objects CREATE OBJECT o_passenger_airplane EXPORTING im_name = 'LH505' im_planetype = 'Boing 747' im_n_o_seats = 350. CREATE OBJECT o_cargo_plane EXPORTING im_name = 'AR13' im_planetype = 'DC 3' im_cargomax = 35. * Display attributes CALL METHOD o_passenger_airplane->display_attributes. CALL METHOD o_cargo_plane->display_attributes. * Call static method display_n_o_airplanes * Note: The syntax for calling a superclass method, differs from the syntax when calling a * subclass method. * When calling a superclass => must be used instead of -> CALL METHOD lcl_airplane=>display_n_o_airplanes.

OUTPUT Result: No. planes: 0 Name: LH505 Planetype: Boing 747 No. seats: 350 Name: AR13 Planetype: DC 3 Cargo max: 35,0000 No. planes: 2

You might also like