Saturday, June 1, 2013

OOP in ABAP Part 2 (Examples on Constructor, Class Constructor, Inheritance & Interface )

 1.       Constructor
Constructor is a special instance method and always named CONSTRUCTOR. It is automatically called at runtime with CREATE OBJECT statement. It must be defined in public area. It can have only importing parameters and exceptions. On the other hand static constructor or class constructor is always named CLASS_CONSTRUCTOR. It is called automatically when the class is accessed for the first time. It can’t have parameters or exceptions. It can’t be called explicitly.

Example on constructor:

2.       Inheritance
A class can inherits all properties from another class termed as super class. The subclass can add new components and can replace the implementation of inherited methods. Here the relationship is IS A relation and also termed as specialization. In ABAP there is no multiple inheritance as only one superclass can be specified directly above a class. A superclass is a generalization of subclass. Inheritance is a one sided relationship i.e. the subclass can recognize the superclass but the parent class can’t recognize the child class. During redefinition the signature of superclass method can’t be changed. To access the original components of the parent class subclass can use SUPER. For constructor both the signature and implementation can be changed. Constructor of direct superclass must be called within the constructor of subclass.
Only protected and public components are visible in subclass. Static methods of superclass can’t be redefined. Private components of super class only are accessed via public and protected methods.
Assignments of instance of subclass back to superclass instance is called narrowing cast or up cast and the reverse is called widening cast or downcast.in downcast the instance can only access the shared components. For widening cast error CX_SY_MOSVE_CAST_ERROR is triggered when there is a mismatch of content requirement.

Example on inheritance:

3.       Interface & Abstract Class
In simple words interface can be referred as superclass which can’t be instantiated and have no implementation part with only public section.  So interface has no implementation. It has only empty method declaration and its components are always public. But the visibility of components can be changed within implementing class using aliases.
If a new method is added to the interface then all the classes which implement the interface have to implement the new method else it will throw an error.

Abstract class is a special kind of class which can’t be instantiated .Abstract class should contain at least one abstract method (method without implementation) ,it can have methods with implementation. The subclass of abstract class can be instantiated if they are not abstract.
Unlike interface when a new non abstract method is added to class there is no need of redefining the same in subclasses.

Interface should be used when working with a wide range of objects which enforces no default behavior as it has no implementation and Abstract class is used when there is a need of closely related classes with partial default implementation.

Example on interface:

4.       Multiple Inheritance
Although a class can’t get inherited from two different classes but it can have more than one interface in its definition. The procedure to achieve multiple inheritance is shown pictorially below.

Pictorial View

184 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. This comment has been removed by a blog administrator.

    ReplyDelete
  3. This comment has been removed by a blog administrator.

    ReplyDelete
  4. This comment has been removed by a blog administrator.

    ReplyDelete
  5. This comment has been removed by a blog administrator.

    ReplyDelete
  6. This comment has been removed by a blog administrator.

    ReplyDelete
  7. This comment has been removed by a blog administrator.

    ReplyDelete
  8. This comment has been removed by a blog administrator.

    ReplyDelete
  9. This comment has been removed by a blog administrator.

    ReplyDelete
  10. This comment has been removed by a blog administrator.

    ReplyDelete
  11. This comment has been removed by a blog administrator.

    ReplyDelete
  12. This comment has been removed by a blog administrator.

    ReplyDelete
  13. This comment has been removed by a blog administrator.

    ReplyDelete
  14. This comment has been removed by a blog administrator.

    ReplyDelete
  15. This comment has been removed by a blog administrator.

    ReplyDelete
  16. This comment has been removed by a blog administrator.

    ReplyDelete
  17. This comment has been removed by a blog administrator.

    ReplyDelete
  18. This comment has been removed by a blog administrator.

    ReplyDelete
  19. This comment has been removed by a blog administrator.

    ReplyDelete
  20. This comment has been removed by a blog administrator.

    ReplyDelete
  21. This comment has been removed by a blog administrator.

    ReplyDelete
  22. This comment has been removed by a blog administrator.

    ReplyDelete
  23. This comment has been removed by a blog administrator.

    ReplyDelete
  24. This comment has been removed by a blog administrator.

    ReplyDelete
  25. This comment has been removed by a blog administrator.

    ReplyDelete
  26. This comment has been removed by a blog administrator.

    ReplyDelete
  27. This comment has been removed by a blog administrator.

    ReplyDelete
  28. This comment has been removed by a blog administrator.

    ReplyDelete
  29. This comment has been removed by a blog administrator.

    ReplyDelete
  30. This comment has been removed by a blog administrator.

    ReplyDelete
  31. This comment has been removed by a blog administrator.

    ReplyDelete
  32. This comment has been removed by a blog administrator.

    ReplyDelete
  33. This comment has been removed by a blog administrator.

    ReplyDelete
  34. This comment has been removed by a blog administrator.

    ReplyDelete
  35. This comment has been removed by a blog administrator.

    ReplyDelete
  36. This comment has been removed by a blog administrator.

    ReplyDelete
  37. This comment has been removed by a blog administrator.

    ReplyDelete
  38. This comment has been removed by a blog administrator.

    ReplyDelete
  39. This comment has been removed by a blog administrator.

    ReplyDelete
  40. This comment has been removed by a blog administrator.

    ReplyDelete
  41. This comment has been removed by a blog administrator.

    ReplyDelete
  42. This comment has been removed by a blog administrator.

    ReplyDelete
  43. This comment has been removed by a blog administrator.

    ReplyDelete
  44. This comment has been removed by a blog administrator.

    ReplyDelete
  45. This comment has been removed by a blog administrator.

    ReplyDelete
  46. This comment has been removed by a blog administrator.

    ReplyDelete
  47. This comment has been removed by a blog administrator.

    ReplyDelete
  48. This comment has been removed by a blog administrator.

    ReplyDelete
  49. This comment has been removed by a blog administrator.

    ReplyDelete
  50. This comment has been removed by a blog administrator.

    ReplyDelete
  51. This comment has been removed by a blog administrator.

    ReplyDelete
  52. This comment has been removed by a blog administrator.

    ReplyDelete
  53. This comment has been removed by a blog administrator.

    ReplyDelete
  54. This comment has been removed by a blog administrator.

    ReplyDelete
  55. This comment has been removed by a blog administrator.

    ReplyDelete
  56. This comment has been removed by a blog administrator.

    ReplyDelete
  57. This comment has been removed by a blog administrator.

    ReplyDelete
  58. This comment has been removed by a blog administrator.

    ReplyDelete
  59. This comment has been removed by a blog administrator.

    ReplyDelete
  60. This comment has been removed by a blog administrator.

    ReplyDelete
  61. Chanel Handbags Sale
    I couldn't resist commenting. Well written!

    ReplyDelete
  62. This comment has been removed by a blog administrator.

    ReplyDelete
  63. This comment has been removed by a blog administrator.

    ReplyDelete
  64. This comment has been removed by a blog administrator.

    ReplyDelete
  65. This comment has been removed by a blog administrator.

    ReplyDelete
  66. This comment has been removed by a blog administrator.

    ReplyDelete
  67. This comment has been removed by a blog administrator.

    ReplyDelete
  68. This comment has been removed by a blog administrator.

    ReplyDelete
  69. This comment has been removed by a blog administrator.

    ReplyDelete
  70. This comment has been removed by a blog administrator.

    ReplyDelete
  71. This comment has been removed by a blog administrator.

    ReplyDelete
  72. This comment has been removed by a blog administrator.

    ReplyDelete
  73. This comment has been removed by a blog administrator.

    ReplyDelete
  74. This comment has been removed by a blog administrator.

    ReplyDelete
  75. This comment has been removed by a blog administrator.

    ReplyDelete
  76. This comment has been removed by a blog administrator.

    ReplyDelete
  77. This comment has been removed by a blog administrator.

    ReplyDelete
  78. This comment has been removed by a blog administrator.

    ReplyDelete
  79. This comment has been removed by a blog administrator.

    ReplyDelete
  80. This comment has been removed by a blog administrator.

    ReplyDelete
  81. This comment has been removed by a blog administrator.

    ReplyDelete
  82. This comment has been removed by a blog administrator.

    ReplyDelete
  83. This comment has been removed by a blog administrator.

    ReplyDelete
  84. This comment has been removed by a blog administrator.

    ReplyDelete
  85. This comment has been removed by a blog administrator.

    ReplyDelete
  86. This comment has been removed by a blog administrator.

    ReplyDelete
  87. This comment has been removed by a blog administrator.

    ReplyDelete
  88. This comment has been removed by a blog administrator.

    ReplyDelete
  89. This comment has been removed by a blog administrator.

    ReplyDelete
  90. This comment has been removed by a blog administrator.

    ReplyDelete
  91. This comment has been removed by a blog administrator.

    ReplyDelete
  92. This comment has been removed by a blog administrator.

    ReplyDelete
  93. This comment has been removed by a blog administrator.

    ReplyDelete
  94. This comment has been removed by a blog administrator.

    ReplyDelete
  95. This comment has been removed by a blog administrator.

    ReplyDelete
  96. This comment has been removed by a blog administrator.

    ReplyDelete
  97. This comment has been removed by a blog administrator.

    ReplyDelete
  98. This comment has been removed by a blog administrator.

    ReplyDelete
  99. This comment has been removed by a blog administrator.

    ReplyDelete
  100. This comment has been removed by a blog administrator.

    ReplyDelete
  101. This comment has been removed by a blog administrator.

    ReplyDelete
  102. This comment has been removed by a blog administrator.

    ReplyDelete
  103. This comment has been removed by a blog administrator.

    ReplyDelete
  104. This comment has been removed by a blog administrator.

    ReplyDelete
  105. This comment has been removed by a blog administrator.

    ReplyDelete
  106. This comment has been removed by a blog administrator.

    ReplyDelete
  107. This comment has been removed by a blog administrator.

    ReplyDelete
  108. This comment has been removed by a blog administrator.

    ReplyDelete
  109. This comment has been removed by a blog administrator.

    ReplyDelete
  110. This comment has been removed by a blog administrator.

    ReplyDelete
  111. This comment has been removed by a blog administrator.

    ReplyDelete
  112. This comment has been removed by a blog administrator.

    ReplyDelete
  113. This comment has been removed by a blog administrator.

    ReplyDelete
  114. This comment has been removed by a blog administrator.

    ReplyDelete
  115. This comment has been removed by a blog administrator.

    ReplyDelete
  116. This comment has been removed by a blog administrator.

    ReplyDelete
  117. This comment has been removed by a blog administrator.

    ReplyDelete
  118. This comment has been removed by a blog administrator.

    ReplyDelete
  119. This comment has been removed by a blog administrator.

    ReplyDelete
  120. This comment has been removed by a blog administrator.

    ReplyDelete
  121. This comment has been removed by a blog administrator.

    ReplyDelete
  122. This comment has been removed by a blog administrator.

    ReplyDelete
  123. This comment has been removed by a blog administrator.

    ReplyDelete
  124. This comment has been removed by a blog administrator.

    ReplyDelete
  125. This comment has been removed by a blog administrator.

    ReplyDelete
  126. This comment has been removed by a blog administrator.

    ReplyDelete
  127. This comment has been removed by a blog administrator.

    ReplyDelete
  128. This comment has been removed by a blog administrator.

    ReplyDelete
  129. This comment has been removed by a blog administrator.

    ReplyDelete
  130. This comment has been removed by a blog administrator.

    ReplyDelete
  131. This comment has been removed by a blog administrator.

    ReplyDelete
  132. This comment has been removed by a blog administrator.

    ReplyDelete
  133. This comment has been removed by a blog administrator.

    ReplyDelete
  134. This comment has been removed by a blog administrator.

    ReplyDelete
  135. This comment has been removed by a blog administrator.

    ReplyDelete
  136. This comment has been removed by a blog administrator.

    ReplyDelete
  137. This comment has been removed by a blog administrator.

    ReplyDelete
  138. This comment has been removed by a blog administrator.

    ReplyDelete
  139. This comment has been removed by a blog administrator.

    ReplyDelete
  140. This comment has been removed by a blog administrator.

    ReplyDelete
  141. This comment has been removed by a blog administrator.

    ReplyDelete
  142. This comment has been removed by a blog administrator.

    ReplyDelete
  143. This comment has been removed by a blog administrator.

    ReplyDelete
  144. This comment has been removed by a blog administrator.

    ReplyDelete
  145. This comment has been removed by a blog administrator.

    ReplyDelete
  146. This comment has been removed by a blog administrator.

    ReplyDelete
  147. This comment has been removed by a blog administrator.

    ReplyDelete
  148. This comment has been removed by a blog administrator.

    ReplyDelete
  149. This comment has been removed by a blog administrator.

    ReplyDelete
  150. This comment has been removed by a blog administrator.

    ReplyDelete
  151. This comment has been removed by a blog administrator.

    ReplyDelete
  152. This comment has been removed by a blog administrator.

    ReplyDelete
  153. This comment has been removed by a blog administrator.

    ReplyDelete
  154. This comment has been removed by a blog administrator.

    ReplyDelete
  155. This comment has been removed by a blog administrator.

    ReplyDelete
  156. This comment has been removed by a blog administrator.

    ReplyDelete
  157. This comment has been removed by a blog administrator.

    ReplyDelete
  158. This comment has been removed by a blog administrator.

    ReplyDelete
  159. This comment has been removed by a blog administrator.

    ReplyDelete
  160. This comment has been removed by a blog administrator.

    ReplyDelete
  161. This comment has been removed by a blog administrator.

    ReplyDelete
  162. This comment has been removed by a blog administrator.

    ReplyDelete
  163. This comment has been removed by a blog administrator.

    ReplyDelete
  164. This comment has been removed by a blog administrator.

    ReplyDelete
  165. This comment has been removed by a blog administrator.

    ReplyDelete
  166. This comment has been removed by a blog administrator.

    ReplyDelete
  167. This comment has been removed by a blog administrator.

    ReplyDelete
  168. This comment has been removed by a blog administrator.

    ReplyDelete
  169. This comment has been removed by a blog administrator.

    ReplyDelete
  170. This comment has been removed by a blog administrator.

    ReplyDelete
  171. This comment has been removed by a blog administrator.

    ReplyDelete
  172. This comment has been removed by a blog administrator.

    ReplyDelete
  173. This comment has been removed by a blog administrator.

    ReplyDelete
  174. This comment has been removed by a blog administrator.

    ReplyDelete
  175. This comment has been removed by a blog administrator.

    ReplyDelete
  176. This comment has been removed by a blog administrator.

    ReplyDelete
  177. This comment has been removed by a blog administrator.

    ReplyDelete
  178. This comment has been removed by a blog administrator.

    ReplyDelete
  179. This comment has been removed by a blog administrator.

    ReplyDelete
  180. This comment has been removed by a blog administrator.

    ReplyDelete
  181. This comment has been removed by a blog administrator.

    ReplyDelete
  182. This comment has been removed by a blog administrator.

    ReplyDelete
  183. This comment has been removed by a blog administrator.

    ReplyDelete
  184. This comment has been removed by a blog administrator.

    ReplyDelete