OOPS ABAB
OOPS ABAB
Class is a User Defined Data type having component in form of attributes and Methods, events,
interfaces and they are bundled together.
Class is combination of data Class -> Instance Variables Class -> Attributes
members + Member functions + Methods + Methods
+ Interfaces
+ Events
+ Aliases
How many different types of Class in OOPS ABAP?
Class
Global Class –
It can be accessed in any of the
Reports/FM/Methods if its visibility Local Class–
sets to Public It is created inside a report and
accessed in that report only
T-Code: SE24( Class Builder )
Global class is accessible in any Its scope is specific to the program
program where it has been written.
What is Static and Instance Attribute? VVI
90
Math=>sta_sub( exporting .. Importing )
Class MATH
10 15
INSa2
STA_Sub
STAa3
Insa
1 Insa2 Obj2
20 30
Obj1->staa3 = 20
Obj1->insa1 = 10 Obj1->staa3 = 40
Ojb1->insa2 = 15 Obj2->staa3 = 90
Obj2->insa1 = 20
static Attribute: Instance Attribute: CL_ABC
Ob1->ia1 = 4.
Ob1->ia2 = 5. WRITE: OB2->IA1 =4
ob1->sa3 = 10 WRITE: OB2->IA2 =5
ob2->ia1 = 1000. WRITE: OB2->IA3 =6
Ob2->ia2 = 2000.
Ob2->ia1 = 30000.
Ob2->sa3 = 9999.
What is Object?
Object : Instantiation of the class. New Syntax(NW7.40)
To Create Object –( Instantiation of the class ) DATA (ob1) = NEW cl_sample()
1. Reference of the class – Naming the address in memory where your object will sit :
data: go_obj1 TYPE REF TO CL_ABC.
1. Create the object ( Instantiation )- DATA(go_obj1) = NEW CL_ABC( ).
CREATE OBJECT go_obj1
Level 1
ZCL_VEHICLE.
Protected: color
Zcl_Dealer
color
Level 2 ZCL_CAR.
Protected: color
Level 3
ZCL_SUZUKI.
ZCL_A ZCL_B ZCL_C
Q. If Multiple Inheritance is not supported, is there any way that it can be achieved?
ZCL_SUZUKI.
Protected: color
METHOD RUN.
ENDMETHOD.
Output:-
ZCL_CAR.
Protected: color
METHOD DUMMY
SUPER->RUN( )
ENDMETHOD.
What are the different types of Inheritance?
ABSTRACT CLASS
INTERAFACE
ZCL_VEHICLE
ZCL_ANIMALS
ZCL_LIVING
ZCL_ORE
2. We can go for it if we know some of the 2. We can go for it if we don’t know any of
behavior( method ) in advance the behavior.
3. You can’t achieve multiple inheritance 3. Multiple inheritance with the help of
with the help of Abstract Class Interface can be achieved.
What is Constructor? Definition:
CONSTRUCTOR Special method To set the state of an Object/
And what are the different types of
Constructor? Special method To initialize the Variable of the
object/Class
CONSTRUCTOR/INSTANCE
CONSTRUCTOR/OBJECT CONSTRUCTOR
STATIC CONSTRUCTOR/CLASS
CONSTRUCTOR
What is the difference between Class Constructor and Instance Constructor?
1. It is called only one time whenever a static 1. This is going to be called every time whenever you
component of a class is accessed or object is created. create the object of the class
When the constructor is going to be called – Before object creation or After object Creation?
Ans. Before Object Creation
What is the rule for Static Constructor?
1. If any object is created then very first time class constructor will be called.
2. If in the program any static component of the class has been accessed then
while loading the event in which that code has been written which is accessing
the component.
3. Same thing is true for Subroutine and Module.
What is the syntax to access the static method in the Inside a static method can you use the instance component of a class?
program? No – Not possible
Ans.->
This is first way using the class name -> CLASS NAME => Is it possible to override(redefine) the static method in the subclass?
Static Method Name Ans -> No not possible
Data: ob1 type ref to CL_ABC.
CREATE OB1 Why We are supposed to create or use the instance component(
Ob1->Static Method method, attributes, events) over static component?
Ans. – Because we can’t use the polymorphism which is one of the
Then, when to use Static Method?
Every time we don’t require polymorphism for example in case of Factory method.
When we have to create the test program we can go for it because accessing the
method is very easier here
Polymorphism
Overloading Overriding
Class:-
Mehtod : run ->1
Method : run importing x type i. ->2
Method : run importing y type char5. ->3
Method: run importing x type I z type I
exporting y time char10. ->4
Obj->run( 5 )
Obj->run( ‘5’ )
Obj->run(5,6)
Q. Can a Class Constructor will b
Polymorphism and Inheritance inherited?
Ans. No
Q. Can a Constructor will be
inherited
->Class Constructor can never be inherited. Ans. Yes .
Q. Can a constructor will be
redefined?
->Constructor can be inherited and can not be redefined but a subclass Ans. No.
can have its own constructor but must use the SUPER-CONSTRUCTOR.
Q. Can a subclass have its own
constructor defined?
Ans. Yes.
If a class A is friend of Class B, then Class A can access the Protected or Private component ( attributes,
Methods and Events)
Of Class B.
What is the major / Important / basic / fundamental difference between Friend Class
and Subclass.
Report zdummyloaddemo.
class zcl_eb22_vehicle DEFINITION load.
" Whenever you are going to access the static component of the class
zcl_eb22_vehicle=>test_static( ).
Ans. Just to let the compiler know that the used class is somewhere declared later in the program.
Q. What is Interface?
Q. When you create the Object of the Subclass in below scenario in what sequence call will take place?
Ans. UML: This is standard pictorial/graphical representation of denoting the components of a class and its
relationship between one or more than One Classes.
Cl_Anilmal
Attributes(Color,
Category)
Methods(run, sleep)
Cl_cow
+ total_milk
Methods(eat_grass)
How to represent Class via UML diagram. What is the meaning of symbol “+”, “-” and “#” and “________”
Ans – Below is the detail
# Protected
# animal Type
+ set foot ( )
+ get foot ( )
CLASS METHODS
- run ( )
# awake ( )
What is association relationship, aggregation relationship and composition relationship in OOPS
Ans. – Association: is a USING relationship where all objects have there own lifecycle and there is no owner. E.g. (
Relationship between Teacher and Student, Doctor and Patient etc.
Aggregation: is a HAS A relationship which is specialized form of Association. Objects have their own lifecycle
but only one of the objects will be owner. E.g. Teacher and School, Company and employee, District and people(District
Magistrate) etc.
Composition: is a MUST HAVE relationship which is again a specialized form of aggregation where one object
life cycle is completely dependent on another object. E.g. Building and Room, Plane and Wings, Body and Mind etc.
ASSOCIATION
AGGREGATION
COMPOSITION
Casting
Data: lv_age_i type int4.
Data: lv_age_c type char5.
Narrow Casting: Assigning the object of a sub class Wide Casting: Assigning the object of lv_age_i = Lv_age_c
to The reference of the super class is known as Super class to the sub class
Narrow Casting Condition: Before performing the Wide
Casting we must have to perform the
Narrow Casting
?= Lesser Perspective animal
Animal NARROWER
Ob_animal = ob_cow”narrowcasting.
Ob_cow ?= re_ob_Animal “widecasting
Cow
Q1. What is the different types of casting in OOPS in terms of inheritance(i.e. Super Class vs Sub
class)( VVI )
Ans. Assigning the object instance of super class to the subclass. i.e. you
are going from lesser perspective to wider perspective
Classical Report:
LOAD-OF-PROGRAM
INITIALIAZATION
AT SELECTION SCREEN
AT SELECTION SCREEN OUTPUT
Interactive report:
AT USER COMMAND
AT LINE SELECTION
Module Pool:
PAI
PBO
POH
POV
Events
An event is an action used for providing dynamic features to applications.
1. Create an Event
2. Create a Method to handle the Event( Event Handler Method)
3. Set up the linkage between Action and Event handler method
4. Register the Event
5. RAISE the Event in a separate Method
6. Create Program and from there call the method in step 5
Functionality
ACTION (Method)
(Event) HITTHEDOG BARK()
Event Handler Method
Method
Raise EVENT
RAISE HITTHEDOG
Event Method
Ans. -> We must have to register the method with the events
SYNTAX(EVENT):
EVENTS: hit_the_dog EXPORTING VALUE(im_event) TYPE i .
SYNTAX:
SET HANDLER go_event_handler->weeping FOR ALL INSTANCES .
Note: FOR ALL INSTANCE addition is only valid for INSTANCE EVENT
Sender parameter
How to Register:
SET HANDLER go_event_handler-
>barking FOR ALL INSTANCES ACTIVATION abap_true.
Note: If you don’t specify ACTIVATION abap_true then also no problem. By default it takes.
One Event : Multiple Handler Method
• One event can have multiple handler method.
• It gets executed in the same sequence in which it is registered.
Click
Method2
STATIC EVETNS
Static Events Can be raised inside the Instance Method or static Method.
Main Difference:
While registering the handler we have to use only
Q1. Can u raise the static event inside the instance method? Yes
Q1. Can u raise the Instance event inside the instance method? Yes
Q2. Can u raise the instance event inside the static method? NO
Q4. Does the handler method depend on Event type( Static Event or Instance Event ). No
EVENTS
Parameters:
Registration 1. SET HANDLER <HANDLER Method> for 1. SET HANDLER <HANDLER METHOD>
<object> /<ALL INSTANCES>
Method 2. Only inside the instance Method 2. Inside the instance method and Static
In which type method
of the method
event will be
raised?
3. Supported 3. Not Supported
Sender
reference
What are the Different types of Exception?
EXCEPTION
CX_SY_ZERODIVIDE
EXAMPLE: MYOWNEXCEPTION
CLEANUP.
AJK
AJKLDSFJL
LJKALDSF
ENDCLEANUP.
What are the different types of Object Services ?
Object Services
ZCL_* OR YCL_*
PROTECTED
ZCA_DEMO
You require a database which is going to save the data.
DML
ZCL_EMPLOYEE
Id, ZCL_LOGGER O_LOGGER-
Name SET_DATA( ): >PRINTLOG()
ZCL_LOGGER PRINT_MESSAGE( ):
It is used to hide the complexity of object creation and produces the object on
demand
Design Pattern
Adapter Design
Singleton Design Interpreter Design
Bridge
Factory Design Pattern Iterator design pattern
Composite
Singleton Factory Design Mediator
Pattern Decorator
Observer Design Patter
Object Pool Façade
Visitor Design Pattern
Prototype Flyweight
Command Design Pattern
Abstract Factory Proxy
State Design Pattern
MVC – Model view Controller
• Sometimes you need to delay the instantiation of the object because it may be very costly to instantiate as it
may take large amount of resources as memory and CPU without having any fruitful result. It will take extra
time as well
• Why we don’t get fruitful result because we may have bad data, which may further stop processing.
• Hence it’s always better to first validate all the required data in a separate class
• This another class is known as proxy class because it gets executed first, in stead of actual class.
➢ Proxy class is light weight class. Very- very heavy class- excel,
➢ It hold the implementation of validation logic outlook,
➢ If validation is successful then only go and create the sap heavy class.
actual/heavy class object and execute the functionality.
Proxy Design Pattern Implementation
Generally proxy design pattern is used in ABAP when we have to instantiate the NON ABAP object.
Because, NON-ABAP objects are very heavy ( e.g. EXCEL object and OUTLOOK object )
Adapter Design Pattern
1. Adapter converts the objects which are incompatible due to the difference in the interface. By
Implementing the Adapter, we can allow classes to work together, which can’t work without Adapter.
2. Sometimes we come across the scenario where client expects to have certain interface. Already we have an
object which is satisfying the client need then why to create the another object. We can create a wrapper
and use it.
You already have a function module to sum( say ), and your client came
up with need to pass the parameters – How to do?
Adapter Design Pattern Implementation
Caller Interface(Target)
Adapter Adaptee
Facade Design Pattern
Consider a scenario where your team is developing a big application. Application have been
divided into different parts and assigned to different DEVELOPERS.
Now suppose , every one has to handle the message in that application different languages.
So here everyone would end up with there own function module or methods for displaying the
message or capturing the message.
So what is solution: Façade : So Team can decide to create a single function module / Method
which will handle the message and this FM/method can be used by everyone. Now tomorrow if
some extra addition need to be done related with message, that single FM will be the place where
one can go and change and communicate to other team members.
Façade Design Pattern
Complex logic
Source: Internet(sapignite.com)
Façade Design Pattern -> Google.come
Template Design Pattern
Interface
Abstract Class
Class A Class B
Interface ZIF_VEHICLE_FUNCTION ZCL_VEHICLE: ( ABSTRACT CLASS,
Template Design Pattern CONCRETE CLASS OR INTERFACE )
1. RUN( )
2. ACCELARATE( )
ABSTRACT CLASS ( ZCL_VEHICLE )
3. SPEED( )
4. SOUND( )
MAIN
Decorator ( Decoration )
ALV OUTPUT
STD_ALV CLASS DECORATOR CLASS PDF OUTPUT
DISPLAY_OUTPUT DISPLAY_OUTPUT( )
PDF CLASS
DISPLAY_OUTPUT
Decorator Design Pattern
1. When we require to extend the behavior of certain objects at run time then we
use the Decorator Design Pattern
2. This can be separate than the existing instances of the same class
3. This can be achieved by creating certain environment( Infrastructure ).
1. Identify the common method and put inside the Abstract Class
2. Create a Decorator Class – This is nothing but subclass of the main class. This is
also called component
3. In the subclass( i.e. in Decorator Class ), create an attribute with type ref to Main
Class
4. Create a constructor in Decorator Class with importing parameter REF TO Main
Class. Set the attribute from this parameter
5. Redefine all other methods in decorator class inherited from Main Class. Just call
the same method using the reference of the main class
6. Create subclass of the decorator when a new behavior is required
7. Redefine the methods
8. Write the main logic using a helper variable of type ref to Main Super Class
Observer Design Pattern
Subject
Tablet Device
Weather Station Observer Class ON_TEMP_CHANGED
METHOD: TO TRIGGER THE ON_TEMP_CHANGED
EVENT
EVENT:
TEMPERATURE_CHANGED
Mobile Device
ON_TEMP_CHANGED
Observer
Composite Design Pattern( Structural Design Pattern)
Me
My Father My Mother
Grand-pa
Grand-ma Grandma
Grandpa
Computer(Composite Node )
Composite
Node Monitor Mouse
Cabinet
Leaf Node Leaf Node
Cabinet
setPrice( )
getPrice( )
Hard Disc
getPrice CPU MotherBoard
setPrice( ) Get_Price
IT_HD_COMP setPrice( )
ram
garphics
RAWMAT SOFTWARE
What is Strategy design pattern?
Spearman Archer
Attack( ) Attack( )
Refill( ) Refill( )
Robot
Palladian
Archer Gun Man Spearman
Spearman Spearman
Attack( ) Attack( ) Attack( )
Attack( ) Attack( )
Refill( ) Refill( ) Refill( )
Refill( ) Refill( )
Repair( )
Refill -> Pause for 5 seconds Repair -> Repair the Weapon
Gunman –> Time based feeling Robot –> External Repair
Archer -> Count based feeling Iron Man -> Internal Repair
Strategy Pattern Soldier If_refill_weapon If_repair_weapon
Attack( )
Refill( )
Repair( )
Palladian Robot
Archer Gun Man
Spearman Spearman Attack( )
Attack( ) Attack( )
Attack( ) Attack( ) Refill( )
Refill( ) Refill( )
Refill( ) Refill( ) Repair( )
Refill -> Pause for 5 seconds Repair -> Repair the Weapon
Gunman –> Time based feeling Robot –> External Repair
Archer -> Count based feeling Iron Man -> Internal Repair
If_refill_weapon If_repair_weapon
Strategy Pattern Soldier
refill( ) repair( )
Attack( )
Refill( )
Repair( )
Refill -> Pause for 5 seconds Repair -> Repair the Weapon
Gunman –> Time based feeling Robot –> External Repair
Archer -> Count based feeling Iron Man -> Internal Repair
Soldier
Strategy If_refill_weapon If_repair_weapon
Attack( )
Pattern
Refill( )
Repair( ) Count_based_refill Internal_repair External_repair
Time_based_refill
Refill -> Pause for 5 seconds Repair -> Repair the Weapon
Gunman –> Time based feeling Robot –> External Repair
Archer -> Count based feeling Iron Man -> Internal Repair
Strategy Pattern: Conclusion
1. Identify the changeable variable and take it out and put in an interface or Abstract Class
2. Encapsulate in family of algorithm separately in concrete classes
3. Client should be using these interface as references. Client should not implement it
Context/Client Class
ZCL_SOLDIER
-GO_REFILL : ZIF_REFILL_WEAPON
-GO_REPAIR: ZIF_REPAIR_WEAPON
<<interface>> <<interface>>
ZIF_REFILL_WEAPON ZIF_REFPAIR_WEAPON
+Refill() +Repair()
Single-responsibility principle
Open-closed principle
Liskov substitution principle
subclass Interface segregation principle
Dependency inversion principle
Super class
SOLID This was given by author - Robert C. Martin ( Uncle BOB ) in Year 2000.
Definition:
This is very popular set of design principles which guides while developing a software in OOPS, what are
the important guideline need to be followed for better maintainability and reusability and flexibility. INTERFACE
It is mnemonic acronym for below set of principles - METHOD A,
S -> Single Responsibility Principle (SRP )
A class should have one and only one reason to change
A
O -> Open-Closed Principle
Open for Extension and Closed for Modification
RENAULT_CAR
KWID DUSTER
Open Closed Principle
The principle says that any class must be directly replaceable by any of its subclasses without any error.
Link: https://www.youtube.com/tch?v=ObHQHszbIcE
The Liskov substitution principle (LSP) is a specific definition of a subtyping relation created by Barbara Liskov and
Jeannette Wing.
D: Dependency inversion principle
Control in general:
✓ Controls are independent software component embedded in SAP GUI.
✓ SAP supports ACTIVEX Controls for MS Windows platforms and JavaBeans for the SAP GUI in the Java environment.
✓ Frequent data exchange on from Backend to Frontend and vice versa increase the load on the network and gives the bad experience
for user due to slowdown of the application
✓ Hence if most of the work is going to done at frontend only then control becomes useful.
Controls framework:
✓ It was introduced in Release 4.6
✓ CFW encapsulates all the controls in global classes of ABAP objects.
Controls
Application
control
Container Controls How to use:-
1. Create custom container in screen painter (say CONTAINER )
2. Pass the custom container name(CONTAINER) to create the object of the class
CL_GUI_CUSTOM_CONTAINER
CL_GUI_CUSTOM_CONTAINER. Say object name is O_CUST_CONTAINER
3. Call the application control object method by passing the custom control
object(O_CUST_CONTAINER) as parent
How to Use:-
1. While creating the object of this class must need to be linked with any one of the
CL_GUI_DOCKING_CONTAINER
edge of the screen.
2. That edge is now become Docker and can be used for the purposed of any
application control
Note: No Screen painter is required.
CL_GUI_CFW=>FLUSH
GD_LAYOUT-INFO_FIELDNAME = ‘ROW_COLOR'.
Case 3. Check the next Slide
Case 3. Color at cell Level
step 1. Include a field CELLCOLOR of type LVC_T_SCOL at structure in last.
Step 2. Inform to the program by setting the COLTAB_FIELDNAME attribute of LAYOUT that
the extra field CELLCOLOR is for the purpose of setting the color at cell level.
Do as below -
layout-COLTAB_FIELDNAME = 'CELLCOLOR'.
Step 3. Populate the final internal table where corresponding CELLCOLOR field need to
populated accordingly.
1. What is the Priority of the color for below Row Color, Column Color and Cell
Color
Step1. Define one extra column(CHECKBOX_FIELD) of CHAR1 which will be used for check
box