Thursday, 21 May 2020

Casting : Up/Narrow Cast, Down/Widening Cast


CLASS lcl_vehicle DEFINITION.
  PUBLIC SECTION.
    METHODS :
      constructor IMPORTING im_type TYPE char10 im_model TYPE char10 im_capacity TYPE i,
      increase_speed IMPORTING im_val TYPE i,
      apply_brake,
      get_speed,
      get_vehicle_detail.

  PROTECTED SECTION.
  PRIVATE SECTION.
    DATA : speed TYPE i.
    DATA : vehicle_type    TYPE char10,
           vehicle_model   TYPE char10,
           engine_capacity TYPE i.
ENDCLASS.

CLASS lcl_vehicle IMPLEMENTATION.
  METHOD constructor.
    vehicle_model = im_model.
    vehicle_type = im_type.
    engine_capacity = im_capacity.
  ENDMETHOD.

  METHOD increase_speed.
    speed = speed + im_val.
  ENDMETHOD.

  METHOD apply_brake.
    speed = 0.
  ENDMETHOD.
  METHOD get_vehicle_detail.
    WRITE :/ 'Type :', vehicle_type,
           / 'Model :', vehicle_model,
           / 'Engine Capacity :', engine_capacity.
  ENDMETHOD.
  METHOD get_speed.
    WRITE :/ 'Speed :', speed.
  ENDMETHOD.
ENDCLASS.


CLASS lcl_scooter DEFINITION INHERITING FROM lcl_vehicle.
  PUBLIC SECTION.
    METHODS : constructor IMPORTING im_type TYPE char10 im_model TYPE char10 im_capacity TYPE i im_helmet TYPE char10,
      get_scooter_details.

  PROTECTED SECTION.
  PRIVATE SECTION.
    DATA : helmet TYPE char10.
ENDCLASS.


CLASS lcl_scooter IMPLEMENTATION.
  METHOD constructor.
    super->constructor( im_type = im_type im_model = im_model im_capacity = im_capacity ).
    me->helmet = im_helmet.
  ENDMETHOD.
  METHOD get_scooter_details.
    CALL METHOD get_vehicle_detail( ).
    WRITE :/ 'Helmet :', helmet.
  ENDMETHOD.
ENDCLASS.


CLASS lcl_car DEFINITION INHERITING FROM lcl_vehicle.
  PUBLIC SECTION.
    METHODS : constructor IMPORTING im_type TYPE char10 im_model TYPE char10 im_capacity TYPE i im_seatbelt TYPE char10,
      get_car_details.

  PROTECTED SECTION.
  PRIVATE SECTION.
    DATA : seatbelt TYPE char10.
ENDCLASS.


CLASS lcl_car IMPLEMENTATION.
  METHOD constructor.
    super->constructor( im_type = im_type im_model = im_model im_capacity = im_capacity ).
    me->seatbelt = im_seatbelt.
  ENDMETHOD.
  METHOD get_car_details.
    CALL METHOD get_vehicle_detail( ).
    WRITE :/ 'Seatbelt :', seatbelt.
  ENDMETHOD.
ENDCLASS.


START-OF-SELECTION.

  DATA(lo_vehicle) = NEW lcl_vehicle( im_type = 'CAR' im_model = 'SWIFT' im_capacity = 1300 ).
  DATA(lo_vehicle2) = NEW lcl_vehicle( im_type = 'CAR' im_model = 'SAFARI' im_capacity = 1300 ).
  DATA : lo_scooter TYPE REF TO lcl_scooter.
  CREATE OBJECT lo_scooter EXPORTING  im_type = 'Activa' im_model = '4G' im_capacity = 125 im_helmet = 'Vega'.

  CALL METHOD lo_scooter->increase_speed( im_val = 10 ).
  CALL METHOD lo_scooter->get_speed.
  CALL METHOD lo_scooter->apply_brake.
  CALL METHOD lo_scooter->get_speed.
  CALL METHOD lo_scooter->get_scooter_details.

  WRITE :/ ' Vehicle class'.

  lo_vehicle->increase_speed( im_val = 40 ).
  lo_vehicle->get_speed( ).
  lo_vehicle->get_vehicle_detail( ).
  TRY .
      WRITE :/ 'Up/Narrow cast - assign scooter to vehicle '.
      lo_vehicle ?= lo_scooter.
      lo_vehicle->get_speed( ).
      lo_vehicle->get_vehicle_detail( ).
    CATCH cx_root.
      MESSAGE i555(bc_bor) WITH 'Something went wrong with UP Casting'.
  ENDTRY.


  TRY .
      WRITE :/ 'Down/Widening cast - vehicle to scooter'.
      lo_scooter ?= lo_vehicle2.
      lo_scooter->get_speed( ).
      lo_scooter->get_vehicle_detail( ).
    CATCH cx_root.
      MESSAGE i555(bc_bor) WITH 'Something went wrong with Down Casting'.
  ENDTRY.

No comments:

Post a Comment