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

Lack of access specifer & in C does not result in a compile error

In C, when & is absent for an argument in scanf(), no compilation error is produced; instead, the displayed results are wrong (i.e. a semantic error occurs). Consider the following code: char str[30]; int a; printf("Enter the value"); scanf("%s %d",…
Gon
  • 41
  • 3
2
votes
2 answers

Making an equals method without needing to make private fields public

I'm writing a Ruby class, and want to override the == method. I want to say something like: class ReminderTimingInfo attr_reader :times, :frequencies #don't want these to exist def initialize(times, frequencies) @times, @frequencies =…
Alex Baranosky
  • 48,865
  • 44
  • 102
  • 150
2
votes
2 answers

Which C++ compilers use the access specifier in name mangling?

I know MSVC does, and GCC doesn't? What about the others?
Marc Mutz - mmutz
  • 24,485
  • 12
  • 80
  • 90
2
votes
3 answers

Child Class cannot be protected

import java.util.*; public class NewTreeSet2{ void count(){ for (int x=0; x<7; x++,x++){ System.out.print(" " + x); } } } protected class NewTreeSet extends NewTreeSet2{ public static void main(String []…
2
votes
4 answers

Class-specific method visibility

Is there some object oriented thing that you can call some methods from certain classes, but not all of them? Is there something like that which is similiar to protected? Say you have a method void foo() and you want it to be available to the…
AMDG
  • 1,118
  • 1
  • 13
  • 29
2
votes
2 answers

The role of access specifiers on typedefs and forward declaration

Consider the following sample code for a linked list type class. I wish to declare a method which returns an Iterator, which is a typedef for a Node*. However, Node is a private nested class so in order to make the typdef, i am required to let the…
StickyCube
  • 1,721
  • 2
  • 17
  • 22
2
votes
1 answer

Accessing hidden functions from base classes with different signatures

We have: class A { public: int f(); int f(int); //... }; class B { public: int f(); int f(int); //... }; class AB : public A, public B { public: long f(double, double); //... }; A::f(), A::f(int), B::f(), B::f(int) are…
string QNA
  • 3,110
  • 3
  • 17
  • 14
2
votes
4 answers

how to use namespaces in asp.net application?

How can I use "public variables" in a same or a different namespace but in a same application? for example: 1st namespace in a BookStore web application namespace BookStore { public partial class index : System.Web.UI.Page { public…
Juyal Jee
  • 517
  • 5
  • 10
2
votes
3 answers

C#: Why are accessibility modifiers for members optional (rather than required)?

I've read this very well written blog post by Eric Lippert, but it doesn't explain the specific case of accessibility modifiers. Conventional stackoverflow wisdom says to always specify "private" explicitly, which would imply that members should…
Jon
  • 5,275
  • 5
  • 39
  • 51
2
votes
4 answers

Does the friend function have to be in the same file?

I am actually testing a file and I have a situation, where I need to access some of the protected members of the class from main.cpp. I tried to add, main() as friend, didn't work out and learned that it wont work, so I moved everything in the…
howtechstuffworks
  • 1,824
  • 4
  • 29
  • 46
1
vote
2 answers

How should I decide whether to build a "protected interface"?

From: http://www.parashift.com/c++-faq-lite/basics-of-inheritance.html#faq-19.9 Three keys: ROI, ROI and ROI. Every interface you build has a cost and a benefit. Every reusable component you build has a cost and a benefit. Every test case, every …
Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411
1
vote
3 answers

C++ Multiple inheritence, base class visibility and the dreaded diamond. Re-exposing ancestor base class as public?

I need to abstract away a lot of the interface from a base class by making it protected, but I also need public access to a simple ancestor class Object. Can I negotiate the dreaded diamond without write/edit access to these ancestors and still…
John
  • 6,433
  • 7
  • 47
  • 82
1
vote
1 answer

Ruby - inline declaration of private methods

Currently writing a class where methods that I am considering making private are spread throughout the code. Rather than adding a private line and copy-pasting everything below it, I want to do an inline declaration at the top of the class, such as…
Richard Stokes
  • 3,532
  • 7
  • 41
  • 57
1
vote
2 answers

private and protected methods in Ruby

The following code works: class MyClass def method_a method_b end private def method_b puts "Hello!" end end m = MyClass.new m.method_a Changing the call to method_b to self.method_b however does not work: def method_a …
SundayMonday
  • 19,147
  • 29
  • 100
  • 154