You are on page 1of 48

Preface

In the word of programming Object Oriented Programming is not a new thing, but as far as ABAP is concerned its somewhat new. Popular programming languages such as C++ and JAVA are all object oriented, then why not ABAP? The present material is an answer to the question. OOP is an advanced approach to programming and it has many key features, which makes program management as well as maintenance easier. It was felt that comprehensive study on OOP is required before one could jump into it. This material has been carefully prepared so that it covers all the aspect of object oriented features, the material has been divided into chapters so that its more or less like a book. Abap codes have been added, wherever it was felt necessary, examples have been provided to make better understanding of the subject matter. Although the material has been revised several times, but still some mistakes might creep in. I would be very thankful if any one points out those. And last of all I would like to say that your criticism and comments on the matter would provide me a valuable feed back for its further improvement.

Sudip Das

Acknowledgment
I would like to thank all those who have provide the support to bring out this material. I would like to thank my senior colleagues Mr Sauti Sen , Mr Sandip Mondal for their encouragement ,without which this would have not been possible . I would like to thank my colleagues Mr Samagata Das ,Mr Ranodeep Bannerjee for their valuable suggestion on the subject matter. And last but not the least I would like to thank my friends Mr Rajesh Sarkar , Mr Amardip Ghosh whose continuos support have helped me to develop this material.

Contents Chapter1

1. Introduction to OOP 2. Need for OOP

Chapter2 (Fundamentals of Class and Objects)


1. 2. 3. 4. 5. 6. 7. Introduction to classes Components of class Component visibility Assigning object reference and creating objects Introduction to methods Constructor methods Parameterized constructor

Chapter3 (A closer look into class and method)


1. 2. 3. 4. 5. 6. 7. Method that import parameters Method that export parameter Passing internal table as method parameter Understanding the concept of static and instance How to declare static and instance components Static and instance constructor Overview of abstract and final class

Chapter4 (Events)
1. 2. 3. 4. 5. 6. 7. 8. 9.

Fundamentals of events Creating an event Event handler method Setting the handler for the event(registering the event) Dynamic registration and de-registration Raising the event Event parameters Event and error handling Object lifetime and event.

Chapter5 (inheritance)
1. 2. 3. 4. 5. 6. 7. Fundamentals of inheritance Creating multilevel inheritance Member access and inheritance Method redefinition Concept of super and final Use of super for method call Constructor and inheritance

Chapter 6(Interface Polymorphism and inheritance)


1. 2. 3. 4. 5. 6. 7. Introduction to interface Class and interface Interface reference and component access. Reference variable Concept of polymorphism Polymorphism via interface Polymorphism via inheritance

Chapter1(Introduction to object oriented programming in ABAP.)


In the world of programming languages, there has been a lot of development, as far as the programming concept is concerned. And object oriented programming approach is one of them. The programming approach that is usually followed is procedure oriented approach, which stress more on doing things i.e. executing the instruction set. This approach is ok, if the program is simple, but if program size increases and the program complexity is high, then we need to find some better alternative, and the result of the search is OOP. OOP is one of the most advanced programming approaches. The programming approach ensures better program management, reusability data encapsulation and abstraction and extensibility. That we cannot have in procedure oriented programming.

Introduction.

1. 2. 3. 4.

Procedural languages (drawback)

Program maintenance as well as management becomes difficult as program size increases. Data is undervalued Very difficult to model real word entity. Lack of extensibility.

1. Object-oriented programming is an approach that provides a way of modularizing programs by creating patron memory area for both data and function that can be used as templates for creating copies of such modules on demand 2. Model real world entity very well (class and objects). 3. Stress on data as well as functionality 4. Data encapsulation and abstraction 5. Inheritance 6. Polymorphism 7. Dynamic binding

Object oriented approach (key features)

Comparison between OOP and procedure


Procedure (Comparison) Emphasis on doing things Large programs are divided into smaller programs known as functions Most of the functions share global data Data move openly around the system from function to function Top-down approach OOP (comparison) Emphasis on data rather than procedure Programs are divided into objects Data structures are designed such that they characterized the objects Functions that operate on the data of an object are tied together in the data structure Data is hidden and cannot be accessed by external functions New data and functions can be easily added whenever necessary Bottom-up approach Lets be more specific -> OOP and abap 1. to be at par with the others 2. To understand the recent concepts of abap ex bapi, badi workflow. 3. For interfacing abap with Microsoft technologies ,Java as all these are Build on the concept of OOP 4. To exploit the object resource that has been provided by SAP.

Benefits of oop (summary)

1. Through inheritance inheritance we can eliminate redundant code and extend the use of existing class 2. We can develop programs form the existing classes in given by SAP(further details) 3. The concept of data hiding helps the programmer to build secure programs that cannot be invaded by code in other parts of programs 4. OOP system can be easily upgraded from smaller system to larger system 5. Software complexity can be easily managed.

Chapter 2(Fundamentals of class and objects)

1. 2. 3. 4. 5. 6. 7.

Introduction to classes Components of class Component visibility Assigning object reference and creating objects Introduction to methods Constructor methods Parameterized constructor

Introduction to class

The main objective of OOP is to model real world entity, each real world entity can be modeled by its characteristics and functionality .The real world entity when modeled into OOP world is known as Class, characteristics as attributes and functionality as methods. A class has two parts definition part and implementation part. In the definition part the definition of the class components as well as visibility of components is done. And in the implementation usually the method implementation is done. Now the question comes what is an Object then? Well the answer to this is, Objects are instance of a Class or in other words they are run time entities, in an object-oriented system.

Components of class

As we know Class is a modeled real world entity it must have some characteristics as well as functionality .The characteristics are known as attributes in OOP terminology, they are basically different data types. The functionality is known as methods and events, methods and events reflect the capabilities of a class. Methods are functions which can be called by the instance of a class (i.e. objects) where as events are called automatically when certain sets of conditions (for that particular event are fulfilled). . Other possible components can be interface, structure internal table etc.

Component visibility

This is one of the most important concepts of OOP, here we provided with the flexibility to assign visibility of the class components. But before we proceed any further lets clear the fact what do we mean by visibility of class components? The components of a class can only be accessed through the objects but declaring the class components in different domains, which are defined as Public Private and Protected, these domains control the accessibility.

Public components of a class can be accessed by the objects of a class components in the public domain are totally visible outside the class other components within the class can also access them. Private components cannot be accessed by the objects of the class they can only be accessed by the members within the class i.e. any member function within the class can access the private members thus from the point of visibility no private components are visible outside class. The sequence of declaring the components are public protected and private.

Example

CLASS my_class DEFINITION. PUBLIC SECTION. METHODS: test_method. PROTECTED SECTION. PRIVATE SECTION. DATA: name TYPE C. ENDCLASS.

CLASS my_class IMPLEMENTATION. METHOD test_method. Write:/ this is my test method. ENDMETHOD. ENDCLASS.

Assigning object reference and creating objects.

As we know objects are runtime replica of classes ,they are real runtime entities of the class, and come into picture during program execution. Objects occupy memory space . An object cannot exist by itself it must have a class reference ,every object has the class reference stored in a variable ,and we call it object name ,with the help of this reference we can access the class components . Lets see how we can do it . DATA: my_obj_ref TYPE REF TO ref_class_name. The TYPE REF TO statement helps us to assign the object reference . Objects can be created using CREATE OBJECT statement. CREATE OBJECT: my_obj_ref. Example.

CLASS test DEFINITION. PUBLIC SECTION. METHODS test_method. ENDCLASS. CLASS test IMPLEMENTATION. METHOD test_method. WRITE:/ this is my method. ENDMETHOD. ENDCLASS. *************** main program ************** START-OF-SELECTION. DATA: obj_ref TYPE REF TO test. object reference is assigned CREATE OBJECT: obj_ref. object is created CALL METHOD Obj_ref->test_method. object method is called using the reference. END-OF-SELECTION.

Introduction to methods

Methods are the functionality of a class , ABAP codes are written within a method to incorporate the functionality. Methods can be both public as well as private .Public methods can be accessed by the objects of the class to which the method belongs to. Private methods can only be accessed by methods that are within the class. Methods can import as well as export parameters. As said earlier there can be instance method and static method Other kind of methods are constructor method and event handler methods which will be covered later.

Constructor methods

Constructor methods are special method that are implicitly associated with every instance of a class .They are implicitly defined by the system .Constructor methods are called automatically whenever an object is created .However the constructor methods can be explicitly defined by the programmer .We can define the number of input parameters for the constructor .A point to note a constructor method cannot return any value. The name of the constructor method is implicitly defined by the system as CONSTRUCTOR and it cannot be changed . To use a constructor method i.e to incorporate some features , we need to explicitly define it in class definition part .There are two types of constructor method

Static and instance constructor , which will be covered later. Example. CLASS my_class DEFINITION. PUBLIC SECTION. METHODS CONSTRUCTOR. note constructor should be always declared in public. ENDCLASS. CLASS my_class IMPLEMENTATION. METHODS CONSTRUCTOR. write :/ this is constructor method. ENDMETHOD. ENDCLASS.

Parameterized constructor

Like normal methods constructor methods can import parameters ,but since the constructor methods cannot be called like normal methods ,so how do we pass the parameters to constructor methods?? We can pass the parameters to a constructor methods during the object creation i.e while using create object statement. A point to note a constructor cannot export any parameter. Example CLASS test DEFINITION. PUBLIC SECTION. METHODS: print_private_data, CONSTRUCTOR IMPORTING num TYPE I. constructor importing parameter PRIVATE SECTION. DATA: private_num TYPE I. ENDCLASS. CLASS test IMPLEMENTATION. METHOD print_private_data. WRITE:/ private_num. ENDMETHOD. METHOD CONSTRUCTOR. this constructor initializes the private member with the private_num = num. constructor parameter

ENDMETHOD. ENDCLASS.

************* main program ****************************** START-OF-SELECTION. DATA: my_obj_ref TYPE REF TO test. CREATE OBJECT : my_obj_ref EXPORTING num = 2. CALL METHOD my_obj_ref->print_private_data. END-OF-SELECTION.

Chapter 3(A closer look into class and methods.)

1. 2. 3. 4. 5. 6. 7.

Methods that import parameters Method that export parameter Passing internal table as method parameter Understanding the concept of static and instance How to declare static and instance components Static and instance constructor Overview of abstract and final class

Methods that import parameters

Methods like functions or forms can import parameter as well as export parameter. In this section we will show an example that import three parameters. CLASS test DEFINITION. PUBLIC SECTION. METHODS: test_method IMPORTING number1 TYPE I Character1 TYPE C Variable TYPE I. ENDCLASS. CLASS test IMPLEMENTATION. METHOD test_method. Write:/ number1,20 character1,30 variable1. ENDMETHOD. ENDCLASS.

Method that export parameter

In this section we will show how a method can export parameter. CLASS test DEFINITION. PUBLIC SECTION. METHODS: test_method IMPORTING number TYPE I. ENDCLASS. CLASS test IMPLEMENTATION. METHOD test_method. Write:/ number. ENDMETHOD. ENDCLASS.

Passing internal table as method parameter.

As we have seen in the earlier section that a method can import as well as export parameters, but this is not all it can import as well as export structures internal table etc

like forms .In this section we will show how to pass an internal table as parameter to a method. A point to note the internal table that is to be passed to the method should not have header line as this is against the concept of OOP. While defining the method it is necessary to give the type of the internal table to be passed; we can give the type as any predefined structure of the database dictionary. But it is convenient to keep the type declaration more generic, because when we pass the actual internal table the whole internal table is passed into the method, and the method takes the structure of the internal table that has been actually passed .To keep the type declaration generic, we declare the type as any. CLASS test DEFINITION. PUBLIC SECTION. METHODS: test_method_itab EXPORTING itab TYPE ANY TABLE. ENDCLASS. CLASS test IMPLEMENTATION. METHOD test_method_itab. **** wa is a work area it must be declared in the main program or it should be any ****dictionary structure. Loop at itab into wa. Write:/ wa-field_1,20 wa-field_2. Endloop. ENDMETHOD. ENDCLASS. Example In this example the things that were covered in the earlier sections have been summarized. This is a full working program which , shows how a method can import as well as export parameters, it has been also show how internal table can be passed into a methods and how to process the internal table in the method . CLASS test DEFINITION. PUBLIC SECTION. METHODS: read_data IMPORTING num1 TYPE I num2 TYPE I RETURNING value(result1) TYPE I , print_itab exporting itab1 type any table. PRIVATE SECTION. DATA:NUMP1 TYPE I, NUMP2 LIKE NUMP1, RESULT LIKE NUMP1. ENDCLASS.

CLASS test IMPLEMENTATION. METHOD read_data. nump1 = num1. nump2 = num2. result1 = nump1 + nump2. ENDMETHOD. METHOD print_itab. LOOP AT ITAB1 into Wa WRITE:/5 wa-NAME,20 wa-ROLL,30 wa-HALL. ENDLOOP. Clear wa. ENDMETHOD. ENDCLASS.

**************************MAIN PROGRAM***************************** TABLES:ystudent.

TYPES:BEGIN OF STRUCT, NAME LIKE YSTUDENT-NAME, ROLL LIKE YSTUDENT-ROLLNO, HALL LIKE YSTUDENT-HALL, END OF STRUCT. DATA:ITAB1 TYPE STANDARD TABLE OF STRUCT INITIAL SIZE 0, worka LIKE LINE OF ITAB1. PARAMETERS:num1 TYPE I, num2 TYPE I, result TYPE I. DATA:test TYPE REF TO test. CREATE OBJECT test TYPE test. START-OF-SELECTION. WRITE:/5 num1, /5 num2. SELECT: NAME ROLLNO HALL FROM YSTUDENT INTO TABLE ITAB1 WHERE HALL =

'RP' result = test->read_data( num1 = num1 num2 = num2 ). CALL METHOD test->print_itab importing itab1 = itab1. ULINE. WRITE:/5 result. END-OF-SELECTION. *************************MAIN PROGRAM ENDS*******************

The class components that have been declared fall under two category static and instance. Now what do we mean by this actually? As we know class is only a logical modelling of an object .It does not occupies any memory space during program execution only the instance of an class (objects) occupies memory space. We know a class can have multiple instance i.e multiple object can refer to same class. And in oop separate memory are allocated for all different objects and these memory area are not shareable by other objects . But it is possible to have only one common memeory area for all instance of a class. The class components that share a common memory area for all the class instance are static components. The class components that have separate memory area for separate instance are instance components.

Understanding the concept of static and instance

Object1 Of class

Object2 Of class

Object1 Of class

Object2 Of class

Memory for object1

Memory for Object2

Memory for object1 and object2 Static

Instance

Schematic diagram to show the concept of static and instance.

A point of note that a class can have static as well as instance components. Only static methods can access static attribute and static event can be raised by static methods and the same is true for instance methods attributes and events

How to declare static and instance components


To declare instance components we declare them as follows: CLASS test DEFINITION. PUBLIC SECTION. METHODS:. ***/instance method . DATA : .. ***/instance attribute EVENTS:.. ***/instance events .. ENDCLASS. To declare static components we declare them as follows: CLASS test DEFINITION. PUBLIC SECTION. CLASS-METHODS:.. ****/static method . CLASS-DATA:. ***/static attribute . CLASS-EVENTS:.***/static events. ENDCLASS.

Static and instance constructor.


As we know c onstructors are special methods that cannot be called using CALL METHOD. Instead, they are called automatically by the system to set the starting state of a new object or class. There are two types of constructors - instance constructors and static constructors. Constructors are methods with a predefined name. To use them, you must declare them explicitly in the class. The instance constructor of a class is the predefined instance method CONSTRUCTOR. You declare it in the public section as follows: METHODS CONSTRUCTOR IMPORTING.. [VALUE(]<ii>[)] TYPE type [OPTIONAL].. EXCEPTIONS.. <ei>. and implement it in the implementation section like any other method. The system calls the instance constructor once for each instance of the class, directly after the object has been created in the CREATE OBJECT statement. You can pass the input parameters of the instance constructor and handle its exceptions using the EXPORTING and EXCEPTIONS additions in the CREATE OBJECT statement. The static constructor of a class is the predefined static method CLASS_CONSTRUCTOR. You declare it in the public section as follows: CLASS-METHODS CLASS_CONSTRUCTOR. and implement it in the implementation section like any other method. The static constructor has no parameters. The system calls the static constructor once for each class, before the class is accessed for the first time. The static constructor cannot therefore access the components of its own class.

Overview of abstract and final class


In class definition we have some more variation. The two variations are abstract and final, if we add ABSTRACT and FINAL to the METHODS and CLASS statements then it allows us u to define abstract and final methods or classes. Lets discuss what are all these , if we declare a class as abstract then that class is not implementable as well as it cannot be instantiated, but the what is the use of such class ?? Well this type of class are used in the concept of inheritance, the abstract class is only implementable in its sub class.

Lets come to final, what is final?? These is also used with inheritance if we want to prevent a class from being inherited then we do it by adding the word final to class definition final class cannot be inherited and its methods cannot be redefined in the subclass.

Chapter 4 (Events)

1. 2. 3. 4. 5. 6. 7. 8. 9.

Fundamentals of events Creating an event Event handler method Setting the handler for the event (registering the event) Dynamic event registration and de-registration of handler method Raising the event Event parameters Event and error handling Object lifetime and event.

Fundamentals of events

Events are like one of the class components, events are triggered when certain set of conditions is met, and unlike normal method they cannot be called using the call method statement. Events denote the state of an object, that is what is the current status of an object, in laymans word events adds the dimension of time to the modeled entity. Events enable objects or classes to trigger event handler methods in other objects or classes. Whereas a method can have any number of callers in regular method calls, any number of event handler methods can be called by triggering an event The life of an event consists of four stages. Creating an event Creating an event handler method for the event Registering the handler method to the event Raising the event Few points to note: Events have a parameter interface similar to methods; however, they have only output parameters, no input parameters. These parameters can be passed to the event handler methods by the triggering object/class (statement RAISE EVENT). The event handler methods use them as input parameters.

Events can be created like normal class component CLASS test DEFINITION. PUBLIC SECTION. EVENTS: test_event. ENDCLASS.

Creating an event

Event handler method.

These are like normal methods, the functionality of these methods are more specific i.e. these methods are only executed when the event associated with these methods are triggered. Basically these methods carry out the functionality of the events. Note an event can have multiple event handler method associate with it, and it may be such that the handler method may be in some other class, but irrespective of that all the handler methods are

executed when that particular event is raised. The link between the triggering event and the associated method is established at run time. Now how do we write event handler method?

CLASS event_handler DEFINITION. PUBLIC SECTION. METHODS: event_method FOR EVENT test_event OF CLASS test. ENDCLASS.

Setting the handler for the event (registering the event)

To execute the event handler method, automatically when the event is raised the handler method has to be registered, the registration process is dynamic i.e the link is established at runtime. The key statement set handler can register the handler method. The following piece of code will show how the handler method can be registered. ********** Main program********************* Start-of-selection. Data: my_object type ref to test. Create object: my_object . SET HANDLER my_object->event_method FOR test. setting the handler

Dynamic registration and de-registration of handler methods.

Handler methods can be registered or deregistered dynamically by the optional key word ACTIVATION. Activation can register new method or deregister existing method using a variable .The variable must be character type of length 1. Note if the variable contains the Value as x then the method is registered and if it contains the value as space then the method is deregistered. Lets take an example Start-of-selection. Data: my_object type ref to test. Create object: my_object. SET HANDLER my_object->event_method FOR test. registering the handler SET HANDLER my_object->event_method FOR test ACTIVATION x. deregistering the handler SET HANDLER my_object->event_method FOR test ACTIVATION SPACE. registering the handler.

Raising the event .

An event can be raised in by using the ABAP statement raise event. Events can be raised anywhere It may be raised in any method of a class or may be raised anywhere in the main program. Lets take an example CLASS testing_event DEFINITION. PUBLIC SECTION. EVENTS my_event. METHODS event_raising_method. ENDCLASS. CLASS testing_event IMPLEMENTATION. METHOD event_raising_method. RAISE EVENT my_event. ENDMETHOD. ENDCLASS.

Like methods parameters can be passed to events, events can only have input parameters; any no of parameters can be passed to an event. As we know an event does not have any functionality, (the functionality associated with an event is carried out by the event handler method that has been assigned to it) then the question is what does the event do with the input parameters? The parameters that are passed to the events is actually utilized by the handler methods that are registered to the events, the handler methods have the liberty to decide on the number of input parameters passed by the events .The number of input parameters for the handler methods has to be defined explicitly. However, an event handler method is not obliged to handle all of the actual parameters that are passed to it. As well as the explicitly defined parameters, each instance event also contains the implicit EXPORTING parameter SENDER. This has the type of a reference variable referring to the class or interface in which the event is declared. If you include the SENDER parameter in the IMPORTING parameter list of the handler method, the method receives a reference to the triggering object. Remember, however, that teh type of the reference variable is not necessarily the class of the sending object. If the event is declared in a superclass of the class of the sender, or in an interface implemented by it, the type of the reference variable will refer to the superclass or interface instead. Static events do not have a SENDER parameter. Few points to note Event parameters are always passed by VALUE

Event parameters.

Must always have a type specification (TYPE, LIKE) Can be optional (OPTIONAL, DEFAULT) Lets take an example CLASS evt_container DEFINITION. PUBLIC SECTION. METHODS event_trigger. EVENTS my_event EXPORTING VALUE(PAR1) TYPE I VALUE(PAR2) TYPE I OPTIONAL VALUE(PAR3) TYPE I DEFAULT 3. ENDCLASS. CLASS class_evt_handler_method DEFINITION. PUBLIC SECTION. METHODS handler_for_event FOR EVENT my_event OF evt_container IMPORTING SENDER PAR3. ENDCLASS.

CLASS evt_container IMPLEMENTATION. METHOD evt_trigger. RAISE EVENT my_event EXPORTING PAR1 = 1. ENDMETHOD. ENDCLASS. CLASS class_event_handler_method IMPLEMENTATION. METHOD handler_my_event. Write :/ this is event handler method. ENDMETHOD. ENDCLASS. *************** main program ******************************* DATA:ref_obj1 TYPE REF TO evt_container, Ref_obj2 TYPE REF TO class_evt_handler_method. Create object : ref_obj1, Ref_obj2. SET HANDLER ref_obj2->handler_for_event FOR evt_container. Call method ref_obj1->evt_trigger. this will raise the event. event handler method will be called automatically *************** out put *********************************** this is event handler method

Few important point regarding event registration. If an instance triggers an event, the registration is implicitly deleted if the triggering instance is deleted by the automatic memory management (garbage collection). When you register instance methods as event handlers, note that the registration refers to the current instance and not to the reference variable that is used for registration. Even when the reference variable takes another value after SET HANDLER, the registration of the object remains unchanged. This also affects to the lifetime of objects - an object exists for as long as it is registered as an event handler, even if no more reference variables point to it. The object is either explicitly deregistered using the ACTIVATION addition, or implicitly when the triggering instance no longer exists.

Events and error handling.

System variable check(sy_subrc) for event handling. If sy-subrc = 0 then all event handler methods have been properly registered SY-SUBRC = 4: then you tried to register the same combination of triggering event, event handling method, and handler more than once. If sy-subrc = 8: then you tried to deregister an event handler that was not registered.

Non-cacheable runtime error during event handling. SET_HANDLER_DISP_OVERFLOW: Unable to register any more handlers. SET_HANDLER_E_NO_FOR: Handlers of instance methods need the FOR addition. SET_HANDLER_FOR_CE: Event handler registered for a static event. SET_HANDLER_FOR_NULL: An event may not be triggered using NULL. Note If an instance triggers an event, the registration is implicitly deleted if the automatic memory management (garbage collection) deletes the triggering instance. When you register instance methods as event handlers, note that the registration refers to the current instance and not to the reference variable that is used for registration. Even when the reference variable takes another value after SET HANDLER, the registration of the object remains unchanged. This also affects to the lifetime of objects - an object exists for as long as it is registered as an event handler, even if no more reference variables point

to it. The object is either explicitly deregistered using the ACTIVATION addition, or implicitly when the triggering instance no longer exists.

Object Lifetime and event.

An object exists for as long as it is being used in the program. An object is in use by a program for as long as at least one reference points to it, or at least one method of the object is registered as an event handler. As soon as there are no more references to an object, and so long as none of its methods are registered as event handlers, it is deleted by the automatic memory management (garbage collection). The ID of the object then becomes free, and can be used by a new object.

Chapter 5(Inheritance)
1. 2. 3. 4. 5.
6.

Fundamentals of inheritance Creating multilevel inheritance Member access and inheritance Method redefinition Concept of super and final Constructor and inheritance

Fundamentals of inheritance
The concept of inheritance is used to incorporate the properties of an existing class into a new class .The beauty of this feature is that the methods and the attribute of the existing class need not to be coded into the new class ,as well as new features can be added into the the new class . In OOP inheritance can be multiple as well as multi-level ,but in ABAP only multi-level inheritance is possible .In Multiple Inheritance a new class can inherite properties of several class .Where as multi-level inheritance is an inheritance of hiercial fashion . In OOP terminology the parent class is called the super class and the derieved class is called the
subclass.

Father

Mother

grandfather father

Child
Multiple Inheritance

child
Multi- level Inheritance

If you do not add any new declarations to the subclass, it contains the same components as the superclass. However, only the public and protected components of the superclass are visible in the subclass. Although the private components of the superclass exist in the subclass, they are not visible. Super Class
Public Section: Super Class Private Section: Super Class

Note the private Section is not visible in the sub class

Sub Class
Public Section: Super Class Protected Section: Super

You can declare private components in a subclass that have the same names as private

components of the superclass. Each class works with its own private components. Methods that a subclass inherits from a superclass use the private attributes of the superclass, and not any private components of the subclass with the same names.

If the superclass does not have a private visibility section, the subclass is an exact replica of the superclass. However, you can add new components to the subclass. This allows you to turn the subclass into a specialized version of the superclass.

Creating multilevel inheritance


Multilevel inheritance can be better explained as grandfather-> father->child type of inheritance ,where the sub class inherits the property of the superclass ,as well as it has some property of its own . Multilevel inheritance can be created like normal inheritance .for example father inheriting from grandfather and further the child from father. Lets take an example. CLASS grand_father DEFINITION. ******/ write the code***** ENDCLASS. CLASS father DEFINITION INHERITING FROM grand_father. *******/write the code********* ENDCLASS. CLASS son DEFINITION INHERITING FROM father. *******/write the code ******** ENDCLASS. The class implementation part of sub class remains the same , as that of normal classes .

Member access and inheritance .


As we know that in inheritance the subclass inherits the property of the super class ,now the question comes can the subclass inherit all the property of the super class ?? the answer is no ,only the public and the protected members are inheritable by the sub class. Now what happens if any component of the superclass has the same name that of the sub class ?? This is only possible in case of private components ,the private components of a super class or subclass does not interfere or interact they are sole property of the class in which they are declared . Where as public or protected components must be unique i.e they must not have same name .

Another question might come up , suppose a super class method is called by the subclass ,and suppose it takes some input parameters that are private , and the input parameters are explicity defined both in sub class as well as in super class ,then during the function call which components does it uses ,is it of super class or that of subclass.?? Well it uses the components of the super class.

Method Redefinition.
The method that has already been defined in the superclass can be redefined within the subclass .When a super class method is redefined within the sub class then the method name export import parameters remain the same but the method implemetation changes .And whenever the redefined method is called from any instance of the sub class then the new implementation comes into picture .The earlier implementation that already existed in the super class is shadowed by the new implementation.

Super class
METHOD2 calls METHDO1

METHOD1

Instance of sub l

Sub- class
METHOD1 Redefined METHOD2 calls METHDO1

Call Method Method2 Call Method Super->method2

Call Method Method2

Schematic diagram to represent the concept of method redefinition and

Accessibility of components by use of super. From the above diagram it can be seen that if a method is redefined then ,the uniqueness of the method is implicitly defined by the system ,this prevents the confusion during the method call. When a redefined method is called by the super class then the method implementation in the super class is called and when the redefined method is called from the sub class then the newly defined version is called Example of method redefinition. CLASS super_class DEFINITION.
PUBLIC SECTION. METHODS super_class_method.

ENDCLASS. CLASS sub_class DEFINITION INHERITING FROM super_class PUBLIC SECTION. METHODS super_class_method REDIFINITION. ENDCLASS. CLASS super_class IMPLEMENTATION. METHOD super_class_method. Write:/ this is super class method. ENDMETHOD. ENDCLASS. CLASS sub_class IMPLEMENTATION. METHOD super_class_method. Write:/ super class method is redefined in sub class. ENDMETHOD. ENDCLASS. *************** MAIN PROGRAM **************************** START-OF-SELECTION. Data: sub_ref type ref to sub_class, reference for sub_class Sup_ref type ref to super_class. reference for super_class. Create object: sub_ref ,

Sup_ref. Call method sub_ref->super_class_method. Call method sup_ref->super_class_method.

******************out put ************************* this is super class method super class method is redefined in sub class

Concept of super and final.


Super As we have seen in method redefinition how the system manages the uniqueness of the method name ,it has been also shown which method definition is called when the method is called from subclass or superclass. But what if we want to call the method implementation of the superclass from the subclass. We can do this by using the ABAP key word super, super is a reference to the super class and with this reference we can call the method implementation in the super class

Example .

CLASS father DEFINITION. PUBLIC SECTION.


METHODS: my_method. ENDCLASS.

CLASS father IMPLEMENTATION. METHOD my_method.


Write:/ this is father method.

ENDMETHOD. ENDCLASS.

CLASS son DEFINITION INHERITING FROM father. PUBLIC SECTION. METHODS:my_method REDEFINITION, Call_my_method. ENDCLASS.

CLASS son IMPLEMENTATION. METHOD my_method. Write:/ father method redefined in son. ENDMETHOD.

METHOD call_my_method. Call super->my_method. ENDMETHOD. ENDCLASS.

the method definition of father will be called

Final
We have seen that any class can be inherited by any other class ,but suppose we want to prevent a class from being inherited then what do we do??
We can do this by adding the key word final with class. Any class that is declared bas final cannot be inheritated ,or in other words final puts an end to the inheritance tree. Similarly if we want to put a stop to any particular method ,from being inherited we can do it by declaring as final. A point to note a final method cannot be redefined in the subclass.
Example.

CLASS test DEFINITION FINAL. this class cannot be inherited ENDCLASS. CLASS test IMPLEMENTATION.
ENDCLASS.

Constructor and inheritance.

In the earlier section we have seen the concept of inheritance ,how can we access the methods of the super class etc. From our earlier exposure to constructor we know that every class has a static as well as instance constructor ,and they are implicitly defined by the system . Therefore in an inheritance tree the super class as well as the subclass has an unique constructor . In this section we plan to look into some of the issues which might come up, lets take them point wise. suppose we instantiate a sub class ,what happens to the superclass whose constructor has not been defined explicitly? When ever we instantiate a sub class ,whose super class constructor is not explicitly defined ,then the constructor of the super class is called automatically by the system ,we need not take any extra care during subclass instantiation . suppose we instantiate a sub class which has an explicitly defined constructor ,and the super class constructor is not defined explicitly then what happens? In cases where the super class constructor is not defined explicitly ,the system automatically calls the the super class constructor during instantiation of subclass.The sequence in which the constructor are called is same as that of the inheritance hierarchy i.e grandfather-> father-> child suppose we instantiate a sub class which has an explicitly defined constructor and the super class constructor is also explicitly defined then what happens during the instantiation of sub class? In cases where the super class constructor is explicitly defined ,the system cannot call the constructor of the super class during subclass instantiation ,the super class constructor has to be explicitly coded in the sub class constructor I,e we should write the code as Call method super->constructor importing <parameters> if suppose the super constructor is explicitly defined and has some input parameters then how do we pass the parameters to the superclass constructor from the sub class?
To pass parameters to the super constructor we must do it at the time of instantiation of the sub class ,constructor of the subclass has to be defined explicitly and parameters has to be passed to it ,in the definition part of the subclass constructor the super class constructor has to be called and then parameters has to be passed to it .

example ******************************************************************** * gf_num importing parameter for grandfather constructor * f_num importing parameter for father constructor

* c_num importing parameter for child constructor. * this example shows how parameters can be passed to super constructor from *subclass constructor other methods may also exist. * * *this is a full working program showing constructor and inheritance ******************************************************************* CLASS grand_father DEFINITION. PUBLIC SECTION . METHODS : gf_property,
constructor importing gf_num type i.

ENDCLASS. CLASS grand_father IMPLEMENTATION. METHOD gf_property. write:/ 'i am grand father'. endmethod. method constructor . write:/ 'grand father constructor'. write:/ gf_num. endmethod. ENDCLASS. CLASS father DEFINITION INHERITING FROM grand_father.
PUBLIC SECTION.

METHODS:f_property,
constructor importing f_num type i gf_num type i.

ENDCLASS. CLASS father IMPLEMENTATION. METHOD f_property. write:/ 'i am father'. ENDMETHOD.

METHOD constructor. call method super->constructor exporting gf_num = gf_num. ******/constructor of grandfather is called from father constructor
write:/ 'father constructor'. write:/ f_num.

ENDMETHOD. ENDCLASS. CLASS child DEFINITION INHERITING FROM father. PUBLIC SECTION. METHODS:c_property, constructor importing c_num type i f_num type i gf_num type i. ENDCLASS. CLASS child IMPLEMENTATION. METHOD c_property. write:/'i am child'. ENDMETHOD. METHOD constructor. call method super->constructor exporting f_num = f_num gf_num = gf_num *******/constructor of father is called from the child constructor write:/ 'child constructor'. write:c_num. ENDMETHOD. ENDCLASS. ******************* main program*****************8 start-of-selection. data: my_obj TYPE REF TO CHILD. CREATE OBJECT: my_obj exporting c_num = 3 f_num = 2 gf_num = 3. call method my_obj->gf_property. calling the method of class grandfather call method my_obj->f_property. calling the method of class father

end-of-selection.

Chapter 6 (Interface polymorphism inheritance.)

Introduction to interface Class and interface Interface reference and component access. Concept of polymorphism Reference variable Polymorphism via interface Polymorphism via inheritance.

Introduction to interface
Interface is one of the strong features of OOP; interface coupled with inheritance is used to feature one of the most powerful features of OOP called polymorphism, which will be discussed later. Interface is similar to abstract class but it has many strong features. Interface like class has both definitions as well as the implementation part. The interface can be implemented only in the class that uses it. Interface, which is an independent structure, is used to implement in a class to extend the scope of a class. Interfaces extend the scope of a class by adding their own components to its public section. This allows users to address different classes via a universal point of contact. How to write simple interface INTERFACE my_interface. Data : name(20). METHODS: I_method1, I_method2. ENDINTERFACE.

Class and interface


Class and interface are interrelated, an interface cannot be implemented without a class .interface do not have instances. Interface can be implemented in a class using the statement INTERFACES <interface_name> in the public section of a class. The component of the interface are added automatically in the public section of the class. Now the question comes how do we access the components of the interface ?? Well the components of an interface can be identified as if they are components of the class, through the identifier <interface_name~interface _component > Lets take an example.

INTERFACE my_interface . METHODS my_interface_method exporting num type i. ENDINTERFACE. CLASS interface_class DEFINITION. INTERFACES my_interface. ENDCLASS.

CLASS interface_class IMPLEMENTATION. METHOD my_interface~my_interface_method. Write:/ num. ENDMETHOD. ENDCLASS. The class must implement the methods of all interfaces implemented in it. The implementation part of the class must contain a method implementation for each interface method <imeth>: METHOD <intf~imeth>. ... ENDMETHOD. NOTE Interfaces can be implemented by different classes. Each of these classes is extended by the same set of components. However, the methods of the interface can be implemented differently in each class. Lets take an example INTERFACE common_interface. METHODS: interface_method. ENDINTERFACE. CLASS interface_class1 DEFINITION. PUBLIC SECTION. INTERFACES common_interface. ENDCLASS. CLASS interface_class1 IMPLEMENTATION. METHOD common_interface~interface_method. interface method is implemented as type 1 WRITE:/ METHODS IMPLEMENTATION TYPE 1

ENDMETHOD. ENDCLASS. CLASS interface_class2 DEFINITION. PUBLIC SECTION. INTERFACES common_interface. ENDCLASS. CLASS interface_class2 IMPLEMENTATION. METHOD common_interface~interface_method. Interface method is implemented astype2 WRITE:/ METHODS IMPLEMENTATION TYPE 2 ENDMETHOD. ENDCLASS

Interface reference and component access


As we know that before creation of any object it must have a reference to a class, similarly an interface can also have a reference. But we know interface cannot be instantiated so why do we need a reference for it?? The interface reference is used from a totally different point of view, an interface reference is used to access the interface components that has been implemented differently in different class. We will elaborate this in the next article. An interface reference can be created similar to object reference. For example DATA: interface_ref TYPE REF TO my_interface. To create an object of the class <class>, you must first have declared a reference variable <cref> with reference to the class. If the class <class> implements an interface <intf>, you can use the following assignment between the class reference variable <cref> and an interface reference <iref> to make the interface reference in <iref> point to the same object as the class reference in <cref>: <iref> = <cref> If the interface <intf> contains an instance attribute <attr> and an instance method <meth>, you can address the interface components as follows: Using the class reference variable <cref>: To access an attribute <attr>: <cref>-><intf~attr> To call a method <meth>: CALL METHOD <cref>-><intf~meth> Using the interface reference variable <iref>:

To access an attribute <attr>: < iref>-><attr> To call a method <meth>: CALL METHOD <iref>-><meth>

Reference variable.

As we know that a reference variable contains an object reference, a reference variable has two characteristics, static as well as dynamic characteristic. The static characteristic is the class or interface used in reference variable definition. The dynamic type is the object to which the reference variable is currently pointing. Reference variable assigning rule. When the static and the dynamic type of a reference variable are different, the principal rule is that the static type is always more general than the dynamic type. For example if the static type is an interface the dynamic type can be a class implementing the interface.

Concept of Polymorphism
Polymorphism refers to the property by which objects belonging to different classes are able to respond to the same message, but in different forms, an essential requirement of polymorphism is the ability to refer to objects without any regard to their class. This necessitates the use of a single reference variable to refer to the object of different class. For example consider two classes, say scooter and bike, both the scooter and bike have a method called engine_power, and the method is implemented differently in different class. The concept of polymorphism says that we should have a common point of access to the method engine _power implemented differently. Now the question comes how do we achieve polymorphism. The concept of polymorphism can be shown through inheritance as well as interface.

Polymorphism via interface


Interfaces allow you to use different classes in a uniform way using interface references .For example, interfaces that are implemented in different classes extend the public scope of each class by the same set of components. If a class does not have any class-specific public components, the interfaces define the entire public face of the class. As we know that

any no of class can implement the interface differently ,and we have seen that how we can access the components of the interface through the class objects. But we want to have a common point of access for the different implementation of the interface methods. So how do we do that? Well we can do that by using an interface reference; we can access the different class objects by pointing the interface reference to that class. Lets take an example: INTERFACE my_infc. METHODS: I_method. ENDINTERFACE.

CLASS c1 DEFINITION. PUBLIC SECTION. INTERFACES: my_infc. ENDCLASS. CLASS c1 IMPLEMENTATION. METHOD my_infc~I_method. Write:/ method for c1. ENDMETHOD. ENDCLASS.

CLASS c2 DEFINITION. PUBLIC SECTION. INTERFACES: my_infc. ENDCLASS. CLASS c2 IMPLEMENTATION. METHOD my_infc~I_method. Write:/ method for c2. ENDMETHOD. ENDCLASS. START-OF-SELECTION. DATA: iref type ref to my_infc, C1ref type ref to c1. C2ref type ref to c2.

CREATE OBJECT: C1ref, C2ref . Iref = C1ref. reference of class c1 is passed to interface reference Iref->I_method. method implemented in class c1 is called Iref = C2ref. reference of class c1 is passed to interface reference Iref->I_method. method implemented in class c2 is called.

********************* OUTPUT************** method for c1 method for c2

With inheritance, a reference variable defined with respect to a class c1 may not only point to instances of c1 but also to instances of subclasses of c1. You can even create subclass objects using a reference variable typed with respect to a superclass. A reference variable is said to have static and a dynamic type. Using inheritance only, the static type is either the same as the dynamic type or is a superclass of the dynamic type. In other words, we can use the reference of super class to point to the instance of the sub class, and using this reference we can access the components of the subclass. A point of note, we can access only those components of the subclass that it has inherited from the super class. For example suppose there are two class father and child. And suppose father is the super class and the child is the subclass . Now we create two instances say one of father and one of child ,o_father and o_child respectively. From the point of view of polymorphism we need to have a common point of access of the components of o_father as well as of o_child (that are common to both ). Then how do we create the common point ?? Well we con do that by a reference variable, whose static part is of type father and dynamic part should point to the child. Lets take an example. Lets take an example.

Polymorphism via inheritance.

CLASS father DEFINITION. PUBLIC SECTION.

METHODS: f_meth. ENDCLASS. CLASS father IMPLEMENTATION. METHOD f_meth. Write: / father method. ENDMETHOD. ENDCLASS. CLASS child DEFINITION INHERITING FROM FATHER. PUBLIC SECTION. METHODS : f_meth REDEFINITION, C_meth. ENDCLASS. CLASS child IMPLEMENTATION. METHOD f_meth. Write: father method redefined. ENDMETHOD. METHOD c_meth. Write: child method. ENDMETHOD. ENDCLASS.

START-OF-SELECTION. DATA: f_ref TYPE REF TO father, Ref TYPE REF TO father, C_ref TYPE REF TO child. CREATE OBJECT: C_ref,f_ref. Ref = f_ref. Call method ref->f_meth. Ref = C_ref. Call method ref->f_meth. ** call method ref->c_meth . this assignment is not possible.

*************** out put *********************** father method father method redefined.

EXAMPLE 2. In this example the previous example has been shown with interface. interface my_ic. methods:add_value. endinterface. class father definition. public section. interfaces my_ic. endclass. class father implementation. method my_ic~add_value . write:/ 'result father' . endmethod. endclass.

class child definition inheriting from father. public section. methods:my_ic~add_value redefinition, child_meth. endclass.

class child implementation. method my_ic~add_value. write:/ 'child'. endmethod. method child_meth. write:/ 'child own method'. endmethod.

endclass.

start-of-selection. data:c_ref type ref to child, f_ref type ref to father, i_ref type ref to my_ic.

create object: f_ref type child. call method f_ref->my_ic~add_value. *call method f_ref->child_meth. " this is not possible as this was not "inherited

You might also like