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: 

Reg: Public and Private class

radhushankar
Participant
0 Kudos

Hi all

I am new to abab objects and i got the technical details about public as well as private section..can any one let me in which case we have to go for public and in which case we have to go for private section and what are the limitation in both the cases??

Thanks

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Declare all your Methods in Public section and all your data in the private section.

Use your methods to access the private data.

Thanks,

Karthik

15 REPLIES 15

Former Member
0 Kudos

Declare all your Methods in Public section and all your data in the private section.

Use your methods to access the private data.

Thanks,

Karthik

0 Kudos

Hello Radhu

You will find many ABAP classes where you have public instance attributes yet they are read-only, e.g. (IF_PT_EMPLOYEE~PERNR of CL_PT_EMPLOYEE).

Adavantage of such an approach (instead of a GET method for the PERNR) is the more direct access to this key attribute of the class.

Regards

Uwe

matt
Active Contributor
0 Kudos

>

> Declare all your Methods in Public section and all your data in the private section.

> Use your methods to access the private data.

>

> Thanks,

> Karthik

A good start, but not right

Only methods that are designed to be accessed outside of the class should be public. Methods that are designed to be used only by the class or its subclasses should be protected. Methods that are designed to be use only by the class should be private.

Same goes for attributes, types etc.

Essentially, everything should be private, unless it needs to be otherwise.

Some developers will tell you that ALL attributes should be private/protected, and they should only be accessible, publicly, through setter and getter methods. This is my preferred approach, but I wouldn't insist on it.

0 Kudos

Hey All,

I would agree with Matthew's approach. But more generally its a question of design. While thinking about the design of your application/module or whatever, you should carefully take into consideration who should have access to what and who should be allowed to see what.

When implementing a pattern, e.g. singleton, there are certain rules (a private constructor which is called by a public getInstance Method) which you should stick to or take into account.

So there is no general answer on what kind of accessibility or access you should choose, it always depends on your application and how you would like others to use it and be save that nothing brakes. Just be aware of what different techniques really mean and try to use them to solve problems at design-time.

A tutorial, like suggested before, can give you the security to understand the impact of declaring something as private, protected or public and by that increase your confidence that you know what you are doing.

Cheers,

Justus

Former Member
0 Kudos

This message was moderated.

Former Member
0 Kudos

This message was moderated.

former_member69765
Contributor
0 Kudos

Hi..

sometimes a little googling helps...

Check this : http://www.abaplearning.com/abap-tutorials/abap-objects

A very good example of public, private and protected attributes / methods is given.

Check it out ..

Former Member
0 Kudos

This message was moderated.

Former Member
0 Kudos

Hi,

ABAP OBJECTS is an Object Oriented part of ABAP. Now to start with ABAP Objects, I would suggest you to first read some documentation on Object Oriented Programming.

Private attributes are the attributes which are declared in Private section of a class. These attrbutes cannot be accessed from outside the class. This is called Information Hiding or Encapsulation.

Public attributes are those attributes which can be accessed by outside user.They are declared in PUBLIC SECTION of class.

Check this link, this link will guide you on ABAP OBJECTS.

http://www.sap-press.de/katalog/buecher/htmlleseproben/gp/htmlprobID-28?GalileoSession=22448306A3l7U...

Regards

Abhijeet

0 Kudos

hi abhi,

That material is simply awesome dude..thanks a lot.

Former Member
0 Kudos

This message was moderated.

matt
Active Contributor
0 Kudos

Same example as Sridevi D

Did you both cut and paste from the same source?

0 Kudos

Most probably

narin_nandivada3
Active Contributor
0 Kudos

Hi Shankar,

Please check these threads

Check this WIKI for example regarding VISIBILITY SECTION.

https://www.sdn.sap.com/irj/sdn/wiki?path=/display/snippets/abapObjects-CreatingyourFirstLocalClass-Definingthe+Visibility

If you want more.. Search SDN for these kind of questions...

Good luck

Narin

Former Member
0 Kudos

hi Radhu Shankar G

if u want tht ur one class element is access by the other class by inhert tht call...thn u declare tht class as public..or the element of tht as public....

nd if u want tht no other class can access the elemt of the base tthn declarre it as private....nd onn inheritance these class element r nt access by the other class object...fine..

regards

Ritesh