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

When to use @objc in Swift?

In Swift, I see some methods like: @objc private func doubleTapGestureRecognized(recognizer: UITapGestureRecognizer) I was wondering, when to use @objc? I read some documents, but they are saying when you want it to be callable in Objective-C, you…
Wingzero
  • 9,644
  • 10
  • 39
  • 80
104
votes
2 answers

Why are `private val` and `private final val` different?

I used to think that private val and private final val are same, until I saw section 4.1 in Scala Reference: A constant value definition is of the form final val x = e where e is a constant expression (§6.24). The final modifier must be present…
Yang Bo
  • 3,586
  • 3
  • 22
  • 35
92
votes
7 answers

Are private methods really safe?

In Java the private access modifier consider as safe since it is not visible outside of the class. Then outside world doesn't know about that method either. But I thought Java reflection can use to break this rule. Consider following case: public…
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
90
votes
7 answers

Java: accessing private constructor with type parameters

This is a followup to this question about java private constructors. Suppose I have the following class: class Foo { private T arg; private Foo(T t) { // private! this.arg = t; } @Override public String…
dsg
  • 12,924
  • 21
  • 67
  • 111
86
votes
6 answers

Get all private fields using reflection

I wonder is there a way to get all private fields of some class in Java and their type. For example lets suppose I have a class: class SomeClass { private String aaa; private SomeOtherClass bbb; private double ccc; } Now I would like to…
user2152361
  • 863
  • 1
  • 6
  • 4
84
votes
5 answers

Protected and private methods in Rails

Method visibility in Ruby (public, protected, and private methods) has been well explained in places like this blog post. But in Ruby on Rails it seems slightly different than it would be in a regular Ruby application because of the way the…
jrdioko
  • 32,230
  • 28
  • 81
  • 120
80
votes
1 answer

What is the best way of testing private methods with GoogleTest?

I would like to test some private methods using GoogleTest. class Foo { private: int bar(...) } GoogleTest allows a couple of ways of doing this. OPTION 1 With FRIEND_TEST: class Foo { private: FRIEND_TEST(Foo, barReturnsZero); int…
Carlos Perez-Lopez
  • 951
  • 1
  • 7
  • 7
77
votes
6 answers

JUnit Testing private variables?

I have been assigned the task of unit testing a class that I never worked directly on with JUnit, and am strictly forbidden to change the code in the package. This is usually no issue, since most of our unit testing is just for functionality and…
donnyton
  • 5,874
  • 9
  • 42
  • 60
74
votes
6 answers

When would I want to make my private class static?

In general, are there any benefits in declaring a private class as static? In what cases would I want to use one of the following over the other? private static class Foo { ... } vs private class Foo { ... }
His
  • 5,891
  • 15
  • 61
  • 82
74
votes
4 answers

Are private constants possible in PHP?

PHP does not allow class Foo { private const my_private_const; but of course allows const my_const; So in effect constants are global because I can access my_const anywhere using Foo::my_const Is there a way to make private constants?
user656925
73
votes
4 answers

Advantage of set and get methods vs public variable

Possible Duplicate: Why use getters and setters? Is there any advantage to making methods to access private variables in your class instead of making the variable public? For example is the second case better than the first? //Case 1 public class…
72
votes
2 answers

Why doesn't PHP permit private const?

I have a class that benefits from the use of constants in its internal implementation, but I would like to limit visibility of these constants. Why doesn't PHP permit private constants? Is there another way to achieve this or is PHP trying to…
leo
  • 2,022
  • 2
  • 17
  • 18
70
votes
8 answers

Why do we actually need Private or Protected inheritance in C++?

In C++, I can't think of a case in which I would like to inherit private/protected from a base class: class Base; class Derived1 : private Base; class Derived2 : protected Base; Is it really useful?
Gal Goldman
  • 8,641
  • 11
  • 45
  • 45
69
votes
4 answers

Private Variables and Methods in Python

When should I use _foo (an underscore) or __bar (double underscore) for private members and methods in Python?
67
votes
14 answers

Are private members inherited in C#?

Just seen one tutorial saying that: Class Dog { private string Name; } Class SuperDog:Dog { private string Mood; } Then there was an UML displaying that SuperDog will inherit Name as well. I have tried but to me it seems that only public members…
Petr
  • 7,787
  • 14
  • 44
  • 53