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

How to prevent derived class from making a private/protected virtual function public?

There are good reasons for constructing the base class interface with all virtual functions as private or protected (see this). But then how does one prevent the derived classes (which may be in the hands of external clients) from making the private…
amit kumar
  • 20,438
  • 23
  • 90
  • 126
6
votes
4 answers

How to access private function of a class in another class in c#?

I am new to c# programming and i know public data member of class is accessible from another class.Is there any way i can access private function from another class? This is what i have tried.please help me out. using System; using…
SrividhyaShama
  • 499
  • 2
  • 7
  • 18
6
votes
3 answers

Why is Java's "protected" less protected than default?

In Java, we have four access specifiers: public, protected, package-private (default), and private. This is well known and not an issue for me. My question is with regard to the naming of protected. As shown in the table here, giving a field the…
wchargin
  • 15,589
  • 12
  • 71
  • 110
6
votes
3 answers

When should you restrict accessibility to a virtual function in a derived class?

Consider the following code: class Base { public: virtual void Foo() {} }; class Derived : public Base { private: void Foo() {} }; void func() { Base* a = new Derived; a->Foo(); //fine, calls Derived::Foo() Derived* b = new…
Jon
  • 5,275
  • 5
  • 39
  • 51
5
votes
3 answers

Are Ruby imported methods always private?

This is best explained with an example: file1.rb: def foo puts 123 end file2.rb: class A require 'file1' end A.new.foo will give an error "': private method 'foo' called". I can get around this by doing A.new.send("foo") but is there a way to…
alexloh
  • 1,606
  • 2
  • 15
  • 29
5
votes
3 answers

How is the "public/protected/private" method implemented, and how can I emulate it?

In ruby, you can do this: class Thing public def f1 puts "f1" end private def f2 puts "f2" end public def f3 puts "f3" end private def f4 puts "f4" end end where now f1 and f3 and public, f2 and f4 is…
A Question Asker
  • 3,339
  • 7
  • 31
  • 39
5
votes
2 answers

Scope of private, protected, and public

Within a Ruby class definition, what is the scopes of the private keyword in the following scenarios: class Foo def bar_public puts "public" end private def bar_private puts "private" end def bar_public_2 puts "another…
pseudosudo
  • 1,962
  • 1
  • 19
  • 30
5
votes
2 answers

Ruby Matrix set_element private?

When calling set_element on an instance of the Matrix class I get the following error NoMethodError: private method ‘set_element’ called for Matrix[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]:Matrix But set_element is listed under public instance…
Ram
  • 423
  • 4
  • 7
5
votes
3 answers

Swift:Class cannot be declared public because its Super class is internal

I am trying to declare the class as public as shown below class RewardsAndRedemptionModel:BaseObject { var rewardHistory :[RewardHistoryModel]! } This is where i am trying to make the class public but i could not. public class RewardHistoryModel…
Sam
  • 449
  • 2
  • 9
  • 18
5
votes
1 answer

Public vs. Protected abstract class method

Is there any security/access difference when making a package access level abstract class's non-static methods public vs making them protected? Only classes from within the same package that extend the abstract class can access the non-static…
user5164938
5
votes
5 answers

class member access specifiers and binary code

I understand what the typical access specifiers are, and what they mean. 'public' members are accessible anywhere, 'private' members are accessible only by the same class and friends, etc. What I'm wondering is what, if anything, this equates to in…
pdehaan
  • 332
  • 2
  • 13
5
votes
1 answer

Why can I access private/protected methods using Object#send in Ruby?

The class class A private def foo puts :foo end public def bar puts :bar end private def zim puts :zim end protected def dib puts :dib end end instance of A a = A.new test a.foo rescue puts :fail a.bar…
maček
  • 76,434
  • 37
  • 167
  • 198
5
votes
3 answers

Make instance methods private in runtime

I need to make some instance methods private after registering that object in another object. I don't want to freeze the object because it must remain editable, only with less functionality. And I don't want to undef the methods since they are used…
Filipe Miguel Fonseca
  • 6,400
  • 1
  • 31
  • 26
5
votes
3 answers

x in y is defined in an inaccessible class or interface, ID in structure

I am getting main.java: error: someId in someStruct is defined in an inaccessible class or interface but how can I solve this? my main class: import subdir.Subclass; public class main{ ... Subclass.someArrayList.get(i).someString ... …
user753676
5
votes
2 answers

Confusion regarding access specifiers in Java and C#

I was comparing access modifiers in Java and C#. I wanted to find an alternative of C#'s protected internal in Java. But I noticed that protected modifier is different in both the languages (C# and Java). protected in Java is equivalent to protected…
Shashwat
  • 2,538
  • 7
  • 37
  • 56