Summary -

In this topic, we described about the below sections -

Objects establish the key concept in object oriented programming. An object is a self-contained unit that has its own status, behavior and identity. Objects are patterns or instances of classes. An object has the following three main characteristics −

  • State
  • Behaviour
  • Identity

The object behavior is determined by its methods and refers to the changes that occur in its attributes over a period. Each object has a unique identity that can be used to distinguish it from other objects. Two objects may exhibit the same behaviour however those may or may not have the same state and they never have the same identity. The object behaviours or methods represents the object performed actions. Objects creates a container that contain data which combines the character to the respective behavior to provide services. An object is accessed referring via reference variables to the address.

The object identity is determined by the address in memory. Likewise, the object identity never change throughout its lifetime. Objects can interact with another object by sending messages. An object in a program that performs a certain tasks that should reflect a real object of the task. A clear difference can be made between the public, private and protected components with objects.

The object state is described as a set of attributes and their values. For example, a bank account has a set of attributes such as Account Number, Name, Account Type, Balance.

One object can interact with another by accessing its attributes directly in a method, or calling methods, or triggering an event.

The services are known as methods also known as operations or functions. The attributes of an object can't be changed directly by the user, however those can changed only by the methods of the object.

In programming, Objects can be used as user-defined data type with help of class and contain code to manipulate the data. Objects are called as variables of the type class and we can create any number of objects belongs to specific class after defining it.

Creating an Object -

Creating an object is of two types based on how it is defined. Those are -

  • Creating a reference variable to the class. The syntax is −
    DATA: <obj_name> TYPE REF TO <class_name>.

  • Creating an object from the reference variable. The syntax is −
    CREATE Object: <obj	_name>.


Example -

Simple example to create class object.


Code -

*&---------------------------------------------------------------------*
*& Report  Z_OBJECTS
*&---------------------------------------------------------------------*
*& Written by TutorialsCampus
*&---------------------------------------------------------------------*

REPORT  Z_OBJECTS.

* Class definition
CLASS classnew DEFINITION.
PUBLIC SECTION.

* Defining Method
  METHODS: methodnew.
ENDCLASS.

*Class implementation
CLASS classnew IMPLEMENTATION.

* Method Implementation
  METHOD methodnew.
     Write:/ 'Classnew public method: methodnew'.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.

* Defining a class object
DATA: objectnew TYPE REF TO classnew.

* Creating object objectnew
CREATE OBJECT objectnew.

* Calling method using object.
CALL METHOD: objectnew->methodnew.

Output -

Objects Example Output

Explaining Example -

In the above example, each and every statement is preceeded with a comment to explain about the statement. Go through them to get clear understanding of example code.

classnew is the class defined with a public method methodnew. The methodnew implementation coded in classnew implementation. objectnew declared as of type reference to class classnew.

CREATE OBJECT creates the objectnew object. objectnew->methodnew calls the method methodnew and executes the code in method implementation.