Questions tagged [private]

Private is a way of encapsulation in object-oriented programming.

Private is a way of encapsulation in object-oriented programming.

2890 questions
3652
votes
31 answers

What is the difference between public, protected, package-private and private in Java?

In Java, are there clear rules on when to use each of access modifiers, namely the default (package private), public, protected and private, while making class and interface and dealing with inheritance?
intrepion
  • 38,099
  • 4
  • 24
  • 21
1131
votes
16 answers

What is the difference between public, private, and protected?

When and why should I use public, private, and protected functions and variables inside a class? What is the difference between them? Examples: // Public public $variable; public function doSomething() { // ... } // Private private…
Adam Halasz
  • 57,421
  • 66
  • 149
  • 213
741
votes
15 answers

Does Python have “private” variables in classes?

I'm coming from the Java world and reading Bruce Eckels' Python 3 Patterns, Recipes and Idioms. While reading about classes, it goes on to say that in Python there is no need to declare instance variables. You just use them in the constructor, and…
Omnipresent
  • 29,434
  • 47
  • 142
  • 186
564
votes
14 answers

Change private static final field using Java reflection

I have a class with a private static final field that, unfortunately, I need to change it at run-time. Using reflection I get this error: java.lang.IllegalAccessException: Can not set static final boolean field Is there any way to change the…
fixitagain
  • 6,543
  • 3
  • 20
  • 24
557
votes
14 answers

How to read the value of a private field from a different class in Java?

I have a poorly designed class in a 3rd-party JAR and I need to access one of its private fields. For example, why should I need to choose private field is it necessary? class IWasDesignedPoorly { private Hashtable…
Frank Krueger
  • 69,552
  • 46
  • 163
  • 208
507
votes
32 answers

How do you unit test private methods?

I'm building a class library that will have some public & private methods. I want to be able to unit test the private methods (mostly while developing, but also it could be useful for future refactoring). What is the correct way to do this?
Eric Labashosky
  • 29,484
  • 14
  • 39
  • 32
481
votes
7 answers

Internal vs. Private Access Modifiers

What is the difference between the internal and private access modifiers in C#?
Jim Fell
  • 13,750
  • 36
  • 127
  • 202
348
votes
19 answers

What is the difference between private and protected members of C++ classes?

What is the difference between private and protected members in C++ classes? I understand from best practice conventions that variables and functions which are not called outside the class should be made private—but looking at my MFC project, MFC…
Konrad
  • 39,751
  • 32
  • 78
  • 114
334
votes
22 answers

private final static attribute vs private final attribute

In Java, what's the difference between: private final static int NUMBER = 10; and private final int NUMBER = 10; Both are private and final, the difference is the static attribute. What's better? And why?
okami
324
votes
11 answers

Defining private module functions in python

According to http://www.faqs.org/docs/diveintopython/fileinfo_private.html: Like most languages, Python has the concept of private elements: Private functions, which can't be called from outside their module However, if I define two…
olamundo
  • 23,991
  • 34
  • 108
  • 149
287
votes
21 answers

Do subclasses inherit private fields?

This is an interview question. Does subclasses inherit private fields? I answered "No", because we can't access them using the "normal OOP way". But the interviewer thinks that they are inherited, because we can access such fields indirectly or…
Stan Kurilin
  • 15,614
  • 21
  • 81
  • 132
286
votes
4 answers

If I fork someone else's private Github repo into my account, is it going to appear in my account as a public repo?

Someone gave me access to one of their private repo on Github. What I want to do is to fork that project into my own account, so I could make use of Github's pull request feature. I only have a basic account on Github, so I cannot make private repos…
Terence Ponce
  • 9,123
  • 9
  • 31
  • 37
283
votes
3 answers

What does "@private" mean in Objective-C?

What does @private mean in Objective-C?
user100051
250
votes
16 answers

Can a constructor in Java be private?

Can a constructor be private? How is a private constructor useful?
Rajesh
  • 8,477
  • 5
  • 20
  • 7
225
votes
9 answers

What is the use of having destructor as private?

What is the use of having destructor as private?
yesraaj
  • 46,370
  • 69
  • 194
  • 251
1
2 3
99 100