Class basically define how an object would look like and what it can do – structure and functionality.
Let’s talk about a product – Toys. We have a class called ‘Toys’ and it intends to define any toy created by a major player. Structure will define what different properties of the toy like material, intended age group etc. Functionality will define functionalities that can be associated with the object like printing of an invoice.
Class has two components –
- Attributes – it contains data an object will contain when it is defined -> Structure
- Methods – functionalities associated with the object -> Functionality
These two components are defined and implemented separately; either locally or in SE24 – Class Builder
CLASS <class_name> DEFINITION.
…
ENDCLASS.
CLASS <class_name> IMPLEMENTATION.
…
ENDCLASS.
In Class definition attributes are defined as –
- Private Section – Components accessible only to method of the class
- Protected Section – Components accessible to all methods of the class or classes which inherit that class
- Public Section – Components accessible to all users
In the example of Toys, Material might be a public data, Material’s buying price will be a private data. Classification of data is very important in OOP. It is pivotal in hiding any data that might be sensitive in that context.
In SE24, create a class by following below steps –
- Goto SE24
- Create Interface ZIF* as Object Type
- Goto Methods tab and add a method. Then, in that method, define parameters.
- Save and activate.
- Goto SE24 again
- Create Class ZCL* as Object Type
- Goto interfaces tab and put the ZIF* (from previous step) as the interface. The methods will be automatically copied.
- Activate.
The functionality of an objects of a particular class are stored in a method. It has three types of parameters –
- Importing – parameters which bring data into a method
- Exporting – parameters which passes data out of method
- Changing – parameters whose values are expected to change in a method
For example – in printing an invoice, main program would import data like cost and area code based on which tax would be calculated. Basic material description might be passed as a changing parameter. That description will be used to print label in another method. Invoice parameter would like to change the description based on area code and some defined logic. In changing parameter, it will be changed so that invoice and label are in sync. Final invoice details including Selling Price would be exported out of the method.
CLASS <class_name> DEFINITION.
METHODS <method_name> IMPORTING <variable> <data_definition>
EXPORTING <variable> <data_definition>
CHANGING <variable> <data_definition> .
ENDCLASS.
CLASS <class_name> IMPLEMENTATION.
METHOD <method_name>
…
ENDMETHOD .
ENDCLASS.
In the aforementioned example, think of a Teddy Bear (#RespectTeddyRoosevelt). That’s an object. When we create an object, we basically create an instance of the class.
DATA <object_name> TYPE REF TO <class_name> .
CREATE OBJECT <object_name> .
Notes:
Static Attribute – An attribute which is shared across different objects based on a call. It doesn’t initialize with instantiation of an object and can be accessed without creating an object. When you change its value at any point, its value in all the objects change automatically.
Class-methods – Method which access static attributes. An object is not needed to call a Class-method.
Functional Methods – methods which return only single value using Returning in the interface.
~S
Recent Comments