Question:
when do we use protected visibility specifier to a class member?
surodip
2009-05-14 23:52:47 UTC
when do we use protected visibility specifier to a class member
Eight answers:
Jim K
2009-05-15 00:24:41 UTC
hi there,

thought i'd better answer this as you already have one wrong answer ):



see the *** bit below.



a class member declared as private can only be accessed by member functions and friends of that class

a class member declared as protected can only be accessed by member functions and friends of that class, ***and by member functions and friends of derived classes***



hope this helps ...



jim.



p.s. i'm a software engineer, i am coming from a C++ viewpoint, this may differ in other languages, but i would doubt it ...
rk
2009-05-15 00:24:10 UTC
Protected visibility modifier to a method or datamember in a base class is used to control access to these methods either by objects belonging to the base class or objects of the class derived from the base class.

It prevents other objects from accessing these protected datamembers or methods.



----------------

explanation

----------------



Assume we have a class A from which we derive a class B and from B we derive classC.

Also assume that we have a separate class D unconnected to A, B or C

If you define a method or a datamember of class A as protected, you ensure that these datamembers or methods can be accessed by objects belonging to classA or ClassB or class C. and not by D.



If you declare a method in class A as public, then this method can be accessed by objects of A or B or C or D.



If you declare a method in class A as private, then only when you are inside class A, you will be able to access it. Objects of B,C,D cannot access it.
deonejuan
2009-05-15 00:20:44 UTC
protected is for sub-classing. You have to extends a class with the protected member and the new class can then use the member.



public class MyClass {

protected int total;

}

public class MyOtherClass extends MyClass {

total = 555;

}

=========

edit: my example embodies Java, which is single-inheritance. the concept of 'friends' would be the visitor pattern using an interface.
Adam T
2009-05-15 00:15:59 UTC
Typically, protected protects the data from ANY other class. So even inherited classes cannot access variables belonging to a parent class.



This differs from private classes which allow variables to be accessed by children classes.
anonymous
2014-09-02 08:58:05 UTC
class member declared as private can only be accessed by member functions and friends of that class,

class member declared as protected can only be accessed by member functions and friends of that class,

by member functions and friends of derived classes
verdammtwolf
2009-05-15 00:12:50 UTC
If you want to restrict access to it to only classes you allow, in Java these would be classes in the same packages or subclasses of your class I believe...I C++ I have heard those referred to as friend classes. Hope that helps. Access control is part of data hiding which is a major part of OO design.
anonymous
2016-10-06 02:46:35 UTC
stick to to college with a substandard SAT. in case you're admitted, then you definately're a member of a secure type. in case you get a scholarship grant besides, then you definately're actually in a secure type.
balu_ukpan
2009-05-15 00:36:45 UTC
Your question is not quite clear. What Programming Language are you talking about, C++, or Java or C# or any other?


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...