Home ABAP Tutorials ABAP Objects Visibility of Attributes
Visibility of Attributes PDF Print E-mail
User Rating: / 6
PoorBest 
ABAP Tutorials - Object Oriented ABAP
Written by mastram   
Saturday, 07 June 2008 16:56

As learned in the previous chapter, the attributes of a class can be defined in 3 sections:

  • Public Section
  • Private Section
  • Protected Section

Before we go into the technicalities, let us understand why Public, Private and Protected. Let us take our previous example of class Human. The attributes of this class are: Hands, Legs, Kidney, Heart, DNA.

Lets assume that in a system MATRIX there are 2 instances of class Human defined as 'Neo' and 'Agent Smith'. Now Agent Smith wants to kill Neo. So Smith finds the instance of Neo and so it has the access to attribute 'Heart' of Neo. Smith destroys the attribute 'Heart' and Neo dies. Simple right. Smile

So how can the architect save Neo? The architect now has to devise a strategy so that Smith does not have access to Neo's heart. So the architect will make the attribute 'Heart' as a private attribute so that Smith can not access it directly. But the architect can not make all the other attributes like Hands, Legs also private because they have to interact with external systems in Matrix. For e.g. to Kick, run from Smith and Fly. So these will have to be public attributes.

Now consider a external system (e.g. a Waste water recycling system) that takes the Human waste as input and processes it. Now to get the human waste the system accesses the attribute Kidney of class Human. Imagine that one fine day, one of your kidney fails. So this waste water recycling system can not operate because it relies on Human Kidney. However, the Human can survive with one Kidney, but if one kidney is removed the external system dies. So probably I can not remove my damaged kidney because an external system relies on my kidney. Sounds stupid right ! Cry

So how can the architect in this case save the system even after removing the human kidney. Again, had the kidney been a private attribute the external waste water system would never have accessed it directly. But then how the the waste water system works then ? So the waste water system does not access the kidney directly but accesses a method of class human that can give the human waste  from kidney to the waste water system. So now, if the architect removes one kidney and adjusts the method of own human class then nothing is changed for the external system. It works as before since there is no dependency on the attribute of human class. 

Now that we have understood how to decide which attributes of the class should be declared as private, public or protected we can now understand the other impacts of these properties.

Public attributes and methods: These attributes are visible externally to all. A class that inherits this class will have access to all its public components and the visibility in child class will also be Public.

Private attributes and methods: These attributes are visible only inside the class. If you want to expose a private attribute, you can expose it via a public method of the class. A class that inherits this class will not have any access to the private components of this class.

Protected attributes and methods: These attributes are also visible only inside the class. If you want to expose a protected attribute, you can expose it via a public method of the class. A class that inherits this class will have access to the protected components and these will be treated as private components of the child class.

Comments
Add New Search
neelima  - abap   |address124.123.130.144 |2009-02-17 05:47:04

Neo  - awesome stuff   |address217.194.34.103 |2010-02-25 10:09:12
Good examples. you have really kept the content simple..very easy to understand.
Write comment
Name:
Email:
 
Title:
UBBCode:
[b] [i] [u] [url] [quote] [code] [img] 
 
 
:angry::0:confused::cheer:B):evil::silly::dry::lol::kiss::D:pinch:
:(:shock::X:side::):P:unsure::woohoo::huh::whistle:;):s
:!::?::idea::arrow:
 

3.26 Copyright (C) 2008 Compojoom.com / Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."

Last Updated ( Tuesday, 05 May 2009 04:41 )
 

Advertisement

 

Google Search

Advertisement