SAP ABAP : OOPS Concept - Classes And Objects

 Classes and Objects are basic concepts of Object Oriented Programming which revolve around the real life entities.


Class

A class is a user defined blueprint or prototype from which objects are created.  It represents the set of properties or methods that are common to all objects of one type.

Example :  A blueprint of house .

 Using a blueprint we can make multiple houses using our own name, color,floor etc .

Example : An animal

An animal is just a blueprint . Animal can be dog , cat , mouse etc .

Class declarations can include these components, in order:  

  1. Modifiers: A class can be public or has default access.
  2. Class keyword: class keyword is used to create a class.
  3. Class name: The name should begin with an lcl for local class and zcl for Global class.
  4. Superclass(if any): The name of the class’s parent (superclass), if any, preceded by the keyword INHERITING FROM. A class can only INHERIT (subclass) one parent.
  5. Interfaces(if any): A comma-separated list of interfaces implemented by the class, if any, preceded by the keyword implements. A class can implement more than one interface.
  6. Body: The class body surrounded by Class and Endclass.
Example :


Object

 It is a basic unit of Object-Oriented Programming and represents the real life entities.  A typical Java program creates many objects, which as you know, interact by invoking methods.
   An object consists of : 
  1. State: It is represented by attributes of an object. It also reflects the properties of an object.
  2. Behavior: It is represented by methods of an object. It also reflects the response of an object with other objects.
  3. Identity: It gives a unique name to an object and enables one object to interact with other objects.

Example of an object: dog



Code Example :
Create object of class

class lcl_animal DEFINITION"class declaration
  PUBLIC SECTION"access modifier
   DATA name TYPE char10."attributes
   METHODS m_eat "behaviours
ENDCLASS.

DATA lv_obj TYPE REF TO lcl_animal"declaring object of class

"there is two way to create object of class after release 7.4

CREATE OBJECT lv_obj. "or

DATA(lv_obj1) = NEW lcl_animal( )."incline declaration with New keyword "supported only release after 7.4 ve

CLASS lcl_animal IMPLEMENTATION.
  METHOD m_eat.
  ENDMETHOD.
ENDCLASS.


Initializing an object

The new operator or create object instantiates a class by allocating memory for a new object and returning a reference to that memory. The new operator or create object also invokes the class constructor. 


REPORT ztest_demo_sup.
class lcl_animal DEFINITION"class declaration
  PUBLIC SECTION"access modifier
   DATA name TYPE char10."attributes
   METHODS constructor IMPORTING name TYPE char10.
   METHODS m_eat "behaviours
ENDCLASS.
DATA lv_dog TYPE REF TO lcl_animal"declaring object of class

"there is two way to create object of class after release 7.4
CREATE OBJECT lv_dog EXPORTING name 'Milo'"or

DATA(lv_catNEW lcl_animalname 'Kitty')."incline declaration with New keyword "supported only release after 7.4 ve

CLASS lcl_animal IMPLEMENTATION.
  METHOD constructor.

    WRITE / name.

  ENDMETHOD.
  METHOD m_eat.
  ENDMETHOD.
ENDCLASS.



                                             SAP ABAP Supports Two Types of constructor .


Examples :






   

Comments

Popular posts from this blog

SAP ABAP : OOPS Concept - Inheritance

SAP ABAP : OOPS Concept - Encapsulation