6 Columns with Active Background
6 Columns with Active Background
Active List Item Background

24OOP2908 – Types of Visibility Section in OO ABAP

In object-oriented ABAP programming, Visibility refers to the accessibility of class members (attributes and methods) from different parts of the program. There are three types of visibility sections in OO ABAP: public, private and protected. In this blog post, we will discuss each of these visibility sections in detail and their significance in object-oriented programming.

  1. Public Visibility:

Public visibility is the default visibility section in ABAP. It means that the class members declared as public can be accessed from anywhere in the program, including outside the class. This type of visibility is useful when you want to make certain attributes or methods accessible to other classes or programs. Public members can be accessed using the dot notation, i.e., <object_name>-><attribute_name> or <object_name>-><method_name>.

Example:

CLASS lcl_employee DEFINITION.

  PUBLIC SECTION.

    DATA: emp_id TYPE i,

          emp_name TYPE string.

    METHODS: get_details.

ENDCLASS.

 

In the above example, both the attributes emp_id and emp_name are declared as public, which means they can be accessed from outside the class. The method get_details can also be accessed from outside the class.

 

  1. Private Visibility:

Private visibility restricts the access of class members to only within the class. This means that private attributes and methods cannot be accessed from outside the class. Private members are only accessible within the class methods. This type of visibility is useful when you want to hide certain implementation details of the class from the outside world.

Example:

CLASS lcl_employee DEFINITION.

  PRIVATE SECTION.

    DATA: emp_salary TYPE p,

          emp_department TYPE string.

    METHODS: calculate_bonus,

             update_salary.

ENDCLASS.

 

In the above example, the attributes emp_salary and emp_department are declared as private, which means they can only be accessed within the class methods. The methods calculate_bonus and update_salary can also only be accessed within the class.

 

  1. Protected Visibility:

Protected visibility is similar to private visibility, but it also allows access to the class members from its subclasses. This means that protected attributes and methods can be accessed within the class and its subclasses, but not from outside the class hierarchy. This type of visibility is useful when you want to restrict access to certain class members, but still want them to be accessible to subclasses.

Example:

CLASS lcl_employee DEFINITION.

  PROTECTED SECTION.

    DATA: emp_address TYPE string,

          emp_phone TYPE string.

    METHODS: get_address,

             get_phone.

ENDCLASS.

 

CLASS lcl_manager DEFINITION INHERITING FROM lcl_employee.

  PUBLIC SECTION.

    METHODS: get_details.

ENDCLASS.

 

In the above example, the attributes emp_address and emp_phone are declared as protected, which means they can be accessed within the class and its subclasses. The method get_details in the subclass lcl_manager can access these protected attributes.

 

Author : Aniket Pawar, 9373518385                                                     

24OOP2908 – Types of Visibility Section in OO ABAP

In object-oriented ABAP programming, Visibility refers to the accessibility of class members (attributes and methods) from different parts of the program. There are three types of visibility sections in OO ABAP: public, private and protected. In this blog post, we will discuss each of these visibility sections in detail and their significance in object-oriented programming.

  1. Public Visibility:

Public visibility is the default visibility section in ABAP. It means that the class members declared as public can be accessed from anywhere in the program, including outside the class. This type of visibility is useful when you want to make certain attributes or methods accessible to other classes or programs. Public members can be accessed using the dot notation, i.e., <object_name>-><attribute_name> or <object_name>-><method_name>.

Example:

CLASS lcl_employee DEFINITION.

  PUBLIC SECTION.

    DATA: emp_id TYPE i,

          emp_name TYPE string.

    METHODS: get_details.

ENDCLASS.

 

In the above example, both the attributes emp_id and emp_name are declared as public, which means they can be accessed from outside the class. The method get_details can also be accessed from outside the class.

 

  1. Private Visibility:

Private visibility restricts the access of class members to only within the class. This means that private attributes and methods cannot be accessed from outside the class. Private members are only accessible within the class methods. This type of visibility is useful when you want to hide certain implementation details of the class from the outside world.

Example:

CLASS lcl_employee DEFINITION.

  PRIVATE SECTION.

    DATA: emp_salary TYPE p,

          emp_department TYPE string.

    METHODS: calculate_bonus,

             update_salary.

ENDCLASS.

 

In the above example, the attributes emp_salary and emp_department are declared as private, which means they can only be accessed within the class methods. The methods calculate_bonus and update_salary can also only be accessed within the class.

 

  1. Protected Visibility:

Protected visibility is similar to private visibility, but it also allows access to the class members from its subclasses. This means that protected attributes and methods can be accessed within the class and its subclasses, but not from outside the class hierarchy. This type of visibility is useful when you want to restrict access to certain class members, but still want them to be accessible to subclasses.

Example:

CLASS lcl_employee DEFINITION.

  PROTECTED SECTION.

    DATA: emp_address TYPE string,

          emp_phone TYPE string.

    METHODS: get_address,

             get_phone.

ENDCLASS.

 

CLASS lcl_manager DEFINITION INHERITING FROM lcl_employee.

  PUBLIC SECTION.

    METHODS: get_details.

ENDCLASS.

 

In the above example, the attributes emp_address and emp_phone are declared as protected, which means they can be accessed within the class and its subclasses. The method get_details in the subclass lcl_manager can access these protected attributes.

 

Author : Aniket Pawar, 9373518385