Questions tagged [private]

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

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

2684 questions
3352
votes
29 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
  • 34,999
  • 4
  • 21
  • 21
1050
votes
17 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
  • 51,803
  • 63
  • 138
  • 208
644
votes
12 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
  • 27,174
  • 47
  • 133
  • 181
514
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
  • 64,851
  • 44
  • 155
  • 203
506
votes
12 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
  • 5,953
  • 3
  • 17
  • 24
493
votes
31 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
  • 27,105
  • 12
  • 37
  • 32
416
votes
7 answers

Internal vs. Private Access Modifiers

What is the difference between the internal and private access modifiers in C#?
Jim Fell
  • 12,390
  • 29
  • 117
  • 192
314
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
301
votes
17 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
  • 35,953
  • 29
  • 74
  • 111
281
votes
3 answers

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

What does @private mean in Objective-C?
user100051
279
votes
24 answers

Cloning a private Github repo

I have a private repository on Github for a project I'm working on. Until now I had only worked on my home desktop, but I just bought a laptop, and am trying to set it up so that I can work on the project from either computer, and push / pull…
Solomon
  • 4,645
  • 2
  • 23
  • 34
267
votes
3 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
  • 8,092
  • 8
  • 26
  • 35
267
votes
9 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
  • 21,047
  • 32
  • 99
  • 142
262
votes
19 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
  • 14,860
  • 20
  • 75
  • 127
234
votes
15 answers

Can a constructor in Java be private?

Can a constructor be private? How is a private constructor useful?
Rajesh
  • 7,657
  • 5
  • 18
  • 7
1
2 3
99 100