A class is the generic word that will be used to describe the skeletton of an object of the said class.
A class defines ATTRIBUTES and BEHAVIORS.
A set of VALUATED ATTRIBUTES defines a STATE.
The BEHAVIOR alters/change this STATE.
A BEHAVIOR is equivalent to saying METHOD. Let's assume that both wording will be used now.
See the class as the highest level word to describe what will be introduced next
Now let's dig deeper into what's beyond that generic word.
An ABAP class is composed of 2 parts.
- Definition
- Implementation
- Definition section will be the part where we’ll declare the collection of attributes and behaviors.
- We will also specify the VISIBILITY of each attributes and behaviors.
But now, let's just start slowly and focus on this code.
CLASS lcl_flight DEFINITION.
ENDCLASS.
Pretty understandable syntax, right ?
Let's take a moment to discuss what it says.
This declares a local class (LCL_flight) named lcl_flight. See naming conventions.
All the code that will be declared between the two keywords CLASS and ENDCLASS will belongs to LCL_FLIGHT.
- Remember that in definition, we said that it defines the collection of behaviors.
- Defining here only resumes to listing the possible behaviors.
- To give a real body to the behavior, we need to implement it, code it.
Implementation is where all the coding is done.
Can you guess the syntax ? Pretty dull, huh ?
CLASS lcl_flight IMPLEMENTATION.
ENDCLASS.
Now, the interesting part begins.
Visibility management is the process to defines what will be accessible/visible from outside the context of our class.
In DEFINITION part, the user define the visibility:
A public attribute:
- is addressable WHATEVER the referential
- can be changed from outside
- an additional option READ-ONLY forbid it to be changed from outside
A public method:
- is usable whatever the calling referential
Good Practice: To protect attributes from external access, it’s recommended to set them as PRIVATE
A private attribute is visible:
- is ONLY addressable from the class referential
A private method:
- is ONLY usable within the class referential
Good Practice: It’s recommended to give access only to method giving a result. The internal process shall remain hidden to the external access
Let's make it visual
CLASS lcl_flight DEFINITION.
PUBLIC SECTION.
PRIVATE SECTION.
ENDCLASS.
CLASS lcl_flight IMPLEMENTATION.
ENDCLASS.
What you have above is the beginning of a successful ENCAPSULATION
The encapsulation is based on hiding the definition of a class to the other components of the system.
Thus, these components cannot speculate on the internal state andd behavior of an object of this class or of the class it-self.
The class is the shell enveloping its attributes and behaviors.
The principle of visibility is the warranty to hide or show these implementations and solely expose what the external components have to interact with
- Public - green
- Protected - orange
- Private - red