Questions tagged [access-specifier]

The access specifier in an object-oriented language determines how a class restricts access to its members.

The access specifier in an object-oriented language determines how a class restricts access to its members.

The usual access specifiers are private, protected and public.

  • A class member whose access specifier is private can only be used by instances of that class.
  • A class member whose access specifier is protected can only be used by some other classes.
    Usually this means that access to these members is granted only to subclasses, but this is not a hard-and-fast rule. In Java, for example, classes that are in the same package also have access to each other's protected members.
  • A class member whose access specifier is public can be used by all other classes in the program.

You can use this tag for questions about how access to the members of your class is resolved.

242 questions
10
votes
3 answers

Private/Protected block in Ruby?

Ruby doesn't seem to have a facility for defining a protected/private block like so: protected do def method end end This would be nice compared to protected def method end public where you might forget to "public" after the protected…
gsmendoza
  • 1,394
  • 1
  • 13
  • 31
10
votes
3 answers

When should we consider using private or protected?

Just wondering, when should we actually must use private or protected for some methods in the model? Sometimes I can't not be bothered to group my methods in private nor protected. I just leave it as it is. But I know it must be a bad practice,…
Victor
  • 13,010
  • 18
  • 83
  • 146
10
votes
1 answer

Is size of the object affected by type of access-specifier and type of inheritance?

While answering one of the question, there was a discussion thread below my answer. Which suggests that depending on the access specifier (or may be the type of inheritance) private/protected/public the sizeof the class object may vary! I still…
iammilind
  • 68,093
  • 33
  • 169
  • 336
10
votes
1 answer

Indenting issue after access specifiers in Visual Studio Express

I am using Visual Studio Express 2013 After I use an access specifier, I want Visual Studio to automatically indent my members another 4 spaces further than my access specifier; but instead, it keeps the members in line with the access specifier. Is…
Driggers
  • 111
  • 4
9
votes
5 answers

Why protected method is not accessible from subclass?

Consider the following code snippets: package vehicle; public abstract class AbstractVehicle { protected int speedFactor() { return 5; } } package car; import vehicle.AbstractVehicle; public class SedanCar extends AbstractVehicle…
Hariharan
  • 881
  • 1
  • 13
  • 25
8
votes
2 answers

What actually occurs when stating "private"/"protected" in Ruby?

What is actually occurring when private/protected is stated within a Ruby class definition? They are not keywords, so that implies they must be method calls, but I cannot find where they are defined. They do not appear to be documented. Are the two…
Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
8
votes
2 answers

calling member functions from within another member function of the same class in C++, objective C

Consider the following: class A{ //data members void foo() { bar();//is this possible? or should you say this->bar() note that bar is not static } void bar() { } }//end of class A How do you call member…
Namratha
  • 16,630
  • 27
  • 90
  • 125
7
votes
3 answers

Need to understand the statement "Accessability is checked statically and not dynamically in C++"

I am confused regarding checking of access specifiers statically or dynamically. It is said that access specifiers are not checked dynamically. What does that mean ? This example has been taken from different posts on SO. Consider this…
Rajeshwar
  • 11,179
  • 26
  • 86
  • 158
7
votes
1 answer

SFINAE and visibility-checking in Clang vs. GCC vs. MSVC -- which is correct?

I've written my attempt at a C++03-compatible implementation of is_default_constructible: template struct is_default_constructible; template<> struct is_default_constructible<> { protected: // Put base typedefs here to avoid…
user541686
  • 205,094
  • 128
  • 528
  • 886
7
votes
2 answers

Meaning of "Cannot reduce the visibility of the inherited method" with an interface

I have two files: public interface PrintService { void print(PrintDetails details); class PrintDetails { private String printTemplate; } public interface Task { String ACTION = "print"; } } and public class A…
Alan2
  • 23,493
  • 79
  • 256
  • 450
6
votes
2 answers

Error: expected a declaration

So far all I have in my DecisionTree.h file is namespace DecisionTree { public static double Entropy(int pos, int neg); } and Visual Studio is already highlighting the public and saying Error: expected a declaration. What am I missing?
Daniel
  • 6,595
  • 9
  • 38
  • 70
6
votes
5 answers

C++ advice from Code Complete on encapsulation?

In the section on "Good Encapsulation" in Code Complete, it is recommended to hide private implementation details. An example is given in C++. The idea is basically to completely separate the interface from the implementation, even in the class…
voithos
  • 68,482
  • 12
  • 101
  • 116
6
votes
9 answers

What is the difference between access specifier protected and internal protected in C#

What is the difference between access specifier protected and internal protected in C# ?
user422560
  • 69
  • 1
  • 2
6
votes
1 answer

How constructor works in private inheritance

I know there are same question about this topic. But I'm still confused. Please explain how A's class constructor is executing with obj even I inherit A's class constructor privately. #include using namespace std; class A{ public: …
user5028722
6
votes
5 answers

C++ Class Access Specifier Verbosity

A "traditional" C++ class (just some random declarations) might resemble the following: class Foo { public: Foo(); explicit Foo(const std::string&); ~Foo(); enum FooState { Idle, Busy, Unknown }; FooState GetState() const; bool…
PolyTex
  • 63
  • 2