Private

Why Trust Techopedia

What Does Private Mean?

Private is a keyword that specifies access level and provides programmers with some control over which variables and methods are hidden in a class. Variables and methods defined with the private keyword may be accessed only by other methods within the class and cannot be accessed by derived classes.

Advertisements

The private keyword is used in most object-oriented programming (OOP) languages, including C++, C# and Java.

Techopedia Explains Private

The private access specifier is mostly used for encapsulation, which refers to variables or methods hidden from the outside world. This means that private variables and methods are tightly bound to a class and only available within the class scope. Private data members are usually accessed through special methods known as setters. The compiler returns an error message if a programmer attempts to access a private variable or method from outside its class.

C++ enables the use of friend functions and classes to access a private variable or method. If a class is declared a friend to another class (through adding a line of code in the latter class stating that the former class is its friend via the friend keyword), then the former class may access private variables and methods from the latter class. Additionally, if class A is a friend to class B, then B is not implicitly a friend to A. Furthermore, friendship is not transitive. For example, if A is a friend to B, and B is a friend to C, then A is not implicitly a friend to C.

Advertisements

Related Terms

Margaret Rouse
Technology Expert
Margaret Rouse
Technology Expert

Margaret is an award-winning technical writer and teacher known for her ability to explain complex technical subjects to a non-technical business audience. Over the past twenty years, her IT definitions have been published by Que in an encyclopedia of technology terms and cited in articles by the New York Times, Time Magazine, USA Today, ZDNet, PC Magazine, and Discovery Magazine. She joined Techopedia in 2011. Margaret's idea of a fun day is helping IT and business professionals learn to speak each other’s highly specialized languages.