Reference: skysky
In this section, we implement inheritance.
Background: There are two types of aircraft: a passenger plane and a cargo plane. Obviously, they all belong to one type of aircraft, so they get the parent class according to the object abstraction. A passenger plane has a special attribute, that is, the number of seats, while a cargo plane also has a special attribute that can carry the maximum weight. Therefore, the display attribute method of the original parent class is no longer applicable. We can define this method as an abstract method (virtual function in C ++) or keep its common implementation, define the implementation of this method in the subclass.
The UML diagram is as follows:
*----------------------------------------------------------------------*
* Include z_sample_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.
*------------------------------
* Private Section
*------------------------------
Private 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.
* Counts number of instances
N_o_airplanes = n_o_airplanes + 1.
Name = im_name.
Planetype = im_planetype.
Endmethod.
Method display_attributes.
Write:/'name: ', name, 'planetype:', planetype.
Endmethod.
Method display_n_o_airplanes.
Write:/'No. Planes: ', n_o_airplanes.
Endmethod.
Endclass.
*----------------------------------------------------------------------*
* Include z_sample_lcl_pa_airplane *
*----------------------------------------------------------------------*
**************************************** ***************************
* 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
* Construtor
Call method super-> Constructor
Exporting im_name = im_name
Im_planetype = im_planetype.
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.
*----------------------------------------------------------------------*
* Include z_sample_lcl_ca_airplane *
*----------------------------------------------------------------------*
**************************************** ***************************
* 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
*---------------------------------------------------------------------*
Class lcl_cargo_plane implementation.
Method constructor.
* The constructor method of the superclass must be called withing
* Constructor
Call method super-> Constructor
Exporting im_name = im_name
Im_planetype = im_planetype.
Cargomax = im_cargomax.
Endmethod.
Method display_attributes.
* The redefined display_attributes Method
Call method super-> display_attributes.
Write:/'Cargo MAX: ', cargomax.
Endmethod.
Endclass.
Report zbobo_oo_011.
* Super class
Include z_sample_lcl_airplane.
* Sub classes
Include z_sample_lcl_pa_airplane.
Include z_sample_lcl_ca_airplane.
Data:
* Type ref to sub classes. Note: It is not necesssary to make typeref
* 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 = 'h505'
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
* Syntax when calling a subclass method.
* When calling a superclass => must be used instead of->
Call method lcl_airplane => display_n_o_airplanes.