Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

OOP's concept

Former Member
0 Likes
1,022

Hi,

I wanted to understand the basic concepts of OOP's.

Like class, instance, method??

How to use it??

anybody will pls tell me in the hierarchical form??

7 REPLIES 7
Read only

Former Member
0 Likes
840

Hi,

check the below links lot of info and examples r there

http://www.sapgenie.com/abap/OO/index.htm

http://www.geocities.com/victorav15/sapr3/abap_ood.html

http://www.brabandt.de/html/abap_oo.html

Check this cool weblog:

/people/thomas.jung3/blog/2004/12/08/abap-persistent-classes-coding-without-sql

/people/thomas.jung3/blog/2004/12/08/abap-persistent-classes-coding-without-sql

http://help.sap.com/saphelp_nw04/helpdata/en/c3/225b6254f411d194a60000e8353423/frameset.htm

http://www.sapgenie.com/abap/OO/

http://www.sapgenie.com/abap/OO/index.htm

http://help.sap.com/saphelp_nw04/helpdata/en/c3/225b5654f411d194a60000e8353423/content.htm

http://www.esnips.com/doc/375fff1b-5a62-444d-8ec1-55508c308b17/prefinalppt.ppt

http://www.esnips.com/doc/2c76dc57-e74a-4539-a20e-29383317e804/OO-abap.pdf

http://www.esnips.com/doc/5c65b0dd-eddf-4512-8e32-ecd26735f0f2/prefinalppt.ppt

http://www.allsaplinks.com/

http://www.sap-img.com/

http://www.sapgenie.com/

http://help.sap.com

http://www.sapgenie.com/abap/OO/

http://www.sapgenie.com/abap/OO/index.htm

http://www.sapgenie.com/abap/controls/index.htm

http://www.esnips.com/doc/2c76dc57-e74a-4539-a20e-29383317e804/OO-abap.pdf

http://www.esnips.com/doc/0ef39d4b-586a-4637-abbb-e4f69d2d9307/SAP-CONTROLS-WORKSHOP.pdf

http://www.sapgenie.com/abap/OO/index.htm

http://help.sap.com/saphelp_erp2005/helpdata/en/ce/b518b6513611d194a50000e8353423/frameset.htm

http://www.sapgenie.com/abap/OO/

these links

http://help.sap.com/saphelp_47x200/helpdata/en/ce/b518b6513611d194a50000e8353423/content.htm

For funtion module to class

http://help.sap.com/saphelp_47x200/helpdata/en/c3/225b5954f411d194a60000e8353423/content.htm

for classes

http://help.sap.com/saphelp_47x200/helpdata/en/c3/225b5c54f411d194a60000e8353423/content.htm

for methods

http://help.sap.com/saphelp_47x200/helpdata/en/08/d27c03b81011d194f60000e8353423/content.htm

for inheritance

http://help.sap.com/saphelp_47x200/helpdata/en/dd/4049c40f4611d3b9380000e8353423/content.htm

for interfaces

http://help.sap.com/saphelp_47x200/helpdata/en/c3/225b6254f411d194a60000e8353423/content.htm

For Materials:

1) http://help.sap.com/printdocu/core/Print46c/en/data/pdf/BCABA/BCABA.pdf -- Page no: 1291

2) http://esnips.com/doc/5c65b0dd-eddf-4512-8e32-ecd26735f0f2/prefinalppt.ppt

3) http://esnips.com/doc/2c76dc57-e74a-4539-a20e-29383317e804/OO-abap.pdf

4) http://esnips.com/doc/0ef39d4b-586a-4637-abbb-e4f69d2d9307/SAP-CONTROLS-WORKSHOP.pdf

5) http://esnips.com/doc/92be4457-1b6e-4061-92e5-8e4b3a6e3239/Object-Oriented-ABAP.ppt

6) http://esnips.com/doc/448e8302-68b1-4046-9fef-8fa8808caee0/abap-objects-by-helen.pdf

7) http://esnips.com/doc/39fdc647-1aed-4b40-a476-4d3042b6ec28/class_builder.ppt

😎 http://www.amazon.com/gp/explorer/0201750805/2/ref=pd_lpo_ase/102-9378020-8749710?ie=UTF8

1) http://www.erpgenie.com/sap/abap/OO/index.htm

2) http://help.sap.com/saphelp_nw04/helpdata/en/ce/b518b6513611d194a50000e8353423/frameset.htm

Regards,

Priyanka.

Read only

Former Member
0 Likes
840

hi neha

u just see the link

www.sapgenie.com

Read only

Former Member
0 Likes
840

Hi Neha,

Check with the below links for OOPs concept in ABAP.

http://en.wikipedia.org/wiki/ABAP

http://help.sap.com/saphelp_nw04/helpdata/en/c3/225b5654f411d194a60000e8353423/content.htm

Reward if useful.

All the best.

Sayee

Read only

selvakumar_mohan
Active Participant
0 Likes
840

Object

Just as procedures used to build structured programs, objects are used to build Object Oriented Programs. An Object Oriented Program is a collection of objects that are organized for, and cooperate toward, the accomplishment of some goal.

In general every Object:

• Contains Data. The data stores information that describes the state of the object.

• Has a set of defined behaviors. These behaviors are the things that the object “knows” how to do and are triggered by sending the object a message

• Has an Individual identity. This makes it possible to distinguish an object from another, just as it’s possible to distinguish one program variable from another.

Everything is an Object

• Everything is an object. Think of an object as a fancy variable; it stores data, but you can also ask it to perform operations on itself by making requests. In theory, you can take any conceptual component in the problem you’re trying to solve (dogs, buildings, services, etc.) and represent it as an object in your program.

• A program is a bunch of objects telling each other what to do by sending messages. To make a request of an object, you “send a message” to that object. More concretely, you can think of a message as a request to call a function that belongs to a particular object.

• Each object has its own memory made up of other objects. Or, you make a new kind of object by making a package containing existing objects. Thus, you can build up complexity in a program while hiding it behind the simplicity of objects.

• Every object has a type. Using the parlance, each object is an instance of a class, where “class” is synonymous with “type.” The most important distinguishing characteristic of a class is “what messages can you send to it?”

• All objects of a particular type can receive the same messages. This is actually a very loaded statement, as you will see later. Because an object of type circle is also an object of type shape, a circle is guaranteed to receive shape messages. This means you can write code that talks to shapes and automatically handle anything that fits the description of a shape.

This substitutability is one of the most powerful concepts in OOP.

Some language designers have decided that object-oriented programming itself is not adequate to easily solve all programming problems, and advocate the combination of various approaches into multiparadigm programming languages.

An object has an interface. Aristotle was probably the first to begin a careful study of the concept of type. He was known to speak of “the class of fishes and the class of birds.” The concept that all objects, while being unique, are also part of a set of objects those have characteristics and behaviors in Common was directly used in the first object-oriented language, Simula, with its fundamental keyword class that introduces a new type into a program (thus class and type often used synonymously).

Simula, as its name implies, was created for developing simulations such as the classic “bank teller problem.” In this, you have a bunch of tellers, customers, accounts, transactions, etc.

The members (elements) of each class share some commonality: every account has a balance, every teller can accept a deposit, etc. At the same time, each member has its own state; each account has a different balance, each teller has a name. Thus the tellers, customers, accounts, transactions, etc. can each be represented with a unique entity in the computer program. This entity is the object, and each object belongs to a particular class that defines its characteristics and behaviors.

So, although what we really do in object-oriented programming is create new data types, virtually all object-oriented programming languages use the “class” keyword. When you see the word “type” think “class” and vice versa.

Some people make a distinction, stating that type determines the interface while class is a particular implementation of that interface. Once a type is established; you can make as many objects of that type as you like, and then manipulate those objects as the elements that exist in the problem you are trying to solve.

Indeed, one of the challenges of object-oriented programming is to create a one-to-one mapping between the elements in the problem space (the place where the problem actually exists) and the solution space (the place where you’re modeling that problem, such as a computer).

But how do you get an object to do useful work?

There must be a way to make a request of that object so it will do something, such as complete a transaction, draw something on the screen or turn on a switch. And each object can satisfy only certain requests. The requests you can make of an object are defined by its interface, and the type is what determines the interface. The idea of type being equivalent to interface is fundamental in object-oriented programming.

Class

Classes are the central element of object-orientation.

A Class describes a general element or a general concept, for example the abstract concepts Business Partner, Material, Transaction, Equipment or List. Classes realize an abstract data type.

Classes contain components: Attributes, Methods and Events.

Classes are templates for objects. You can either define a class locally in an ABAP program, or globally in the class library using the Class Builder tool in the ABAP Workbench. The class library is part of the R/3 repository. Class library classes are stored in special programs called class pools. A class pool is automatically generated for each global class that you create with the Class Builder. The Class Builder also generates the coding frame that defines the classes in the class pool. Global classes are visible to all ABAP programs of the R/3 System.

To define local classes in an ABAP program, such as a report or a function group, you must type the corresponding statements manually into the program. A local class is visible only inside the program in which it is defined.

The data, or variables defined within a Class are called instance variables. The code is contained within methods. Collectively, the methods and variables defined within a class are called members of the class. Thus, it is the methods that determine how a class’s data can be used.

Variables defined within a class are called instance variables because each instance of the class ( that is , each object of the class ) contains its own copy of these variables. Thus, the data for one object is separate and unique from the data for another.

More importantly “ You will never actually write the code for an Object: What you write is the pattern that is used to make objects ”

In ABAP Classes are defined between the CLASS and ENDCLASS statements. A class definition consists of a declaration part, in which the components are defined, and an implementation part, in which the methods are implemented.


CLASS c_user DEFINITION.
  		PUBLIC SECTION.
		    CLASS-DATA: instance_count type i.
		     DATA: id type i.
		     METHODS:  CONSTRUCTOR,
		                      display,
             	           get_user RETURNING value(p_user)  like sy-uname.
		PRIVATE SECTION.
		      DATA:  a_user like sy-uname.
ENDCLASS.

CLASS c_user IMPLEMENTATION.
		  METHOD CONSTRUCTOR.
			      a_user = sy-uname.
		  ENDMETHOD.
		  METHOD display.
		      WRITE :/ a_user, id, instance_count.
		  ENDMETHOD.
		  METHOD get_user.
			p_user = a_user.
		 ENDMETHOD.
ENDCLASS.


 Class components
  Possible class components are attributes, methods and events. 
•	Attributes
They are the internal data variables within a class. They can have any ABAP data type. We distinguish between instance attributes and static attributes. Instance attributes are declared by DATA and determine the state of an instance. You cannot work with instance attributes without creating an object first. Static attributes are declared by CLASS-DATA and determine the state of a class, which in a way applies to all instances. Static attributes form a data set that is shared by the whole class and all of its objects. You do not need to create an object to work with static attributes. 
•	Methods
They are the class’ procedures. They can access all class attributes and can therefore change the state of an object. They have a parameter interface similar to the interface of function modules. They can have named IMPORTING, EXPORTING, and CHANGING parameters, which can be optional or required and can be passed either by reference or by value. As with attributes, we distinguish between instance methods and static methods. Instance methods are declared by METHODS and can access all the attributes of a class. Static methods are declared by CLASS-METHODS and can only access the static attributes of a class. 
Implementing Methods

You must implement all of the methods in a class in the implementation part of the class in a 

METHOD <meth>.
 ...
ENDMETHOD. 

block. When you implement the method, you do not have to specify any interface parameters, since these are defined in the method declaration. The interface parameters of a method behave like local variables within the method implementation. You can define additional local variables within a method using the DATA statement. 
As in function modules, you can use the RAISE <exception> and MESSAGE RAISING statements to handle error situations.
When you implement a static method, remember that it can only work with the static attributes of your class. Instance methods can work with both static and instance attributes. 

Calling Methods

To call a method, use the following statement: 

CALL METHOD <meth> EXPORTING... <ii> =.<f i>... 
                   IMPORTING... <ei> =.<g i>... 
                   CHANGING ... <ci> =.<f i>... 
                   RECEIVING         r = h 
                   EXCEPTIONS... <ei> = rc i...

The way in which you address the method <method> depends on the method itself and from where you are calling it. Within the implementation part of a class, you can call the methods of the same class directly using their name <meth>.


CALL METHOD <meth>...

Outside the class, the visibility of the method depends on whether you can call it at all. Visible instance methods can be called from outside the class using

CALL METHOD <ref>-><meth>...

where <ref> is a reference variable whose value points to an instance of the class. Visible instance methods can be called from outside the class using

CALL METHOD <class>=><meth>...

where <class> is the name of the relevant class.
When you call a method, you must pass all non-optional input parameters using the EXPORTING or CHANGING addition in the CALL METHOD statement. You can (but do not have to) import the output parameters into your program using the IMPORTING or RECEIVING addition. Equally, you can (but do not have to) handle any exceptions triggered by the exceptions using the EXCEPTIONS addition. However, this is recommended.
You pass and receive values to and from methods in the same way as with function modules, that is, with the syntax: 

... <Formal parameter> = <Actual parameter>

after the corresponding addition. The interface parameters (formal parameters) are always on the left-hand side of the equals sign. The actual parameters are always on the right. The equals sign is not an assignment operator in this context; it merely serves to assign program variables to the interface parameters of the method. 
If the interface of a method consists only of a single IMPORTING parameter, you can use the following shortened form of the method call:

CALL METHOD <method>( f).

The actual parameter <f> is passed to the input parameters of the method. 
If the interface of a method consists only of IMPORTING parameters, you can use the following shortened form of the method call:

CALL METHOD <method>(....<ii> =.<f i>...)
.
Each actual parameter <f i > is passed to the corresponding 
formal parameter <i i >.

•	Events and Event Handlers  

As earlier told ABAP supports object oriented features and also in addition to  those features, ABAP supports Event handler methods.
So our primary question would be 




Event Handler Methods
 
Before we step into Event Handler Methods, it is very important to understand and know how methods work in ABAP.
Event handler methods are special methods that cannot all be called using the CALL METHOD statement. Instead, they are triggered using events. You define a method as an event handler method using the addition
... FOR EVENT <evt> OF <cif>...
in the METHODS or CLASS-METHODS statement. 
The following special rules apply to the interface of an event handler method: 
o	The interface may only consist of IMPORTING parameters. 
o	Each IMPORTING parameter must be an EXPORTING parameter of the event <evt>. 
o	The attributes of the parameters are defined in the declaration of the event <evt> (EVENTS statement) and are adopted by the event handler method. 


Read only

Former Member
0 Likes
840

Hi neha

the foolowing document clearly shown the dif between class,method and interface

class

CLASS C1 DEFINITION.
PUBLIC SECTION.
METHODS:
EVENTS:
PROTECTED SECTION.
DATA:
METHODS:
EVENTS:
PRIVATE SECTION.
DATA:
METHODS:
EVENTS:
ENDCLASS.

Classes are the templates for objects

A class definition declares

– Its components: Attributes, Methods, Events

– The visibility of its components: Public, Protected,

Private

The public components form the external

interface of the class – they are visible to all

users of the class as well as to methods within

the class and to methods of subclasses*

Protected components form the interface of the

class to its subclasses* – they are visible to

methods of the heirs of the class as well as to

methods within the class

Private components can only be used in the

methods of the class itself

programs within the main program

Method

CLASS c1 DEFINITION.
PUBLIC SECTION.
METHODS:
do_something
IMPORTING … i1 TYPE …
EXPORTING … e1 TYPE …
CHANGING … c1 TYPE …
EXCEPTIONS … e1.
PRIVATE SECTION.
DATA:
ENDCLASS.
CLASS c1 IMPLEMENTATION.
METHOD do_something
…
ENDMETHOD.
ENDCLASS.

Methods are processing blocks with a parameter

interface very similar to function modules

– With function modules, you should type each

parameter but are not forced to; with methods, you

must type each parameter

Methods are declared, with their parameter

interface, in the class definition part and

implemented in the class implementation part

– Notice that the method’s parameter interface is not

restated in the implementation part.

Instance and static

CLASS c1 DEFINITION.
PUBLIC SECTION.
DATA:
CLASS-DATA:
CONSTANTS:
METHODS:
CLASS-METHODS:
EVENTS:
CLASS-EVENTS:
PRIVATE SECTION.
DATA:
ENDCLASS.

Instance components exist separately in each

instance (object) of the class

Static components only exist once per class and

are valid for all instances of the class

Static components are declared with the CLASS-

  • keywords

Static components can be used without even

creating an instance of the class

Static methods can only access static attributes

of their class

Constants cannot be modified during compile

time or runtime and hence are somewhat like

static attributes

Cheers

Srinu

Read only

Former Member
0 Likes
840

• Calling a ststic attribute using CALL METHOD

• Registering a static event handler

• Registering an evetm handler method for a static event

The static constructor cannot be called explicitly.

Protected components

When we are talking subclassing and enheritance there is one more component than Public and Private, the Protected component. Protected components can be used by the superclass and all of the subclasses. Note that Subclasses cannot access Private components.

Polymorphism

Polymorphism: When the same method is implemented differently in different classes. This can be done using enheritance, by redefining a method from the superclass in subclasses and implement it differently.

Template for making a class

Delete the parts that should not be used

******************************************

  • Definition part

******************************************

CLASS xxx DEFINITION.

*----

-


  • Public section

*----

-


PUBLIC SECTION.

TYPES:

DATA:

  • Static data

CLASS-DATA:

  • Methods

METHODS:

  • Using the constructor to initialize parameters

constructor IMPORTING xxx type yyy,

  • Method with parameters

mm1 IMPORTING iii TYPE ttt.

  • Method without parameters

mm2.

  • Static methods

CLASS-METHODS:

*----


*

  • Protected section. Also accessable by subclasses

*----

-


PROTECTED SECTION.

*----

-


  • Private section. Not accessable by subclasses

*----

-


PRIVATE SECTION.

ENDCLASS.

******************************************

  • Implementation part

******************************************

CLASS lcl_airplane IMPLEMENTATION.

METHOD constructor.

ENDMETHOD.

METHOD mm1.

ENDMETHOD.

METHOD mm2.

ENDMETHOD.

ENDCLASS.

Template for calling a class

  • Create reference to class lcl_airplane

DATA: airplane1 TYPE REF TO lcl_airplane.

START-OF-SELECTION.

  • Create instance using parameters in the cosntructor method

CREATE OBJECT airplane1 exporting im_name = 'Hansemand'

im_planetype = 'Boing 747'.

  • Calling a method with parameters

CALL METHOD: airplane1->display_n_o_airplanes,

airplane1->display_attributes.

Subclass

CLASS xxx DEFINITION INHERITING FROM yyy.

Using af class as a parameter for a method

The class LCL_AIRPLANE is used as a parameter for method add_a_new_airplane:

METHODS:

add_a_new_airplane importing im_airplane TYPE REF to lcl_airplane.

Interfaces

In ABAP interfaces are implemented in addition to, and independently of classes. An interface only has a declaration part,

and do not have visibility sections. Components (Attributes, methods, constants, types) can be defined the same way as in classes.

• Interfaces are listed in the definition part lof the class, and must always be in the PUBLIC SECTION.

• Operations defined in the interface atre impemented as methods of the class. All methods of the interface

must be present in the implementation part of the class.

• Attributes, events, constants and types defined in the interface are automatically available to the class

carrying out the implementation.

• Interface components are addressed in the class by ]

Define, implement and use simple class | Use constructor to create an object with parameters | Subclassing | Polymorphism | Events

Define, implement and use simple class

***INCLUDE ZBC404_HF_LCL_AIRPLANE .

******************************************

  • Definition part

******************************************

CLASS lcl_airplane DEFINITION.

*----

-


  • Public section

*----

-


PUBLIC SECTION.

TYPES: t_name(25) TYPE c.

METHODS:

constructor,

set_attributes IMPORTING p_name TYPE t_name

p_planetype TYPE saplane-planetype,

display_attributes,

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.

ENDMETHOD.

METHOD set_attributes.

name = p_name.

planetype = p_planetype.

ENDMETHOD.

METHOD display_attributes.

WRITE:/ 'Name:', name, 'Planetype:', planetype.

ENDMETHOD.

METHOD display_n_o_airplanes.

WRITE: / 'No. planes:', n_o_airplanes.

ENDMETHOD.

ENDCLASS.

REPORT zbc404_hf_maintain_airplanes .

INCLUDE zbc404_hf_lcl_airplane.

  • Create reference to class lcl_airplane

DATA: airplane1 TYPE REF TO lcl_airplane,

airplane2 TYPE REF TO lcl_airplane.

START-OF-SELECTION.

  • Create instance

CREATE OBJECT airplane1.

CALL METHOD: airplane1->display_n_o_airplanes.

CREATE OBJECT airplane2.

  • Setting attributes using a method with parameters

CALL METHOD airplane1->set_attributes EXPORTING p_name = 'Kurt'

p_planetype = 'MD80'.

END-OF-SELECTION.

  • Using methods

CALL METHOD: airplane1->display_n_o_airplanes,

airplane1->display_attributes.

The resulting report:

Maintain airplanes

No. planes: 1

No. planes: 2

Name: Kurt Planetype: MD80

Use constructor to create an object with parameters

CLASS lcl_airplane DEFINITION.

PUBLIC SECTION.

TYPES: t_name(25) TYPE c.

METHODS:

constructor importing p2_name type t_name

p2_planetype TYPE saplane-planetype,

..... more code .......

CLASS lcl_airplane IMPLEMENTATION.

METHOD constructor.

name = p2_name.

planetype = p2_planetype.

..... more code .......

START-OF-SELECTION.

CREATE OBJECT airplane1 exporting p2_name = 'Hansemand'

p2_planetype = 'Boing 747'.

Subclassing

This example uses a superclass LCL_AIRPLANE and subclasses it into LCL_PASSENGER_AIRPLANE and LCL_CARGO_PLANE.

LCL_AIRPLANE has a method display_n_o_airplanes that displays the number of object instances.

LCL_PASSENGER_AIRPLANE has the private instance attribute n_o_seats, and redefines the superclass method display_attributes,

so it also displays n_o_seats.

LCL_CARGO_PLANE has the private instance attribute cargomax, and redefines the superclass method

display_attributes, so it also displays cargomax.

All instance attributes are provided by the cunstructor method.

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 = 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.

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 = 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.

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.

Result:

No. planes: 0

Name: LH505 Planetype: Boing 747

No. seats: 350

Name: AR13 Planetype: DC 3

Cargo max: 35,0000

No. planes: 2

Polymorphism

Polymorphism: When the same method is implemented differently in different classes. This can be done u

sing enheritance, by redefining a method from the superclass in subclasses and implement it differently.

Classes:

• lcl_airplane Superclass

• lcl_cargo_airplane Subclass

• lcl_passenger_airplane Subclass

The method estimate_fuel_consumption is implemented differently in the 3 classes, as it depends on the airplane type.

Object from different classes are stored in an internal table (plane_list) consisting of references to the

superclass, and the processed

identically for all the classes.

What coding for the estimate_fuel_consumption method taht is actually executed, depends on the dynamic

type of the plane reference variable,

that is, depends on which object plane points to.

DATA: cargo_plane TYPE REF to lcl_cargo_airplane,

passenger_plane TYPE REF to lcl_passenger_airplane,

plane_list TYPE TABLE OF REF TO lcl_airplane.

  • Creating the list of references

CREATE OBJECT cargo_plane.

APPEND cargo_plane to plane_list.

CREATE OBJECT passenger_plane

APPEND passenger_plane to plane list.

  • Generic method for calucalting required fuel

METHOD calculate required_fuel.

DATA: plane TYPE REF TO lcl_airplane.

LOOP AT plane_list INTO plane.

re_fuel = re_fuel + plane->estimate_fuel_consumption( distance ).

ENDLOOP.

ENDMETHOD.

Working example:

This example assumes that the classes lcl_airplane, lcl_passnger_airplane and lcl_cargo plane exists.

Create objects of type lcl_cargo_plane and lcl_passenger_airplane, adds them to a list in lcl_carrier,

and displays the list.

*&----


*

*& Include ZBC404_HF_LCL_CARRIER *

*&----


*

  • CLASS lcl_carrier DEFINITION *

*----


*

CLASS lcl_carrier DEFINITION.

PUBLIC SECTION.

TYPES: BEGIN OF flight_list_type,

connid TYPE sflight-connid,

fldate TYPE sflight-fldate,

airplane TYPE REF TO lcl_airplane,

seatsocc TYPE sflight-seatsocc,

cargo(5) TYPE p DECIMALS 3,

END OF flight_list_type.

METHODS: constructor IMPORTING im_name TYPE string,

get_name RETURNING value(ex_name) TYPE string,

add_a_new_airplane IMPORTING

im_airplane TYPE REF TO lcl_airplane,

create_a_new_flight importing

im_connid type sflight-connid

im_fldate type sflight-fldate

im_airplane type ref to lcl_airplane

im_seatsocc type sflight-seatsocc

optional

im_cargo type p optional,

display_airplanes.

PRIVATE SECTION.

DATA: name TYPE string,

list_of_airplanes TYPE TABLE OF REF TO lcl_airplane,

list_of_flights TYPE TABLE OF flight_list_type.

ENDCLASS.

*----


*

  • CLASS lcl_carrier IMPLEMENTATION

*----


*

CLASS lcl_carrier IMPLEMENTATION.

METHOD constructor.

name = im_name.

ENDMETHOD.

METHOD get_name.

ex_name = name.

ENDMETHOD.

METHOD create_a_new_flight.

DATA: wa_list_of_flights TYPE flight_list_type.

wa_list_of_flights-connid = im_connid.

wa_list_of_flights-fldate = im_fldate.

wa_list_of_flights-airplane = im_airplane.

IF im_seatsocc IS INITIAL.

wa_list_of_flights-cargo = im_cargo.

ELSE.

wa_list_of_flights-seatsocc = im_seatsocc.

ENDIF.

APPEND wa_list_of_flights TO list_of_flights.

ENDMETHOD.

METHOD add_a_new_airplane.

APPEND im_airplane TO list_of_airplanes.

ENDMETHOD.

METHOD display_airplanes.

DATA: l_airplane TYPE REF TO lcl_airplane.

LOOP AT list_of_airplanes INTO l_airplane.

CALL METHOD l_airplane->display_attributes.

ENDLOOP.

ENDMETHOD.

ENDCLASS.

REPORT zbc404_hf_main .

*******************************************************************

  • This reprort uses class LCL_AIRPLNAE and subclasses

  • LCL_CARGO_PLANE and LCL_PASSENGER_AIRPLANE and class LCL_CARRIER

*******************************************************************

  • Super class for airplanes

INCLUDE zbc404_hf_lcl_airplane.

  • Sub classes for airplanes

INCLUDE zbc404_hf_lcl_passenger_plane.

INCLUDE zbc404_hf_lcl_cargo_plane.

  • Carrier class

INCLUDE zbc404_hf_lcl_carrier.

DATA:

  • Type ref to classes

o_passenger_airplane TYPE REF TO lcl_passenger_airplane,

o_passenger_airplane2 TYPE REF TO lcl_passenger_airplane,

o_cargo_plane TYPE REF TO lcl_cargo_plane,

o_cargo_plane2 TYPE REF TO lcl_cargo_plane,

o_carrier TYPE REF TO lcl_carrier.

START-OF-SELECTION.

  • Create objects

CREATE OBJECT o_passenger_airplane

EXPORTING

im_name = 'LH505'

im_planetype = 'Boing 747'

im_n_o_seats = 350.

CREATE OBJECT o_passenger_airplane2

EXPORTING

im_name = 'SK333'

im_planetype = 'MD80'

im_n_o_seats = 110.

CREATE OBJECT o_cargo_plane

EXPORTING

im_name = 'AR13'

im_planetype = 'DC 3'

im_cargomax = 35.

CREATE OBJECT o_cargo_plane2

EXPORTING

im_name = 'AFL124'

im_planetype = 'Iljutsin 2'

im_cargomax = 35000.

CREATE OBJECT o_carrier

EXPORTING im_name = 'Spritisch Airways'.

  • Add passenger and cargo planes to the list of airplanes

CALL METHOD o_carrier->add_a_new_airplane

EXPORTING im_airplane = o_passenger_airplane.

CALL METHOD o_carrier->add_a_new_airplane

EXPORTING im_airplane = o_passenger_airplane2.

CALL METHOD o_carrier->add_a_new_airplane

EXPORTING im_airplane = o_cargo_plane.

CALL METHOD o_carrier->add_a_new_airplane

EXPORTING im_airplane = o_cargo_plane2.

  • Display list of airplanes

call method o_carrier->display_airplanes.

Result:

Name: LH505 Planetype: Boing 747

No. seats: 350

Name: SK333 Planetype: MD80

No. seats: 110

Name: AR13 Planetype: DC 3

Cargo max: 35,0000

Name: AFL124 Planetype: Iljutsin 2

Cargo max: 35.000,0000

Events

Below is a simple example of how to implement an event.

REPORT zbc404_hf_events_5.

*----


*

  • CLASS lcl_dog DEFINITION

*----


*

CLASS lcl_dog DEFINITION.

PUBLIC SECTION.

  • Declare events

EVENTS:

dog_is_hungry

EXPORTING value(ex_time_since_last_meal) TYPE i.

METHODS:

constructor

IMPORTING im_name TYPE string,

set_time_since_last_meal

IMPORTING im_time TYPE i,

on_dog_is_hungry FOR EVENT dog_is_hungry OF lcl_dog

IMPORTING ex_time_since_last_meal.

ENDCLASS.

*----


*

  • CLASS lcl_dog IMPLEMENTATION

*----


*

CLASS lcl_dog IMPLEMENTATION.

METHOD constructor.

WRITE: / 'I am a dog and my name is', im_name.

ENDMETHOD.

METHOD set_time_since_last_meal.

IF im_time < 4.

SKIP 1.

WRITE: / 'You fool, I am not hungry yet'.

ELSE.

  • Subsrcribe for event:

  • set handler <Event handler method>

  • FOR <ref_sender>!FOR ALL INSTANCES [ACTIVATION <var>]

SET HANDLER on_dog_is_hungry FOR ALL INSTANCES ACTIVATION 'X'.

  • Raise event

RAISE EVENT dog_is_hungry

EXPORTING ex_time_since_last_meal = im_time.

ENDIF.

ENDMETHOD.

METHOD on_dog_is_hungry.

  • Event method, called when the event dog_is_hungry is raised

SKIP 1.

WRITE: / 'You son of a ****. I have not eaten for more than',

ex_time_since_last_meal, ' hours'.

WRITE: / 'Give me something to eat NOW!'.

ENDMETHOD.

ENDCLASS.

*----


*

  • R E P O R T

*----


*

DATA: o_dog1 TYPE REF TO lcl_dog.

START-OF-SELECTION.

CREATE OBJECT o_dog1 EXPORTING im_name = 'Beefeater'.

CALL METHOD o_dog1->set_time_since_last_meal

EXPORTING im_time = 2.

  • This method call will raise the event dog_is_hungy

  • because time > 3

CALL METHOD o_dog1->set_time_since_last_meal

EXPORTING im_time = 5.

Result:

I am a dog and my name is Beefeater

You fool, I am not hungry yet

You son of a ****. I have not eaten for more than 5 hours

Give me something to eat NOW

This example is a step by step example, moving from a simple clas to more sophisticated classes, and covers inheritance, interfaces and events.

Simple class | Inheritance and ploymorphism | Interfaces | Events

1. Simple class

This example shows how to create a simple employee class. The constructor method is used to initialize number and name of thje employee when the object is created. A display_employee method can be called to show the attributes of the employee, and CLASS-METHOD dosplay_no_of_employees can be called to show the total number of employees (Number of instances of the employee class).

REPORT zbc404_hf_events_1.

*********************************************************************

  • L C L _ E M P L O Y E E

*********************************************************************

*---- LCL Employee - Definition

CLASS lcl_employee DEFINITION.

PUBLIC SECTION.

*----

-


  • The public section is accesible from outside

*----

-


TYPES:

BEGIN OF t_employee,

no TYPE i,

name TYPE string,

END OF t_employee.

METHODS:

constructor

IMPORTING im_employee_no TYPE i

im_employee_name TYPE string,

display_employee.

  • Class methods are global for all instances

CLASS-METHODS: display_no_of_employees.

PROTECTED SECTION.

*----

-


  • The protecetd section is accesible from the class and its subclasses

*----

-


  • Class data are global for all instances

CLASS-DATA: g_no_of_employees TYPE i.

PRIVATE SECTION.

*----

-


  • The private section is only accesible from within the classs

*----

-


DATA: g_employee TYPE t_employee.

ENDCLASS.

*--- LCL Employee - Implementation

CLASS lcl_employee IMPLEMENTATION.

METHOD constructor.

g_employee-no = im_employee_no.

g_employee-name = im_employee_name.

g_no_of_employees = g_no_of_employees + 1.

ENDMETHOD.

METHOD display_employee.

WRITE:/ 'Employee', g_employee-no, g_employee-name.

ENDMETHOD.

METHOD display_no_of_employees.

WRITE: / 'Number of employees is:', g_no_of_employees.

ENDMETHOD.

ENDCLASS.

************************************************************************

  • R E P O R T

*********************************************************************

DATA: g_employee1 TYPE REF TO lcl_employee,

g_employee2 TYPE REF TO lcl_employee.

START-OF-SELECTION.

CREATE OBJECT g_employee1

EXPORTING im_employee_no = 1

im_employee_name = 'John Jones'.

CREATE OBJECT g_employee2

EXPORTING im_employee_no = 2

im_employee_name = 'Sally Summer'.

CALL METHOD g_employee1->display_employee.

CALL METHOD g_employee2->display_employee.

CALL METHOD g_employee2->display_no_of_employees.

2. Inheritance and polymorphism

This example uses a superclass lcl_company_employees and two subclasses lcl_bluecollar_employee and lcl_whitecollar_employee to add employees to a list and then display a list of employees and there wages. The wages are calcukated in the method add_employee, but as the wages are calculated differently for blue collar employees and white collar emplyees, the superclass method add_employee is redeifined in the subclasses.

Principles:

Create super class LCL_CompanyEmployees.

The class has the methods:

• Constructor

• Add_Employee - Adds a new employee to the list of employees

• Display_Employee_List - Displays all employees and there wage

• Display_no_of_employees - Displays total number of employees

Note the use of CLASS-DATA to keep the list of employees and number of employees the same from instance to instance.

Create subclasses lcl_bluecollar_employee and lcl_whitecollar_employee. The calsses are identical, except for the redifinition of the add_employee method, where the caclculation of wage is different.

Methodes:

• Constructor. The constructor is used to initialize the attributes of the employee. Note that the constructor in the supclasss has to be called from within the constructor of the subclass.

• Add_Employee. This is a redinition of the same method in the superclass. In the redefined class the wage is calcuated, and the superclass method is called to add the employees to the emploee list.:

The program

REPORT zbc404_hf_events_2 .

*******************************************************

  • Super class LCL_CompanyEmployees

*******************************************************

CLASS lcl_company_employees DEFINITION.

PUBLIC SECTION.

TYPES:

BEGIN OF t_employee,

no TYPE i,

name TYPE string,

wage TYPE i,

END OF t_employee.

METHODS:

constructor,

add_employee

IMPORTING im_no TYPE i

im_name TYPE string

im_wage TYPE i,

display_employee_list,

display_no_of_employees.

PRIVATE SECTION.

CLASS-DATA: i_employee_list TYPE TABLE OF t_employee,

no_of_employees TYPE i.

ENDCLASS.

*-- CLASS LCL_CompanyEmployees IMPLEMENTATION

CLASS lcl_company_employees IMPLEMENTATION.

METHOD constructor.

no_of_employees = no_of_employees + 1.

ENDMETHOD.

METHOD add_employee.

  • Adds a new employee to the list of employees

DATA: l_employee TYPE t_employee.

l_employee-no = im_no.

l_employee-name = im_name.

l_employee-wage = im_wage.

APPEND l_employee TO i_employee_list.

ENDMETHOD.

METHOD display_employee_list.

  • Displays all employees and there wage

DATA: l_employee TYPE t_employee.

WRITE: / 'List of Employees'.

LOOP AT i_employee_list INTO l_employee.

WRITE: / l_employee-no, l_employee-name, l_employee-wage.

ENDLOOP.

ENDMETHOD.

METHOD display_no_of_employees.

  • Displays total number of employees

SKIP 3.

WRITE: / 'Total number of employees:', no_of_employees.

ENDMETHOD.

ENDCLASS.

*******************************************************

  • Sub class LCL_BlueCollar_Employee

*******************************************************

CLASS lcl_bluecollar_employee DEFINITION

INHERITING FROM lcl_company_employees.

PUBLIC SECTION.

METHODS:

constructor

IMPORTING im_no TYPE i

im_name TYPE string

im_hours TYPE i

im_hourly_payment TYPE i,

add_employee REDEFINITION.

PRIVATE SECTION.

DATA:no TYPE i,

name TYPE string,

hours TYPE i,

hourly_payment TYPE i.

ENDCLASS.

*---- CLASS LCL_BlueCollar_Employee IMPLEMENTATION

CLASS lcl_bluecollar_employee IMPLEMENTATION.

METHOD constructor.

  • The superclass constructor method must be called from the subclass

  • constructor method

CALL METHOD super->constructor.

no = im_no.

name = im_name.

hours = im_hours.

hourly_payment = im_hourly_payment.

ENDMETHOD.

METHOD add_employee.

  • Calculate wage an call the superclass method add_employee to add

  • the employee to the employee list

DATA: l_wage TYPE i.

l_wage = hours * hourly_payment.

CALL METHOD super->add_employee

EXPORTING im_no = no

im_name = name

im_wage = l_wage.

ENDMETHOD.

ENDCLASS.

*******************************************************

  • Sub class LCL_WhiteCollar_Employee

*******************************************************

CLASS lcl_whitecollar_employee DEFINITION

INHERITING FROM lcl_company_employees.

PUBLIC SECTION.

METHODS:

constructor

IMPORTING im_no TYPE i

im_name TYPE string

im_monthly_salary TYPE i

im_monthly_deducations TYPE i,

add_employee REDEFINITION.

PRIVATE SECTION.

DATA:

no TYPE i,

name TYPE string,

monthly_salary TYPE i,

monthly_deducations TYPE i.

ENDCLASS.

*---- CLASS LCL_WhiteCollar_Employee IMPLEMENTATION

CLASS lcl_whitecollar_employee IMPLEMENTATION.

METHOD constructor.

  • The superclass constructor method must be called from the subclass

  • constructor method

CALL METHOD super->constructor.

no = im_no.

name = im_name.

monthly_salary = im_monthly_salary.

monthly_deducations = im_monthly_deducations.

ENDMETHOD.

METHOD add_employee.

  • Calculate wage an call the superclass method add_employee to add

  • the employee to the employee list

DATA: l_wage TYPE i.

l_wage = monthly_salary - monthly_deducations.

CALL METHOD super->add_employee

EXPORTING im_no = no

im_name = name

im_wage = l_wage.

ENDMETHOD.

ENDCLASS.

*******************************************************

  • R E P O R T

*******************************************************

DATA:

  • Object references

o_bluecollar_employee1 TYPE REF TO lcl_bluecollar_employee,

o_whitecollar_employee1 TYPE REF TO lcl_whitecollar_employee.

START-OF-SELECTION.

  • Create bluecollar employee obeject

CREATE OBJECT o_bluecollar_employee1

EXPORTING im_no = 1

im_name = 'Gylle Karen'

im_hours = 38

im_hourly_payment = 75.

  • Add bluecollar employee to employee list

CALL METHOD o_bluecollar_employee1->add_employee

EXPORTING im_no = 1

im_name = 'Gylle Karen'

im_wage = 0.

  • Create whitecollar employee obeject

CREATE OBJECT o_whitecollar_employee1

EXPORTING im_no = 2

im_name = 'John Dickens'

im_monthly_salary = 10000

im_monthly_deducations = 2500.

  • Add bluecollar employee to employee list

CALL METHOD o_whitecollar_employee1->add_employee

EXPORTING im_no = 1

im_name = 'Karen Johnson'

im_wage = 0.

  • Display employee list and number of employees. Note that the result

  • will be the same when called from o_whitecollar_employee1 or

  • o_bluecolarcollar_employee1, because the methods are defined

  • as static (CLASS-METHODS)

CALL METHOD o_whitecollar_employee1->display_employee_list.

CALL METHOD o_whitecollar_employee1->display_no_of_employees.

The resulting report

List of Employees

1 Karen Johnson 2.850

2 John Dickens 7.500

Total number of employees: 2

3. Interfaces

This example is similiar to th eprevious example, however an interface is implemented with the method add_employee. Note that the interface is only implemented in the superclass ( The INTERFACE stament), but also used in the subclasses.

The interface in the example only contains a method, but an iterface can also contain attrbutes, constants, types and alias names.

The output from example 3 is similiar to the output in example 2.

All changes in the program compared to example 2 are marked with red.

REPORT zbc404_hf_events_3 .

*----


*

  • INTERFACE lif_employee

*----


*

INTERFACE lif_employee.

METHODS:

add_employee

IMPORTING im_no TYPE i

im_name TYPE string

im_wage TYPE i.

ENDINTERFACE.

*******************************************************

  • Super class LCL_CompanyEmployees

*******************************************************

CLASS lcl_company_employees DEFINITION.

PUBLIC SECTION.

INTERFACES lif_employee.

TYPES:

BEGIN OF t_employee,

no TYPE i,

name TYPE string,

wage TYPE i,

END OF t_employee.

METHODS:

constructor,

  • add_employee "Removed

IMPORTING im_no TYPE i

im_name TYPE string

im_wage TYPE i,

display_employee_list,

display_no_of_employees.

PRIVATE SECTION.

CLASS-DATA: i_employee_list TYPE TABLE OF t_employee,

no_of_employees TYPE i.

ENDCLASS.

*-- CLASS LCL_CompanyEmployees IMPLEMENTATION

CLASS lcl_company_employees IMPLEMENTATION.

METHOD constructor.

no_of_employees = no_of_employees + 1.

ENDMETHOD.

METHOD lif_employee~add_employee.

  • Adds a new employee to the list of employees

DATA: l_employee TYPE t_employee.

l_employee-no = im_no.

l_employee-name = im_name.

l_employee-wage = im_wage.

APPEND l_employee TO i_employee_list.

ENDMETHOD.

METHOD display_employee_list.

  • Displays all employees and there wage

DATA: l_employee TYPE t_employee.

WRITE: / 'List of Employees'.

LOOP AT i_employee_list INTO l_employee.

WRITE: / l_employee-no, l_employee-name, l_employee-wage.

ENDLOOP.

ENDMETHOD.

METHOD display_no_of_employees.

  • Displays total number of employees

SKIP 3.

WRITE: / 'Total number of employees:', no_of_employees.

ENDMETHOD.

ENDCLASS.

*******************************************************

  • Sub class LCL_BlueCollar_Employee

*******************************************************

CLASS lcl_bluecollar_employee DEFINITION

INHERITING FROM lcl_company_employees.

PUBLIC SECTION.

METHODS:

constructor

IMPORTING im_no TYPE i

im_name TYPE string

im_hours TYPE i

im_hourly_payment TYPE i,

lif_employee~add_employee REDEFINITION..

PRIVATE SECTION.

DATA:no TYPE i,

name TYPE string,

hours TYPE i,

hourly_payment TYPE i.

ENDCLASS.

*---- CLASS LCL_BlueCollar_Employee IMPLEMENTATION

CLASS lcl_bluecollar_employee IMPLEMENTATION.

METHOD constructor.

  • The superclass constructor method must be called from the subclass

  • constructor method

CALL METHOD super->constructor.

no = im_no.

name = im_name.

hours = im_hours.

hourly_payment = im_hourly_payment.

ENDMETHOD.

METHOD lif_employee~add_employee.

  • Calculate wage an call the superclass method add_employee to add

  • the employee to the employee list

DATA: l_wage TYPE i.

l_wage = hours * hourly_payment.

CALL METHOD super->lif_employee~add_employee

EXPORTING im_no = no

im_name = name

im_wage = l_wage.

ENDMETHOD.

ENDCLASS.

*******************************************************

  • Sub class LCL_WhiteCollar_Employee

*******************************************************

CLASS lcl_whitecollar_employee DEFINITION

INHERITING FROM lcl_company_employees.

PUBLIC SECTION.

METHODS:

constructor

IMPORTING im_no TYPE i

im_name TYPE string

im_monthly_salary TYPE i

im_monthly_deducations TYPE i,

lif_employee~add_employee REDEFINITION.

PRIVATE SECTION.

DATA:

no TYPE i,

name TYPE string,

monthly_salary TYPE i,

monthly_deducations TYPE i.

ENDCLASS.

*---- CLASS LCL_WhiteCollar_Employee IMPLEMENTATION

CLASS lcl_whitecollar_employee IMPLEMENTATION.

METHOD constructor.

  • The superclass constructor method must be called from the subclass

  • constructor method

CALL METHOD super->constructor.

no = im_no.

name = im_name.

monthly_salary = im_monthly_salary.

monthly_deducations = im_monthly_deducations.

ENDMETHOD.

METHOD lif_employee~add_employee.

  • Calculate wage an call the superclass method add_employee to add

  • the employee to the employee list

DATA: l_wage TYPE i.

l_wage = monthly_salary - monthly_deducations.

CALL METHOD super->lif_employee~add_employee

EXPORTING im_no = no

im_name = name

im_wage = l_wage.

ENDMETHOD.

ENDCLASS.

*******************************************************

  • R E P O R T

*******************************************************

DATA:

  • Object references

o_bluecollar_employee1 TYPE REF TO lcl_bluecollar_employee,

o_whitecollar_employee1 TYPE REF TO lcl_whitecollar_employee.

START-OF-SELECTION.

  • Create bluecollar employee obeject

CREATE OBJECT o_bluecollar_employee1

EXPORTING im_no = 1

im_name = 'Gylle Karen'

im_hours = 38

im_hourly_payment = 75.

  • Add bluecollar employee to employee list

CALL METHOD o_bluecollar_employee1->lif_employee~add_employee

EXPORTING im_no = 1

im_name = 'Karen Johnson'

im_wage = 0.

  • Create whitecollar employee obeject

CREATE OBJECT o_whitecollar_employee1

EXPORTING im_no = 2

im_name = 'John Dickens'

im_monthly_salary = 10000

im_monthly_deducations = 2500.

  • Add bluecollar employee to employee list

CALL METHOD o_whitecollar_employee1->lif_employee~add_employee

EXPORTING im_no = 1

im_name = 'Gylle Karen'

im_wage = 0.

  • Display employee list and number of employees. Note that the result

  • will be the same when called from o_whitecollar_employee1 or

  • o_bluecolarcollar_employee1, because the methods are defined

  • as static (CLASS-METHODS)

CALL METHOD o_whitecollar_employee1->display_employee_list.

CALL METHOD o_whitecollar_employee1->display_no_of_employees.

4. Events

This is the same example as example 4. All changes are marked with red. There have been no canges to the subclasses, only to the superclass and the report, sp the code for th esubclasses is not shown.

For a simple example refer to Events in Examples.

REPORT zbc404_hf_events_4 .

*----


*

  • INTERFACE lif_employee

*----


*

INTERFACE lif_employee.

METHODS:

add_employee

IMPORTING im_no TYPE i

im_name TYPE string

im_wage TYPE i.

ENDINTERFACE.

*******************************************************

  • Super class LCL_CompanyEmployees

*******************************************************

CLASS lcl_company_employees DEFINITION.

PUBLIC SECTION.

TYPES:

BEGIN OF t_employee,

no TYPE i,

name TYPE string,

wage TYPE i,

END OF t_employee.

  • Declare event. Note that declaration could also be placed in the

  • interface

EVENTS: employee_added_to_list

EXPORTING value(ex_employee_name) TYPE string.

  • CLASS-EVENTS: Events can also be defined as class-events

INTERFACES lif_employee.

METHODS:

constructor,

display_employee_list,

display_no_of_employees,

  • Declare event method

on_employee_added_to_list FOR EVENT employee_added_to_list OF lcl_company_employees

IMPORTING ex_employee_name sender.

PRIVATE SECTION.

CLASS-DATA:

i_employee_list TYPE TABLE OF t_employee,

no_of_employees TYPE i.

ENDCLASS.

*-- CLASS LCL_CompanyEmployees IMPLEMENTATION

CLASS lcl_company_employees IMPLEMENTATION.

METHOD constructor.

no_of_employees = no_of_employees + 1.

ENDMETHOD.

METHOD lif_employee~add_employee.

  • Adds a new employee to the list of employees

DATA: l_employee TYPE t_employee.

l_employee-no = im_no.

l_employee-name = im_name.

l_employee-wage = im_wage.

APPEND l_employee TO i_employee_list.

  • Raise event employee_added_to_list

RAISE EVENT employee_added_to_list

EXPORTING ex_employee_name = l_employee-name.

ENDMETHOD.

METHOD display_employee_list.

  • Displays all employees and there wage

DATA: l_employee TYPE t_employee.

WRITE: / 'List of Employees'.

LOOP AT i_employee_list INTO l_employee.

WRITE: / l_employee-no, l_employee-name, l_employee-wage.

ENDLOOP.

ENDMETHOD.

METHOD display_no_of_employees.

  • Displays total number of employees

SKIP 3.

WRITE: / 'Total number of employees:', no_of_employees.

ENDMETHOD.

METHOD on_employee_added_to_list.

  • Event method

WRITE: / 'Employee added to list', ex_employee_name.

ENDMETHOD.

ENDCLASS.

*******************************************************

  • Sub class LCL_BlueCollar_Employee

*******************************************************

CLASS lcl_bluecollar_employee DEFINITION

INHERITING FROM lcl_company_employees.

See code in example 3...

ENDCLASS.

CLASS lcl_bluecollar_employee IMPLEMENTATION.

See code in example 3...

ENDCLASS.

*******************************************************

  • Sub class LCL_WhiteCollar_Employee

*******************************************************

CLASS lcl_whitecollar_employee DEFINITION

See code in example 3...

ENDCLASS.

CLASS lcl_whitecollar_employee IMPLEMENTATION.

See code in example 3...

ENDCLASS.

*******************************************************

  • R E P O R T

*******************************************************

DATA:

  • Object references

o_bluecollar_employee1 TYPE REF TO lcl_bluecollar_employee,

o_whitecollar_employee1 TYPE REF TO lcl_whitecollar_employee.

START-OF-SELECTION.

  • Create bluecollar employee obeject

CREATE OBJECT o_bluecollar_employee1

EXPORTING im_no = 1

im_name = 'Karen Johnson'

im_hours = 38

im_hourly_payment = 75.

  • Register event for o_bluecollar_employee1

SET HANDLER o_bluecollar_employee1->on_employee_added_to_list

FOR o_bluecollar_employee1.

  • Add bluecollar employee to employee list

CALL METHOD o_bluecollar_employee1->lif_employee~add_employee

EXPORTING im_no = 1

im_name = 'Gylle Karen'

im_wage = 0.

  • Create whitecollar employee obeject

CREATE OBJECT o_whitecollar_employee1

EXPORTING im_no = 2

im_name = 'John Dickens'

im_monthly_salary = 10000

im_monthly_deducations = 2500.

  • Register event for o_whitecollar_employee1

SET HANDLER o_whitecollar_employee1->on_employee_added_to_list

FOR o_whitecollar_employee1.´

  • Add bluecollar employee to employee list

CALL METHOD o_whitecollar_employee1->lif_employee~add_employee

EXPORTING im_no = 1

im_name = 'Gylle Karen'

im_wage = 0.

  • Display employee list and number of employees. Note that the result

  • will be the same when called from o_whitecollar_employee1 or

  • o_bluecolarcollar_employee1, because the methods are defined

  • as static (CLASS-METHODS)

CALL METHOD o_whitecollar_employee1->display_employee_list.

CALL METHOD o_whitecollar_employee1->display_no_of_employees.

Result:

Employee added to list Karen Johnson

Employee added to list John Dickens

List of Employees

1 Karen Johnson 2.850

2 John Dickens 7.500

Total number of employees: 2