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
Editor

Margaret jest nagradzaną technical writerką, nauczycielką i wykładowczynią. Jest znana z tego, że potrafi w prostych słowach pzybliżyć złożone pojęcia techniczne słuchaczom ze świata biznesu. Od dwudziestu lat jej definicje pojęć z dziedziny IT są publikowane przez Que w encyklopedii terminów technologicznych, a także cytowane w artykułach ukazujących się w New York Times, w magazynie Time, USA Today, ZDNet, a także w magazynach PC i Discovery. Margaret dołączyła do zespołu Techopedii w roku 2011. Margaret lubi pomagać znaleźć wspólny język specjalistom ze świata biznesu i IT. W swojej pracy, jak sama mówi, buduje mosty między tymi dwiema domenami, w ten…