INSTANTIATION AND OBJECT-ORIENTED LANGUAGE <ul><li>Multiple instances(objects) of the same blue print (type or class) are ...
OBJECT  ORIENTED PROGRAMMING  MODEL <ul><li>ABSTRACTION </li></ul><ul><li>This refers to the ability to reflect real-world...
Object Orientation - Advantages   <ul><li>Complex software systems become easier to understand, since object-oriented stru...
Classes <ul><li>Classes describe objects.  </li></ul><ul><li>From a technical point of view, objects are runtime instances...
Object References   <ul><li>In a program, you identify and address objects using unique object references.  </li></ul><ul>...
CLASSES AND OBJECTS <ul><li>CLASS </li></ul><ul><li>General description of objects(blueprint). </li></ul><ul><li>Specifies...
LOCAL/GLOBAL  CLASSES <ul><li>LOCAL CLASSES </li></ul><ul><li>Local Classes can be defined within any ABAP program and are...
CLASS  DEFINITION <ul><li>DECLARATION   </li></ul><ul><li>CLASS   C1   DEFINITION. </li></ul><ul><li>Public Section. </li>...
VISIBILITY SECTIONS <ul><li>PUBLIC </li></ul><ul><li>All components which are assigned to this section are public and can ...
COMPONENTS  OF  THE CLASS <ul><li>ATTRIBUTES </li></ul><ul><ul><li>Attributes are the data objects within a class. The ref...
ATTRIBUTES, TYPES AND CONSTANTS <ul><li>CLASS <CLASSNAME> DEFINITION. </li></ul><ul><li>… </li></ul><ul><li>  TYPES: <NORM...
Attributes  <ul><li>Instance Attributes: </li></ul><ul><li>The contents of instance attributes define the instance specifi...
Methods   <ul><li>Instance Methods </li></ul><ul><li>You declare instance methods using the  METHODS  statement.  </li></u...
METHODS <ul><li>PARAMETER INTERFACE </li></ul><ul><li>The parameter interface of a method is defined by the additions to t...
INSTANCE AND STATIC METHODS <ul><li>STATIC METHODS </li></ul><ul><li>Can only use Static components in their implementatio...
Class Example  (1 of 3) <ul><li>  (Defining the class) </li></ul><ul><li>CLASS c_airplane DEFINITION. </li></ul><ul><li>* ...
Class Example  (2 of 3) <ul><li>  (Implementing the class) </li></ul><ul><li>Type-pools icon. </li></ul><ul><li>CLASS c_ai...
Class Example  (3 of 3) <ul><li>  (Using  the class) </li></ul><ul><li>data: plane_name type string, </li></ul><ul><li>pla...
CONSTRUCTOR <ul><li>A Constructor is a special method which is automatically executed by the runtime environment.  </li></...
INSTANCE CONSTRUCTOR <ul><li>Instance Constructors are called once for each instance of a class after the complete creatio...
STATIC  CONSTRUCTOR <ul><li>The Static Constructor is called in a program once for each class before the class is accessed...
CREATING OBJECTS <ul><li>Objects are created using CREATE OBJECT statement . </li></ul><ul><li>Objects can only be created...
INHERITANCE LCL_SUP1 LCL_SUP2 LCL_SUB1 LCL_SUB2 LCL_SUB3 NO  MULTIPLE INHERITANCE Generalization Specialization
Inheritance <ul><li>You can use an existing class to derive a new class.  </li></ul><ul><li>Derived classes inherit the da...
RELATIONSHIP BETWEEN SUPERCLASS AND SUBCLASS <ul><li>Common Components only exist once in the superclass. </li></ul><ul><l...
INHERITANCE <ul><li>Class lcl_veh Definition </li></ul><ul><li>  Public Section. </li></ul><ul><li>  … </li></ul><ul><li> ...
REDEFINING  METHODS <ul><li>Class lcl_vehicle DEFINITION. </li></ul><ul><li>PUBLIC SECTION. </li></ul><ul><li>METHODS: est...
REDIFINING  METHODS <ul><li>The REDEFINITION statement for the inherited method must be in the same SECTION as the definit...
INHERITANCE AND REDEFINING THE CONSTRUCTOR <ul><li>The Constructor of the superclass must be called within the constructor...
RULES FOR CALLING THE CONSTRUCTOR <ul><li>CASE 1 </li></ul><ul><li>Class of instance to be created has constructor. </li><...
INTERFACES <ul><li>In ABAP objects, interfaces are implemented in addition to and independently of classes. </li></ul><ul>...
DEFINING  AND IMPLEMENTING  INTERFACE <ul><li>Declaring the interface </li></ul><ul><li>INTERFACE lif_partners. </li></ul>...
ABSTRACT  CLASSES <ul><li>Abstract classes themselves cannot be instantiated. Although their subclasses can. </li></ul><ul...
FINAL  CLASSES <ul><li>Final Classes can’t have subclasses and can therefore protect themselves from specialization. </li>...
SUMMARY <ul><li>A final class implicitly only contains final methods. In this case you cannot enter FINAL explicitly in th...
FRIENDS CONCEPT <ul><li>In rare cases, Classes have to work together closely and make all their components, including the ...
Reference Variable ME <ul><li>Within a class, you can use the self-reference  ME  to access the individual components: </l...
Example  (1 of 4) <ul><li>Class Definition. </li></ul><ul><li>CLASS cl_airplane DEFINITION. </li></ul><ul><li>PUBLIC SECTI...
Example  (2 of 4) <ul><li>Class Implementation </li></ul><ul><li>CLASS cl_airplane IMPLEMENTATION. </li></ul><ul><li>METHO...
Example  (3 of 4) <ul><li>METHOD display_attributes. </li></ul><ul><li>WRITE : / 'Name of airplane:'(001), AT pos_1 name, ...
Example  (4 of 4) <ul><li>Creating class reference and accessing the class attributes & methods </li></ul><ul><li>DATA: r_...
Upcoming SlideShare
Loading in …5
×

Objectorientedprogrammingmodel1

2,231 views

Published on

Object oriented programming model

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,231
On SlideShare
0
From Embeds
0
Number of Embeds
15
Actions
Shares
0
Downloads
344
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Objectorientedprogrammingmodel1

  1. 1. INSTANTIATION AND OBJECT-ORIENTED LANGUAGE <ul><li>Multiple instances(objects) of the same blue print (type or class) are a fundamental attribute of object-oriented languages </li></ul><ul><li>The ability to create multiple instances of a “class”, such as a vehicle, is one of the central attributes of object-oriented languages. </li></ul>
  2. 2. OBJECT ORIENTED PROGRAMMING MODEL <ul><li>ABSTRACTION </li></ul><ul><li>This refers to the ability to reflect real-world processes as realistically in the programming language as possible. </li></ul><ul><li>ENCAPSULATION </li></ul><ul><li>Implementation details are hidden behind well-defined and documented interfaces. Implementation of the objects are hidden from other components in the system </li></ul><ul><li>INHERITENCE </li></ul><ul><li>Classes, are derived from existing ones. They inherit all the attributes and methods of the higher-level class and can expand and specialize them. </li></ul><ul><li>POLYMORPHISM </li></ul><ul><li>Different Objects can present the same interface to the outside. A user only needs to know the interface, by using this, he can access various classes without knowing the details. Objects in different classes react differently to the same messages. </li></ul>
  3. 3. Object Orientation - Advantages <ul><li>Complex software systems become easier to understand, since object-oriented structuring provides a closer representation of reality than other programming techniques. </li></ul><ul><li>In a well-designed object-oriented system, it should be possible to implement changes at class level, without having to make alterations at other points in the system. </li></ul><ul><li>This reduces the overall amount of maintenance. </li></ul><ul><li>Through polymorphism and inheritance, object-oriented programming allows you to Re-use individual components. </li></ul><ul><li>The amount of work involved in revising and maintaining the system is reduced, since many problems can be detected and corrected in the design phase . </li></ul>
  4. 4. Classes <ul><li>Classes describe objects. </li></ul><ul><li>From a technical point of view, objects are runtime instances of a class. </li></ul><ul><li>In theory, you can create any number of objects based on a single class. </li></ul><ul><li>Each instance (object) of a class has a unique identity and its own set of values for its attributes. </li></ul>
  5. 5. Object References <ul><li>In a program, you identify and address objects using unique object references. </li></ul><ul><li>Object references allow you to access the attributes and methods of an object. </li></ul>
  6. 6. CLASSES AND OBJECTS <ul><li>CLASS </li></ul><ul><li>General description of objects(blueprint). </li></ul><ul><li>Specifies status data(attributes) and behavior(methods). </li></ul><ul><li>OBJECT(INSTANCE) </li></ul><ul><li>Representation of real world. </li></ul><ul><li>Specific instance of a class. </li></ul><ul><li>Two different objects can have identical attribute values and still not be identical. </li></ul>
  7. 7. LOCAL/GLOBAL CLASSES <ul><li>LOCAL CLASSES </li></ul><ul><li>Local Classes can be defined within any ABAP program and are only visible there. </li></ul><ul><li>GLOBAL CLASSES </li></ul><ul><li>Global Classes are created with the Class Builder tool of the ABAP workbench in the Class Library and are visible to any ABAP program. </li></ul><ul><li>Incidentally, it is unimportant whether you use local or global classes to create objects, except that they differ in their visibility. </li></ul><ul><li>Objects of local classes can only be created in the same program, whereas objects of global classes can be created in any ABAP program. </li></ul>
  8. 8. CLASS DEFINITION <ul><li>DECLARATION </li></ul><ul><li>CLASS C1 DEFINITION. </li></ul><ul><li>Public Section. </li></ul><ul><li>… </li></ul><ul><li> Protected Section. </li></ul><ul><li>… </li></ul><ul><li>Private Section. </li></ul><ul><li>… </li></ul><ul><li>ENDCLASS . </li></ul><ul><li>IMPLEMENTATION </li></ul><ul><li>CLASS C1 IMPLEMENTATION . </li></ul><ul><li>METHOD M1. </li></ul><ul><li>… </li></ul><ul><li>ENDMETHOD. </li></ul><ul><li> METHOD M2. </li></ul><ul><li>… </li></ul><ul><li>ENDMETHOD. </li></ul><ul><li> ENDCLASS . </li></ul>The class statement cannot be nested.
  9. 9. VISIBILITY SECTIONS <ul><li>PUBLIC </li></ul><ul><li>All components which are assigned to this section are public and can be addressed by all users, methods of subclasses and methods of the class itself.They form the external interface of the class. </li></ul><ul><li>PROTECTED </li></ul><ul><li>The components of this section are protected and can be addressed by the methods of subclasses and the methods of the class itself. They form the interface of the class to its subclasses. </li></ul><ul><li>PRIVATE </li></ul><ul><li>Components of this section are private and can only be used in the methods of the class itself. </li></ul>
  10. 10. COMPONENTS OF THE CLASS <ul><li>ATTRIBUTES </li></ul><ul><ul><li>Attributes are the data objects within a class. The reflect an Objects state. </li></ul></ul><ul><ul><li>INSTANCE ATTRIBUTES : DATA statement </li></ul></ul><ul><ul><li>STATIC ATTRIBUTES : CLASS-DATA statement. </li></ul></ul><ul><ul><li>A READ-ONLY addition can be used with both DATA stmt. </li></ul></ul><ul><li>METHODS </li></ul><ul><li>The way in which objects behave is implemented in the methods of a class. </li></ul><ul><li>INSTANCE METHODS : can access all attributes and events of their own class. </li></ul><ul><li>STATIC METHODS : can only access static attributes and static events . </li></ul><ul><li>CONSTRUCTOR </li></ul><ul><li>It is the special method which can be of two types: </li></ul><ul><li>INSTANCE CONSTRUCTOR: implicitly called for each instantiation of a new object. </li></ul><ul><li>STATIC CONSTRUCTOR: is called the first time the class is accessed . </li></ul><ul><li>EVENTS </li></ul><ul><li>All methods of the same class can trigger these events. Other methods of the same or the different class can be declared as special handler methods which react to these events. </li></ul>
  11. 11. ATTRIBUTES, TYPES AND CONSTANTS <ul><li>CLASS <CLASSNAME> DEFINITION. </li></ul><ul><li>… </li></ul><ul><li> TYPES: <NORMAL TYPE DEFINITION> </li></ul><ul><li> CONSTANTS: CONSTANT TYPE <TYPE> VALUE <VALUE>. </li></ul><ul><li> DATA : VAR1 TYPE <TYPE>, </li></ul><ul><li> VAR2 TYPE <DDIC_TYPE>, </li></ul><ul><li> VAR3 TYPE VAR1, </li></ul><ul><li>VAR4 TYPE <TYPE> VALUE <VALUE>, </li></ul><ul><li> VAR5 TYPE <TYPE> READ ONLY, </li></ul><ul><li> VAR6 TYPE REF TO <CLASSNAME>, </li></ul><ul><li>VAR7 TYPE REF TO <INTERFACE>, </li></ul><ul><li>CLASS-DATA : ONLY_ONCE TYPE … </li></ul><ul><li>ENDCLASS </li></ul>
  12. 12. Attributes <ul><li>Instance Attributes: </li></ul><ul><li>The contents of instance attributes define the instance specific state of an object. You declare them using the DATA statement. </li></ul><ul><li>Static Attributes: </li></ul><ul><li>The contents of static attributes define the state of the class that is valid for all instances of the class. </li></ul><ul><li>Static attributes exist once for each class. </li></ul><ul><li>You declare them using the CLASS-DATA statement. </li></ul><ul><li>They are accessible for the entire runtime of the class. </li></ul><ul><li>All of the objects in a class can access its static attributes. </li></ul><ul><li>If you change a static attribute in an object, the change is visible in all other objects in the class. </li></ul>
  13. 13. Methods <ul><li>Instance Methods </li></ul><ul><li>You declare instance methods using the METHODS statement. </li></ul><ul><li>They can access all of the attributes of a class, and can trigger all of the events of the class . </li></ul><ul><li>Static Methods </li></ul><ul><li>You declare static methods using the CLASS-METHODS statement. </li></ul><ul><li>They can only access static attributes and trigger static events. </li></ul><ul><li>Special Methods </li></ul><ul><li>As well as normal methods, which you call using CALL METHOD , there are two special methods called CONSTRUCTOR and CLASS_CONSTRUCTOR , which are automatically called when you create an object (CONSTRUCTOR) or when you first access the components of a class (CLASS_CONSTRUCTOR). </li></ul>
  14. 14. METHODS <ul><li>PARAMETER INTERFACE </li></ul><ul><li>The parameter interface of a method is defined by the additions to the METHODS statement in the declaration part of the class. </li></ul><ul><li>The implementation part does not require any more details for the parameter interface. </li></ul><ul><li>the complete syntax for defining the parameter interface is: </li></ul><ul><li>METHODS meth </li></ul><ul><li>[ IMPORTING <im_var> TYPE <type> </li></ul><ul><li> EXPORTING <ex_var> TYPE <type> </li></ul><ul><li> CHANGING <ch_var> TYPE <type> </li></ul><ul><li> RETURNING VALUE <re_var> TYPE <type> </li></ul><ul><li>RAISING <class_exception> ]. </li></ul><ul><li>All input parameters(IMPORTING, CHANGING parameters) can be defined as OPTIOAL or DEFAULT. </li></ul>
  15. 15. INSTANCE AND STATIC METHODS <ul><li>STATIC METHODS </li></ul><ul><li>Can only use Static components in their implementation part. </li></ul><ul><li>Can be called using the Class. </li></ul><ul><li>INSTANCE METHODS </li></ul><ul><li>Can use both static and instance components in their implementation part </li></ul><ul><li>Can be called using an instance </li></ul>
  16. 16. Class Example (1 of 3) <ul><li> (Defining the class) </li></ul><ul><li>CLASS c_airplane DEFINITION. </li></ul><ul><li>* defining the access specifiers. </li></ul><ul><li>PUBLIC SECTION. </li></ul><ul><li>CONSTANTS: pos_1 TYPE i VALUE 30. </li></ul><ul><li>* parameterized method </li></ul><ul><li>METHODS: set_attributes IMPORTING im_name TYPE string </li></ul><ul><li>im_planetype TYPE string. </li></ul><ul><li>* method </li></ul><ul><li>METHODS: display_attributes. </li></ul><ul><li>* static method. </li></ul><ul><li>CLASS-METHODS: display_n_o_airplanes. </li></ul><ul><li>PRIVATE SECTION. </li></ul><ul><li>DATA: name TYPE string, </li></ul><ul><li>planetype TYPE string. </li></ul><ul><li>* static variable. </li></ul><ul><li>CLASS-DATA: n_o_airplanes TYPE i. </li></ul><ul><li>ENDCLASS. &quot;c_airplane DEFINITION </li></ul>
  17. 17. Class Example (2 of 3) <ul><li> (Implementing the class) </li></ul><ul><li>Type-pools icon. </li></ul><ul><li>CLASS c_airplane IMPLEMENTATION . </li></ul><ul><li>* presenting the logic the methods. </li></ul><ul><li>METHOD set_attributes. </li></ul><ul><ul><li>name = im_name. </li></ul></ul><ul><ul><li>planetype = im_planetype. </li></ul></ul><ul><ul><li>n_o_airplanes = n_o_airplanes + 1. </li></ul></ul><ul><li>ENDMETHOD. &quot;set_attributes </li></ul><ul><li>METHOD display_attributes. </li></ul><ul><ul><li>WRITE: / icon_ws_plane AS ICON, </li></ul></ul><ul><ul><li>/ 'Name of Airline: '(001), AT pos_1 name, </li></ul></ul><ul><ul><li>/ 'Airplane Type'(002), AT pos_1 planetype. </li></ul></ul><ul><li>ENDMETHOD. &quot;display_attributes </li></ul><ul><li>METHOD display_n_o_airplanes. </li></ul><ul><ul><li>WRITE: / 'Total Number of planes ', </li></ul></ul><ul><ul><li>AT pos_1 n_o_airplanes LEFT-JUSTIFIED. </li></ul></ul><ul><li>ENDMETHOD. &quot;display_n_o_airplanes </li></ul><ul><li>ENDCLASS. &quot;c_airplane IMPLEMENTATION </li></ul>
  18. 18. Class Example (3 of 3) <ul><li> (Using the class) </li></ul><ul><li>data: plane_name type string, </li></ul><ul><li>plane_type type string. </li></ul><ul><li>data: obj_air_plane type ref to c_airplane. “defining the object. </li></ul><ul><li>start-of-selection. </li></ul><ul><li>create object obj_air_plane. “creating the object. </li></ul><ul><li>plane_name = 'JET AIRWAYS'. </li></ul><ul><li>plane_type = 'BOEING 747'. </li></ul><ul><li>* calling the methods using the instance object. </li></ul><ul><li>obj_air_plane->set_attributes( im_name = plane_name </li></ul><ul><li>im_planetype = plane_type ) . </li></ul><ul><li>* calling the methods using the instance object. </li></ul><ul><li>obj_air_plane->display_attributes( ) . </li></ul><ul><li>* calling the class name --> static method.. </li></ul><ul><li>c_airplane=>display_n_o_airplanes( ) . </li></ul>
  19. 19. CONSTRUCTOR <ul><li>A Constructor is a special method which is automatically executed by the runtime environment. </li></ul><ul><li>Constructors are used to set an object dynamically to a defined initial state. </li></ul><ul><li>A constructor cannot be called with the CALL METHOD statement , nor can the developer influence the call point. </li></ul><ul><li>Like other methods constructors can be instance dependent or instance-independent. </li></ul><ul><li>It must be declared in the PUBLIC section of the class declaration. </li></ul>
  20. 20. INSTANCE CONSTRUCTOR <ul><li>Instance Constructors are called once for each instance of a class after the complete creation of the instance with the CREATE OBJECT statement. </li></ul><ul><li>It must be declared in the definition part of the class. </li></ul><ul><li>It must have the predefined name CONSTRUCTOR </li></ul><ul><li>METHODS CONSTRUCTOR </li></ul><ul><li>IMPORTING … {VALUE (i 1 )|i 2 } {TYPE type| LIKE dobj} </li></ul><ul><li> [OPTIONAL|DEFAULT def i ]… </li></ul><ul><li> EXCEPTIONS … x i … </li></ul><ul><li>The fact that the constructor does not have the output parameters shows that it is used solely to define an object’s status and has no other purpose. </li></ul>
  21. 21. STATIC CONSTRUCTOR <ul><li>The Static Constructor is called in a program once for each class before the class is accessed for the first time. </li></ul><ul><li>The name of the Static Constructor with which it is declared and implemented is class_constructor. </li></ul><ul><li>CLASS-METHODS class_constructor </li></ul><ul><li>No interface parameters can be defined for a static constructor. </li></ul><ul><li>Like all other static methods, static constructors can only access static attributes of their class. </li></ul>
  22. 22. CREATING OBJECTS <ul><li>Objects are created using CREATE OBJECT statement . </li></ul><ul><li>Objects can only be created and addressed using reference variables. </li></ul><ul><li>DATA: <obj1> TYPE REF TO <class1> </li></ul><ul><li>CREATE OBJECT <obj1> </li></ul><ul><li>CREATE BOJECT statement creates an object in the Main memory. </li></ul><ul><li>The attribute values of this of this object are either initial values or correspond to the VALUE entry. </li></ul>
  23. 23. INHERITANCE LCL_SUP1 LCL_SUP2 LCL_SUB1 LCL_SUB2 LCL_SUB3 NO MULTIPLE INHERITANCE Generalization Specialization
  24. 24. Inheritance <ul><li>You can use an existing class to derive a new class. </li></ul><ul><li>Derived classes inherit the data and methods of the Super classes. </li></ul><ul><li>However, they can redefine existing methods, and also add new ones. </li></ul>
  25. 25. RELATIONSHIP BETWEEN SUPERCLASS AND SUBCLASS <ul><li>Common Components only exist once in the superclass. </li></ul><ul><li>New components in the superclass are automatically available in subclasses. </li></ul><ul><li>Amount of new coding is reduced </li></ul><ul><li>Subclasses are extremely dependent on superclasses </li></ul><ul><li>“ White Box Reuse”: </li></ul><ul><li>Subclass must possess detailed knowledge of the implementation of the superclass. </li></ul>
  26. 26. INHERITANCE <ul><li>Class lcl_veh Definition </li></ul><ul><li> Public Section. </li></ul><ul><li> … </li></ul><ul><li> Private Section. </li></ul><ul><li> … </li></ul><ul><li>Endclass. </li></ul><ul><li>Class lcl_truck Definition INHERITING FROM lcl_veh </li></ul><ul><li> Public Section. </li></ul><ul><li>… </li></ul><ul><li>Private Section. </li></ul><ul><li> … </li></ul><ul><li> Endclass. </li></ul>All Components from the superclass are automatically present in the subclass.
  27. 27. REDEFINING METHODS <ul><li>Class lcl_vehicle DEFINITION. </li></ul><ul><li>PUBLIC SECTION. </li></ul><ul><li>METHODS: estimate_fuel </li></ul><ul><li>IMPORTING im_dist TYPE s_dist </li></ul><ul><li>RETURNING VALUE (re_fuel) ty_fuel </li></ul><ul><li>Endclass. </li></ul><ul><li>Class lcl_truck DEFINITION INHERITANCE FROM lcl_vehicle </li></ul><ul><li>PUBLIC SECTION. </li></ul><ul><li>METHODS: estimate_fuel REDEFINITION. </li></ul><ul><li>Endclass. </li></ul><ul><li> </li></ul><ul><li>Class lcl_truck IMPLEMANTATION. </li></ul><ul><li>METHOD estimate_fuel. </li></ul><ul><li>… </li></ul><ul><li>Super -> estimate_fuel(…) </li></ul><ul><li>ENDMETHOD. </li></ul><ul><li>Endclass. </li></ul>
  28. 28. REDIFINING METHODS <ul><li>The REDEFINITION statement for the inherited method must be in the same SECTION as the definition of the original method.(It can’t be in Private section). </li></ul><ul><li>If you redefine a method, you don’t need to enter its interface again in the subclass, but only the name of the method. </li></ul><ul><li>ABAP Objects don’t support Overloading. </li></ul><ul><li>Overloading is possible with the constructor. </li></ul><ul><li>With the redefined method, you can access the components of the direct Superclass using the SUPER reference. </li></ul>
  29. 29. INHERITANCE AND REDEFINING THE CONSTRUCTOR <ul><li>The Constructor of the superclass must be called within the constructor of the subclass(to ensure that the objects are initialized correctly). </li></ul><ul><li>The Static Constructor in the super class is called automatically. The runtime system ensures that the static constructor of all its superclasses have already been executed before the static constructor in a particular class is executed. </li></ul>
  30. 30. RULES FOR CALLING THE CONSTRUCTOR <ul><li>CASE 1 </li></ul><ul><li>Class of instance to be created has constructor. </li></ul><ul><ul><li>Fill its parameters </li></ul></ul><ul><li>CASE 2 </li></ul><ul><li>Class of instance to be created has no constructor. </li></ul><ul><li>=> Search for the next superclass with a constructor in the inheritance tree. </li></ul><ul><li>=> Fill its parameters. </li></ul>
  31. 31. INTERFACES <ul><li>In ABAP objects, interfaces are implemented in addition to and independently of classes. </li></ul><ul><li>Interfaces only describe the external point of contact of a class, they don’t contain any implementation. </li></ul><ul><li>Features: </li></ul><ul><li>Simulation of Multiple inheritance </li></ul><ul><li>“ Black Box Principle”: client only knows the interface, not the implementation. </li></ul><ul><li>Interface as a generalization of the implementing class. </li></ul>
  32. 32. DEFINING AND IMPLEMENTING INTERFACE <ul><li>Declaring the interface </li></ul><ul><li>INTERFACE lif_partners. </li></ul><ul><li>METHODS: display_partner. </li></ul><ul><li>ENDINTERFACE. </li></ul><ul><li>The Server classes “make the interface public”. </li></ul><ul><li> CLASS lcl_rental DEFINITION. </li></ul><ul><li>PUBLIC SECTION. </li></ul><ul><li> INTERFACES lif_partners. </li></ul><ul><li> ENDCLASS. </li></ul><ul><li>Interface are implemented in classes. </li></ul><ul><li>Interface don’t have visibility sections. </li></ul><ul><li>CLASS lcl_rental IMPLEMENTATION. </li></ul><ul><li>METHOD lif_partners ~ display_partner. </li></ul><ul><li> … </li></ul><ul><li> ENDMETHOD. </li></ul><ul><li> ENDCLASS. </li></ul>
  33. 33. ABSTRACT CLASSES <ul><li>Abstract classes themselves cannot be instantiated. Although their subclasses can. </li></ul><ul><li>Abstract(instance) methods are defined in the class, but not implemented. </li></ul><ul><li>They must be redefined in subclasses. </li></ul><ul><li>Classes with at least one abstract method are abstract. </li></ul><ul><li>STATIC METHODS AND CONSTRUCTORS CAN’T BE ABSTRACT. </li></ul>
  34. 34. FINAL CLASSES <ul><li>Final Classes can’t have subclasses and can therefore protect themselves from specialization. </li></ul><ul><li>Class lcl_truck DEFINITION FINAL </li></ul><ul><li>INHERITING FROM lcl_vehicle. </li></ul><ul><li>… </li></ul><ul><li>Endclass. </li></ul><ul><li>Final Methods can’t be redefined in subclasses and can protect itself in this way against uncontrolled redefinition. </li></ul><ul><li>Class lcl_bus DEFINITION FINAL </li></ul><ul><li>INHERITING FROM lcl_vehicle. </li></ul><ul><li>PUBLIC SECTION. </li></ul><ul><li>METHOD estimate_number_of_seats FINAL. </li></ul><ul><li>Endclass. </li></ul>
  35. 35. SUMMARY <ul><li>A final class implicitly only contains final methods. In this case you cannot enter FINAL explicitly in these methods. </li></ul><ul><li>Methods cannot be both FINAL and ABSTRACT. </li></ul><ul><li>Classes, on the other hand, that are both ABSTRACT and FINAL can be useful. </li></ul>
  36. 36. FRIENDS CONCEPT <ul><li>In rare cases, Classes have to work together closely and make all their components, including the private and protected ones, available to each other. </li></ul><ul><li>Efficient direct access to the data of a class providing friendship. </li></ul><ul><li>Methods that access the same data can therefore be spread over several classes. </li></ul><ul><li>Friendship is ONE-SIDED: A class providing friendship is not automatically a friend of its friend. </li></ul><ul><li>Class lcl_truck DEFINITION FRIENDS lcl_bus. </li></ul><ul><li>… </li></ul><ul><li>Endclass. </li></ul><ul><li>All the subclasses of class lcl_bus have access to data of lcl_truck, the other way is not possible. </li></ul>
  37. 37. Reference Variable ME <ul><li>Within a class, you can use the self-reference ME to access the individual components: </li></ul><ul><ul><li>To access an attribute in the same class </li></ul></ul><ul><ul><li> ME -> <attr> </li></ul></ul><ul><ul><li>To call a method in the same class </li></ul></ul><ul><li> CALL METHOD ME -> <meth> </li></ul><ul><li>Self references allow an object to give other objects a reference to it. </li></ul>
  38. 38. Example (1 of 4) <ul><li>Class Definition. </li></ul><ul><li>CLASS cl_airplane DEFINITION. </li></ul><ul><li>PUBLIC SECTION. </li></ul><ul><li>CONSTANTS: pos_1 TYPE i VALUE 30. </li></ul><ul><li>METHODS: set_attributes IMPORTING &quot; Instance Methods </li></ul><ul><li> im_name TYPE string </li></ul><ul><li> im_planetype TYPE saplane-planetype, </li></ul><ul><li> display_attributes. </li></ul><ul><li>CLASS-METHODS: display_n_o_airplanes. &quot; Static Methods </li></ul><ul><li>PRIVATE SECTION. </li></ul><ul><li>DATA : name TYPE string, </li></ul><ul><li>planetype TYPE saplane-planetype. &quot; Instance Attributes </li></ul><ul><li>CLASS-DATA: n_o_airplanes TYPE i. &quot; Static Attributes </li></ul><ul><li>ENDCLASS. &quot;c_airplane DEFINITION </li></ul>
  39. 39. Example (2 of 4) <ul><li>Class Implementation </li></ul><ul><li>CLASS cl_airplane IMPLEMENTATION. </li></ul><ul><li>METHOD set_attributes. </li></ul><ul><ul><li>DATA: name TYPE string VALUE 'AIRBUS', </li></ul></ul><ul><ul><li>planetype TYPE saplane-planetype VALUE 'A-320'. </li></ul></ul><ul><ul><li>IF im_name IS INITIAL AND im_planetype IS INITIAL. </li></ul></ul><ul><ul><ul><li>name = name. </li></ul></ul></ul><ul><ul><ul><li>me->planetype = planetype. </li></ul></ul></ul><ul><ul><ul><li>me->n_o_airplanes = me->n_o_airplanes + 1. </li></ul></ul></ul><ul><ul><li>ELSE. </li></ul></ul><ul><ul><ul><li>me->name = im_name. </li></ul></ul></ul><ul><ul><ul><li>me->planetype = im_planetype. </li></ul></ul></ul><ul><ul><ul><li>n_o_airplanes = n_o_airplanes + 1. </li></ul></ul></ul><ul><ul><li>ENDIF. </li></ul></ul><ul><li>ENDMETHOD. &quot;set_attributes </li></ul>
  40. 40. Example (3 of 4) <ul><li>METHOD display_attributes. </li></ul><ul><li>WRITE : / 'Name of airplane:'(001), AT pos_1 name, </li></ul><ul><li>/ 'Airplane type: '(002), AT pos_1 planetype. </li></ul><ul><li>ENDMETHOD. &quot;display_attributes </li></ul><ul><li>METHOD display_n_o_airplanes. </li></ul><ul><li>WRITE: /,/ 'Total number of planes'(cal), </li></ul><ul><li>AT pos_1 n_o_airplanes LEFT-JUSTIFIED, /. </li></ul><ul><li>ENDMETHOD. &quot;display_n_o_airplanes </li></ul><ul><li>ENDCLASS. &quot;c_airplane IMPLEMENTATION </li></ul>
  41. 41. Example (4 of 4) <ul><li>Creating class reference and accessing the class attributes & methods </li></ul><ul><li>DATA: r_plane TYPE REF TO cl_airplane. &quot; creating the class reference </li></ul><ul><li>START-OF-SELECTION. </li></ul><ul><li>CREATE OBJECT r_plane. </li></ul><ul><li>CALL METHOD r_plane -> set_attributes( im_name = 'BOEING' </li></ul><ul><li> im_planetype = '747-200' ). </li></ul><ul><li>CALL METHOD r_plane -> display_attributes( ). </li></ul><ul><li>CALL METHOD cl_airplane => display_n_o_airplanes( ) . </li></ul>

×