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
0
votes
1 answer

Difficulty in retaining the default visibility of a method which needs to be accessed by the different package

I am developing a MineSweeper. In that I have 3 packages. 1.frontEnd 2.backEnd 3.mineSweeperControl. mineSweeperControl contains an class ActionSplicer which implements ActionListener.In frontEnd I have an array of JBUTTONS and an array of…
Muthu Ganapathy Nathan
  • 3,199
  • 16
  • 47
  • 77
0
votes
2 answers

How do you designate private or protected attributes in Ruby/Rails?

How do you designate private or protected attributes in Ruby/Rails? Are all DB fields automatically attributes, and don't need to be defined in the Model? Any recommended tutorials? Working in Rails 3.0.7.
B Seven
  • 44,484
  • 66
  • 240
  • 385
0
votes
0 answers

Import certain overloads of base class to private scope of derived

Using the using-directive I'm able to select a certain set of methods from the base class to put into a different access scope. Is this also possible for individual overloads of the method? Something like this: Demo #include class base…
glades
  • 3,778
  • 1
  • 12
  • 34
0
votes
0 answers

Verifying accessibility of data members in a multi-level inheritance example in C++

I have the following code involving multi-level inheritance in C++. I want to verify the accessibility of data members for the object d defined in the main in the inheritance chain. I have summarized the accessibility as the following and was…
Sami
  • 513
  • 4
  • 11
0
votes
1 answer

Why is this protected member not accessible from the derived class?

I thought that protected members in a base class are accessible from a derived class, but not so: template class SharedPointer { protected: explicit SharedPointer(T* const ptr) : ptr(ptr) {} public: constexpr SharedPointer() :…
Zebrafish
  • 11,682
  • 3
  • 43
  • 119
0
votes
2 answers

Why subclasses inherit private fields?

I'm creating a new class which is vehicle. I'm expecting the only protected variables inherit to subclasses. But when I try to create Constructor with IDE, it is also using superclasses private variables? -Which is private String vehicleName- I'm…
user13561105
0
votes
2 answers

Is it possible to access a private static variable and method?

We can access a static property of a class by writing className.propertyName, but if the property (method/variable) is private then is it possible to access that property? For example, class A{ static int a = 50; } public class HelloWorld{ …
DEV_BOT
  • 33
  • 9
0
votes
2 answers

Can we changed the access specifier of a method from private to default just for the sake of method level unit testing

I could find many questions/answers on why a method should not make it public. but I could not find anything specific to "default" in Java.
0
votes
0 answers

Initialization of inner private class within public constructor call of outer class -What's the standard's intention for this?

Recently, I came across code like this: class NeedsFactoryForPublicCreation { private: struct Accessor { // Enable in order to make the compile failing (expected behavior) // explicit Accessor() = default; }; public: …
Secundi
  • 1,150
  • 1
  • 4
  • 12
0
votes
2 answers

two default Constructor, one in public and one in private

My question is about constructors in OOP(C++). When I define default constructor in a class as private and when I initialize an object of that class in main as default then error occurs that default constructor is inaccessible. It's fine. But then I…
0
votes
1 answer

What purpose do access specifiers for nested class serve?

Suppose I have a class A with a private nested class B. As I understand it, that means B is not part of the public API. Does that mean that the public and private access specifiers only server the programmer rather than the user? Are there ways that…
mana
  • 545
  • 4
  • 12
0
votes
2 answers

Value of variable not updating in Java NetBeans

I have two java files in same package. I want to take the updated value of one variable from one file to another. I wrote the following code. In class1.java :- import javax.swing.JOptionPane; public class class1 { public static String bar =…
Uday
  • 5
  • 1
0
votes
1 answer

Why does a child object call private method of the super class having main method?

In the below code, the child class object calls its getBankName() method but instead, the private method getBankName() of parent class is invoked. public class Bank { private void getBankName() { System.out.println("Bank"); } …
hitesh
  • 39
  • 5
0
votes
1 answer

In Python 3, can we have access specifiers of class attributes without using the underscore in naming?

In a MATLAB code, I have a class that is defined with various properties blocks. Following is an example: properties (GetAccess = public, SetAccess = private) c = 0.3; MACHINE_PRECISION = 1e-16; end I want to convert the class definition…
Pratik Dash
  • 81
  • 1
  • 10
0
votes
0 answers

How do you allow a conversion to the base of a privately inherited base class?

I have a class C that privately inherits B that implements an interface A, as shown below: class A { public: virtual void f() = 0; virtual void g() = 0; }; class B: public A { public: void f() override {}; void g() override…
octopod
  • 824
  • 2
  • 10
  • 23