Application Development and Automation 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: 
Read only

private

Former Member
0 Likes
715

why should we declare attributes in a local class with in a private section.Is it mandatory and particularly in case of inheritance we should decalre the attributes in a public section or in a private section.

1 ACCEPTED SOLUTION
Read only

uwe_schieferstein
Active Contributor
0 Likes
677

Hello Sandeep

Declaring attributes as private is not a matter of global vs. local classes. If you want to inherit an attribute then you usually define it as <i>protected</i>.

However, if an attribute has only a meaning for a given class (and not any of its subclasses) and it is of no interest to calling "parties" then I would define the attribute as private.

In general, most attributes will be private. Perhaps a few very important attribute may be <i>public </i>yet <i>read-only </i>(e.g. key values of an instance).

Regards

Uwe

4 REPLIES 4
Read only

uwe_schieferstein
Active Contributor
0 Likes
678

Hello Sandeep

Declaring attributes as private is not a matter of global vs. local classes. If you want to inherit an attribute then you usually define it as <i>protected</i>.

However, if an attribute has only a meaning for a given class (and not any of its subclasses) and it is of no interest to calling "parties" then I would define the attribute as private.

In general, most attributes will be private. Perhaps a few very important attribute may be <i>public </i>yet <i>read-only </i>(e.g. key values of an instance).

Regards

Uwe

Read only

Former Member
0 Likes
677

we need to declare attributes in a local class with in a private section

to avoid access by others class means only objects of that class can access it.

For Ex. In a company some documents are private and confidencial only employee can see it. same way only objects of that calss can acess private objects.

if you are going with inheritance we need decalre the attributes in a public section so that inherited class can access it. you can also use Protedted class wich can be access by inherited class

check links

OOPS – OO ABAP

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

http://www.amazon.com/gp/explorer/0201750805/2/ref=pd_lpo_ase/102-9378020-8749710?ie=UTF8

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

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

Rewards if useful......................

Minal

Read only

Former Member
0 Likes
677

Hi Sandeep,

There is no Hard and Fast rule telling you to declare attributes in Private Section..This is advised just make a good design.The effect of declaring attributes in private section and your methods in public section is that your defining a rule saying that the only way the attributes value can be changed is through your methods..It is a way of control.

Visibility:

Public:visible to all

Private:visible only to class.

Protected:Visible to class and its sub class.

Regards,

Abhishek

Read only

Former Member
0 Likes
677

Hi Sandeep,

As the name 'Local class' indicates it is defined in your program.it cannot be accessed outside your program.because Objects restrict the visibility of their resources (attributes and methods) to other users. Every object has an interface, which determines how other objects can interact with it. The implementation of the object is encapsulated, i.e. invisible outside the object itself. thats why we should declare attributes in a local class with in a private section.

For your reference, i given some additional points here:

Encapsulation is implemented using class visibility sections.

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

PUBLIC SECTION.

PROTECTED SECTION.

PRIVATE SECTION.

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.

READ-ONLY addition can be used for public attributes. This implies that the public attributes can be read from outside the class, but can be modified by only methods of the class.

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.

Protected Section

All components in the protected section can be accessed by all methods of the same class as well as all methods of its sub-classes.

All of the components in the protected section can access the components from the public section. In turn, they can be accessed by the components of the private section.

In case of 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.

Only the public and protected components of the superclass are visible in the subclass.

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.

Thanks,

Usha