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
1
vote
4 answers

C++ access specifiers, too few?

As far as i know, there're only 3 access-specifiers in C++: private, public, protected With these 3 access-specifiers, how can i make a method usable to the classes in the project but unusable to the "foreigners" ??(like internal and public in C#)
rhapsodyn
  • 3,272
  • 5
  • 29
  • 36
1
vote
2 answers

Access private member variable of the class using its object (instance)

Here is a VB.NET code snippet Public Class OOPDemo Private _strtString as String Public Function Func(obj as OOPDemo) as boolean obj._strString = "I can set value to private member using a object" End Function End Class I…
MOZILLA
  • 5,862
  • 14
  • 53
  • 59
1
vote
6 answers

How does a function access private data member of a class object?

My code:- #include using namespace std; class a{ private: int x; public: a(int data) { x=data; } friend void printPrivateMember(a); }; void printPrivateMember(a obj) { cout<
Echo
  • 570
  • 1
  • 7
  • 21
1
vote
3 answers

How to make inaccessible class variable

I'm wondering if it is possible to make a class variable inaccessible inside this class? The only way to change the value of this variable will be through class setter. For example: class foo { private: int m_var; bool m_isBig; void…
Isaac
  • 115
  • 10
1
vote
2 answers

Create Object of class in same package

I created a class HT17 package useFul; class HT17 { void show() { System.out.println("Hello World!"); } } And i tried accessing it from another class from same package package useFul; class HT18 { public static void…
hthakkar8
  • 23
  • 1
  • 4
1
vote
3 answers

static and private method behavior when calling direct on object of child class sounds like overriding?

public class B extends A{ public static void main(String[] args) { new B().privateMethod();//no error -output B-privateMethod.Sounds like overriding new B().staticMethod(); //no error -output B-StaticMethod.Sounds like overriding …
user4768611
1
vote
2 answers

Can an Object have an access specifier?

I'm new to Programming! Ignore if it a silly question. But leave a comment. Is it possible to declare an access specifier to object instances in C#? OR is there any default specifiers for that ? class Person { public int age; } class…
Balaji
  • 1,375
  • 1
  • 16
  • 30
1
vote
1 answer

Why C++ allows to give more restrictive access to a derived class method?

This link talks about allowing more restrictive access to a derived class method. Q. What is the reason for allowing this in C++? Languages such as Java & C# don't allow it. Is it useful in some cases? If so please help me understand.
Destructor
  • 14,123
  • 11
  • 61
  • 126
1
vote
2 answers

why subclass over riding can only have weaker access than super class

When it comes to over - riding , there is only one rule related to access specifiers "The subclass overridden method cannot have weaker access than super class method" why is the child class restricted to have stronger access specifier ? what draw…
1
vote
1 answer

How do I access a private variable from a method?

currentColor = getCarColor(this.car.color) Here color is private and getCarColor is a method, how do I access the variable color?
tarutao
  • 93
  • 1
  • 3
1
vote
3 answers

Default access specifier of Main()

What is the default access specifier of Main method in C#? If the default access specifier of static void Main() is private, then how does an external entity eg. OS invoke this method? Any foreign process should not be able to call this method.
Jiten
  • 13
  • 3
1
vote
3 answers

c++ derived class with private access specifier

I have a derived class (class B) from a base class (class A). Class A has a protected virtual function foo() which I want to override and use it as private in derived class. Class A{ protected: virtual void foo() = 0; } I am wondering…
Avb Avb
  • 545
  • 2
  • 13
  • 25
1
vote
1 answer

Making def_delegator respect private methods

Is it possible to make the Forwardable#def_delegator method respect that the target method is private? In the following code, when def_delegator is used, Foo#hello is called even though it's private, because the implementation of…
Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
1
vote
1 answer

Send allows access to private variables

Consider the following code: def create_class(class_name, superclass, &block) klass = Class.new superclass, &block Object.const_set class_name, klass end After I do: create_class('User', ActiveRecord::Base) the following is…
Bruno Antunes
  • 2,241
  • 4
  • 21
  • 35
1
vote
3 answers

Why the member function can still be accessed even if it is declared as "private"?

Possible Duplicate: Public virtual function derived private in C++ class B { private: int b; public: B(int i); virtual void show() { cout<<"B::show()called. "<
JDein
  • 295
  • 2
  • 9