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
4
votes
4 answers

Private overriden virtual functions in derived class

Is there any point to making virtual member functions, overridden from a base class private, if those are public in the base class? struct base { virtual void a(); }; struct derived : base { // ... private: void a() override; };
4
votes
2 answers

What is the difference between no access specifier and public in java 9 module?

From java 9, public access is limited to it's own module. Does it mean public acts like a package protected ( no access specifier )? Can someone clarify this?
Sagar Pudi
  • 4,634
  • 3
  • 32
  • 51
4
votes
2 answers

Protected data members and data functions

when i declare a protected data member in a class that means its not accesible to the outer world but the derived class. My question is will it be accesible to a class that is derived from the derived class?
Saad Masood
  • 11,036
  • 9
  • 32
  • 40
4
votes
1 answer

C - Writing extern keyword explicitly for global variable

I have 2 C files below. From what i read i know that global variables' default storage class is extern. If i type it explicitly i am getting undefined variable error. What am i missing here? Does that mean when i omit extern keyword it becomes a…
Ali Atıl
  • 121
  • 7
4
votes
1 answer

Why is PHP private variables working on extended class?

Shouldn't it generate error when i try to set the value of a property from the extended class instead of a base class?
Bivek
  • 1,380
  • 10
  • 24
4
votes
5 answers

Understanding Access specifiers on Classes in C#

First of all let me start by saying that I do understand access specifiers I just don't see the point of using them in classes. It makes sense on methods to limit their scope but on classes, why would you want a private class, isn't it the purpose…
fs_tigre
  • 10,650
  • 13
  • 73
  • 146
4
votes
3 answers

Should you make use of private, protected, and public modifiers in Ruby?

I come from more of a C# background yet am learning Ruby in my spare time. Given classes, it is possible to make their methods private, public (default) or protected. While I understand their usage, is it typical for Ruby code to make use of such…
Finglas
  • 15,518
  • 10
  • 56
  • 89
4
votes
6 answers

"Personal" method in ruby

I'm looking for a way of making a method "personal" - note NOT PRIVATE to a class here is an example - by "personal" I mean the behaviour of method "foo" class A def foo "foo" end end class B < A def foo "bar" end end class C <…
4
votes
3 answers

Make java methods visible to only specific classes

I have a manager class that is responsible for managing Objects of a certain kind. To do so it needs to manipulate these Objects, but these Objects have no relation to the manager whatsoever, so design technically, they are in separate packages…
salbeira
  • 2,375
  • 5
  • 26
  • 40
4
votes
2 answers

Why can't interface members be non-public?

Possible Duplicate: Non Public Members for C# Interfaces Suppose I have internal interface IInterface { int MyProperty { get; set; } } public class MyClass : IInterface { internal int MyProperty { get { …
nawfal
  • 70,104
  • 56
  • 326
  • 368
3
votes
3 answers

Disallow functionality automatically provided by C++ compilers

Scott Meyers in his book "Effective C++" says, To disallow functionality automatically provided by compilers, declare the corresponding member functions private and give no implementations.Then if somebody inadvertently calls one, they will …
3
votes
1 answer

What are the access specifiers of inherited (-> "using") base class ctors / operators in derived class?

In the following code you can see that I'm inheriting the base class ctors into the derived class under the "private" access specifier. My initial thought would be that these would adapt to the access specifiers that I have given (here "private")…
3
votes
2 answers

Does a private type-specifier prevent objects from 'understanding' the specifier?

I was having a problem getting a function to accept an enum as a return-type. In the code below there's an enum: Status{ DEAD, WOUNDED, HEALTHY } and a function with Status as a return type: Status getStatus(); Header Code: class…
Azoreo
  • 236
  • 1
  • 3
  • 14
3
votes
2 answers

What is the order of control flow while compiling the code for a class in C++?

I am compiling a class, the complete program to which is given below: #include using namespace std; class Test{ public: Test() { cout<<"Test variable created...\n"; // accessing width variable…
Vaibhav
  • 346
  • 2
  • 12
3
votes
3 answers

How come protected method is accessible in unrelated class?

I have below code written with Eclipse ide: public interface X { final public static int SOME_CONST = 0; } public class Handle implements X { protected void methodHandle () { } //... } public class User implements X { Handle handle = new…
iammilind
  • 68,093
  • 33
  • 169
  • 336