2007 Dec 29 11:15 AM
Hi to all,
What is the exact difference between
public
private
protected
in obect oriented language.and where we use this respectively.
Thanks and regards,
swami
Hi to all,
What is the exact difference between
public
private
protected
in obect oriented language.and where we use this respectively.
Thanks and regards,
swami
2007 Dec 29 11:30 AM
Hi,
Differences are:-
Public: - all declarations in this section(methods, data, events etc) can access and changed from outside the class and also and external user can instantiate the class.
Protected: - all declarations in this section(methods, data, events etc) can access by that class and its subclasses by inheritance.
Protected:-
all declarations in this section(methods, data, events etc) can access by that class and its friends by granting friendship to that class to access its private attributes.
and also by public methods in which intern which calls private methods.
Public:-
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. As a general rule, as few public attributes should be defined as possible.
PUBLIC SECTION.
DATA: Counter type i.
Private 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.
PRIVATE SECTION.
DATA: name(25) TYPE c,
planetype LIKE saplane-planetyp,
Instance attributes
There exist one instance attribute for each instance of the class, thus they exist seperately for each object. Instance attributes are declared with the DATA keyword.
Static attributes
Static attributes exist only once for each class. The data are the same for all instances of the class, and can be used e.g. for instance counters. Static attributes are defined with the keyword CLASS-DATA.
PRIVATE SECTION.
CLASS-DATA: counter type i,
Public methods
Can called from outside the class
PUBLIC SECTION.
METHODS: set_attributes IMPORTING p_name(25) TYPE c,
p_planetype LIKE saplane-planetyp,
Private methods
Can only be called from inside the class. They are placed in the PRIVATE section of the class.
Protected components
When we are talking subclassing and inheritance there is one more component than Public and Private, the Protected component. Protected components can be used by the superclass and all of the subclasses. Note that Subclasses cannot access Private components.
2007 Dec 29 11:34 AM
for more secure purpose , v r usin the concept of public & private fun.
public function visible to all.
private means its for particular. (Encapsulation)
as of OOPS is concern.
field should be declared as private
where as function are declared as public.
this public/private/protected are very useful ven ever using the " Inheritance concept"
public: class members are access from the outside the class
private: class members can access only within the class
protected:class members can access inside the same package
Public variables, are variables that are visible to all classes.
Private variables, are variables that are visible only to the class to which they belong.
Protected variables, are variables that are visible only to the class to which they belong, and any subclasses.
hope u got it.
2007 Dec 29 11:35 AM
Hi,
Please find the answer.
Public: When u declare a method/variable as public, then when u create a object to the class. u can access those things (outside the class).
Private: When u declare a method/variable as private, then when u create a object to the class. u can't access those things (outside the class). Only the public/protected methods can access these things.
Protected: Same as public but it can be accessed only by its derived class. For example, say class A has a method method1 as protected. Class B is derived from Class A. So when u create a object for class B, u can access method1.
Reward points if useful,
Regards,
Niyaz
| User | Count |
|---|---|
| 3 | |
| 2 | |
| 2 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |