Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

OOPS ABAP

Former Member
0 Kudos

Hi All,

Can Somebody tell me how to use Private & Protected Sections & Also Uisng Inheritance in OOPS Abap.

Regards,

Ravi S

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Visibility Sections

You can divide the declaration part of a class into up to three visibility areas:

CLASS class DEFINITION.

PUBLIC SECTION.

PROTECTED SECTION.

PRIVATE SECTION.

ENDCLASS.

These areas define the external visibility of the class components, that is, the interface between the class and its users. Each component of a class must be assigned to one of the visibility sections.

Public Section

All of the components declared in the public section are accessible to all users of the class, and to the methods of the class and any classes that inherit from it. The public components of the class form the interface between the class and its users.

Protected Section

All of the components declared in the protected section are accessible to all methods of the class and of classes that inherit from it. Protected components form a special interface between a class and its subclasses. Since inheritance is not active in Release 4.5B, the protected section currently has the same effect as the private section.

Private Section

Components that you declare in the private section are only visible in the methods of the same class. The private components are not part of the external interface of the class.

Inheritance -

Inheritance allows you to derive a new class from an existing class. You do this using the INHERITING FROM addition in the

CLASS subclass DEFINITION INHERITING FROM superclass.

statement. The new class subclass inherits all of the components of the existing class superclass. The new class is called the subclass of the class from which it is derived. The original class is called the superclass of the new class.

If you do not add any new declarations to the subclass, it contains the same components as the superclass. However, only the public and protected components of the superclass are visible in the subclass. Although the private components of the superclass exist in the subclass, they are not visible. You can declare private components in a subclass that have the same names as private components of the superclass. Each class works with its own private components. Methods that a subclass inherits from a superclass use the private attributes of the superclass, and not any private components of the subclass with the same names.

If the superclass does not have a private visibility section, the subclass is an exact replica of the superclass. However, you can add new components to the subclass. This allows you to turn the subclass into a specialized version of the superclass. If a subclass is itself the superclass of further classes, you introduce a new level of specialization.

A class can have more than one direct subclass, but it may only have one direct superclass. This is called single inheritance. When subclasses inherit from superclasses and the superclass is itself the subclass of another class, all of the classes involved form an inheritance tree, whose degree of specialization increases with each new hierarchical level you add. Conversely, the classes become more generalized until you reach the root node of the inheritance tree. The root node of all inheritance trees in ABAP Objects is the predefined empty class OBJECT. This is the most generalized class possible, since it contains neither attributes nor methods. When you define a new class, you do not have to specify it explicitly as the superclass - the relationship is always implicitly defined. Within an inheritance tree, two adjacent nodes are the direct superclass or direct subclass of one another. Other related nodes are referred to as superclasses and subclasses. The component declarations in a subclass are distributed across all levels of the inheritance tree.

Example -

http://help.sap.com/saphelp_nw2004s/helpdata/en/dd/4049c40f4611d3b9380000e8353423/frameset.htm

REPORT demo_inheritance.

CLASS counter DEFINITION.
PUBLIC SECTION.
    METHODS: set IMPORTING value(set_value) TYPE i,
             increment,
             get EXPORTING value(get_value) TYPE i.
  PROTECTED SECTION.
    DATA count TYPE i.
ENDCLASS.

CLASS counter IMPLEMENTATION.
  METHOD set.
    count = set_value.
  ENDMETHOD.
  METHOD increment.
    ADD 1 TO count.
  ENDMETHOD.
  METHOD get.
    get_value = count.
  ENDMETHOD.
ENDCLASS.

CLASS counter_ten DEFINITION INHERITING FROM counter.
PUBLIC SECTION.
    METHODS increment REDEFINITION.
    DATA count_ten.
ENDCLASS.

CLASS counter_ten IMPLEMENTATION.
  METHOD increment.
    DATA modulo TYPE i.
    CALL METHOD super->increment.
    write / count.
    modulo = count mod 10.
    IF modulo = 0.
      count_ten = count_ten + 1.
      write count_ten.
    ENDIF.
  ENDMETHOD.
ENDCLASS.

DATA: count TYPE REF TO counter,
      number TYPE i VALUE 5.

START-OF-SELECTION.

  CREATE OBJECT count TYPE counter_ten.

  CALL METHOD count->set EXPORTING set_value = number.

  DO 20 TIMES.
    CALL METHOD count->increment.
  ENDDO.

The class counter_ten is derived from counter. It redefines the method increment. To do this, you must change the visibility of the count attribute from PRIVATE to PROTECTED. The redefined method calls the obscured method of the superclass using the pseudoreference super->. The redefined method is a specialization of the inherited method.

The example instantiates the subclass. The reference variable pointing to it has the type of the superclass. When the INCREMENT method is called using the superclass reference, the system executes the redefined method from the subclass.

Have a look at below link and go to page 1291. It will give you good info abt OO ABAP.

http://help.sap.com/printdocu/core/Print46c/en/data/pdf/BCABA/BCABA.pdf

Also have a look at below links:

http://help.sap.com/saphelp_nw2004s/helpdata/en/c3/225b5654f411d194a60000e8353423/content.htm

http://esnips.com/doc/5c65b0dd-eddf-4512-8e32-ecd26735f0f2/prefinalppt.ppt

http://esnips.com/doc/2c76dc57-e74a-4539-a20e-29383317e804/OO-abap.pdf

http://esnips.com/doc/0ef39d4b-586a-4637-abbb-e4f69d2d9307/SAP-CONTROLS-WORKSHOP.pdf

http://esnips.com/doc/92be4457-1b6e-4061-92e5-8e4b3a6e3239/Object-Oriented-ABAP.ppt

http://esnips.com/doc/448e8302-68b1-4046-9fef-8fa8808caee0/abap-objects-by-helen.pdf

http://esnips.com/doc/39fdc647-1aed-4b40-a476-4d3042b6ec28/class_builder.ppt

Please do check transaction ABAPDOCU to learn more about ABAP OO.

I hope it helps.

Best Regards,

Vibha

Please mark all the helpful answers

5 REPLIES 5

Former Member
0 Kudos

Visibility Sections

You can divide the declaration part of a class into up to three visibility areas:

CLASS class DEFINITION.

PUBLIC SECTION.

PROTECTED SECTION.

PRIVATE SECTION.

ENDCLASS.

These areas define the external visibility of the class components, that is, the interface between the class and its users. Each component of a class must be assigned to one of the visibility sections.

Public Section

All of the components declared in the public section are accessible to all users of the class, and to the methods of the class and any classes that inherit from it. The public components of the class form the interface between the class and its users.

Protected Section

All of the components declared in the protected section are accessible to all methods of the class and of classes that inherit from it. Protected components form a special interface between a class and its subclasses. Since inheritance is not active in Release 4.5B, the protected section currently has the same effect as the private section.

Private Section

Components that you declare in the private section are only visible in the methods of the same class. The private components are not part of the external interface of the class.

Inheritance -

Inheritance allows you to derive a new class from an existing class. You do this using the INHERITING FROM addition in the

CLASS subclass DEFINITION INHERITING FROM superclass.

statement. The new class subclass inherits all of the components of the existing class superclass. The new class is called the subclass of the class from which it is derived. The original class is called the superclass of the new class.

If you do not add any new declarations to the subclass, it contains the same components as the superclass. However, only the public and protected components of the superclass are visible in the subclass. Although the private components of the superclass exist in the subclass, they are not visible. You can declare private components in a subclass that have the same names as private components of the superclass. Each class works with its own private components. Methods that a subclass inherits from a superclass use the private attributes of the superclass, and not any private components of the subclass with the same names.

If the superclass does not have a private visibility section, the subclass is an exact replica of the superclass. However, you can add new components to the subclass. This allows you to turn the subclass into a specialized version of the superclass. If a subclass is itself the superclass of further classes, you introduce a new level of specialization.

A class can have more than one direct subclass, but it may only have one direct superclass. This is called single inheritance. When subclasses inherit from superclasses and the superclass is itself the subclass of another class, all of the classes involved form an inheritance tree, whose degree of specialization increases with each new hierarchical level you add. Conversely, the classes become more generalized until you reach the root node of the inheritance tree. The root node of all inheritance trees in ABAP Objects is the predefined empty class OBJECT. This is the most generalized class possible, since it contains neither attributes nor methods. When you define a new class, you do not have to specify it explicitly as the superclass - the relationship is always implicitly defined. Within an inheritance tree, two adjacent nodes are the direct superclass or direct subclass of one another. Other related nodes are referred to as superclasses and subclasses. The component declarations in a subclass are distributed across all levels of the inheritance tree.

Example -

http://help.sap.com/saphelp_nw2004s/helpdata/en/dd/4049c40f4611d3b9380000e8353423/frameset.htm

REPORT demo_inheritance.

CLASS counter DEFINITION.
PUBLIC SECTION.
    METHODS: set IMPORTING value(set_value) TYPE i,
             increment,
             get EXPORTING value(get_value) TYPE i.
  PROTECTED SECTION.
    DATA count TYPE i.
ENDCLASS.

CLASS counter IMPLEMENTATION.
  METHOD set.
    count = set_value.
  ENDMETHOD.
  METHOD increment.
    ADD 1 TO count.
  ENDMETHOD.
  METHOD get.
    get_value = count.
  ENDMETHOD.
ENDCLASS.

CLASS counter_ten DEFINITION INHERITING FROM counter.
PUBLIC SECTION.
    METHODS increment REDEFINITION.
    DATA count_ten.
ENDCLASS.

CLASS counter_ten IMPLEMENTATION.
  METHOD increment.
    DATA modulo TYPE i.
    CALL METHOD super->increment.
    write / count.
    modulo = count mod 10.
    IF modulo = 0.
      count_ten = count_ten + 1.
      write count_ten.
    ENDIF.
  ENDMETHOD.
ENDCLASS.

DATA: count TYPE REF TO counter,
      number TYPE i VALUE 5.

START-OF-SELECTION.

  CREATE OBJECT count TYPE counter_ten.

  CALL METHOD count->set EXPORTING set_value = number.

  DO 20 TIMES.
    CALL METHOD count->increment.
  ENDDO.

The class counter_ten is derived from counter. It redefines the method increment. To do this, you must change the visibility of the count attribute from PRIVATE to PROTECTED. The redefined method calls the obscured method of the superclass using the pseudoreference super->. The redefined method is a specialization of the inherited method.

The example instantiates the subclass. The reference variable pointing to it has the type of the superclass. When the INCREMENT method is called using the superclass reference, the system executes the redefined method from the subclass.

Have a look at below link and go to page 1291. It will give you good info abt OO ABAP.

http://help.sap.com/printdocu/core/Print46c/en/data/pdf/BCABA/BCABA.pdf

Also have a look at below links:

http://help.sap.com/saphelp_nw2004s/helpdata/en/c3/225b5654f411d194a60000e8353423/content.htm

http://esnips.com/doc/5c65b0dd-eddf-4512-8e32-ecd26735f0f2/prefinalppt.ppt

http://esnips.com/doc/2c76dc57-e74a-4539-a20e-29383317e804/OO-abap.pdf

http://esnips.com/doc/0ef39d4b-586a-4637-abbb-e4f69d2d9307/SAP-CONTROLS-WORKSHOP.pdf

http://esnips.com/doc/92be4457-1b6e-4061-92e5-8e4b3a6e3239/Object-Oriented-ABAP.ppt

http://esnips.com/doc/448e8302-68b1-4046-9fef-8fa8808caee0/abap-objects-by-helen.pdf

http://esnips.com/doc/39fdc647-1aed-4b40-a476-4d3042b6ec28/class_builder.ppt

Please do check transaction ABAPDOCU to learn more about ABAP OO.

I hope it helps.

Best Regards,

Vibha

Please mark all the helpful answers

Former Member
0 Kudos

Hi,

Check the below link for more reference....

http://help.sap.com/saphelp_47x200/helpdata/en/dd/4049c40f4611d3b9380000e8353423/content.htm

Regards

Kiran Sure

Former Member
0 Kudos

Hi,

Thanks to all for ur answers.But When to Use a private Section & When to Use a Public section in real time examples.

Regards,

Ravi S

0 Kudos

Public attributes are defined in the PUBLIC section and can be viewed and changed from outside the class. There is direct access to public attributes.

Private attributes are defined in the PRIVATE section. The can only be viewes and changed from within the class. There is no direct access from outside the class.

0 Kudos

Hi Vibha ,

CAn U Give me a real time example Using most of the features of OOPS Like Visibility,Constructors, Methods (Static or Instance) Etc.

Regards,

Ravi S.