Break OO privacy: Difference between revisions

Omit Zig
m (→‎{{header|C sharp}}: Regularize header markup to recommended on category page)
(Omit Zig)
 
(9 intermediate revisions by 6 users not shown)
Line 1:
{{task}} [[Category:Object oriented]]
{{omit from|AWKtask}}
{{omit from|BASIC}}
{{omit from|BBC BASIC}}
{{omit from|C}}
{{omit from|Déjà Vu}}
{{omit from|GUISS}}
{{omit from|Haskell}}
{{omit from|Locomotive Basic}}
{{omit from|Lotus 123 Macro Scripting}}
{{omit from|Mathematica}}
{{omit from|R}}
{{omit from|Rust}}
{{omit from|SmileBASIC}}
{{omit from|ZX Spectrum Basic}}
{{omit from|6502 Assembly}}
{{omit from|68000 Assembly}}
{{omit from|Z80 Assembly}}
{{omit from|8086 Assembly}}
{{omit from|x86 Assembly}}
{{omit from|ARM Assembly}}
 
Show how to access private or protected members of a class in an object-oriented language from outside an instance of the class, without calling non-private or non-protected members of the class as a proxy.
The intent is to show how a debugger, serializer, or other meta-programming tool might access information that is barred by normal access methods to the object but can nevertheless be accessed from within the language by some provided escape hatch or reflection mechanism.
Line 28 ⟶ 8:
as unidiomatic at best, and poor programming practice at worst.
Nonetheless, if your language intentionally maintains a double-standard for OO privacy, here's where you can show it off.
 
=={{header|ABAP}}==
 
Similar to C++, ABAP allows the declaration of friends which can be both classes and interfaces. All subclasses of friend classes are automatically friends of the source class. For example if classA (source) has classB as a friend and classC is a subclass of classB then classC is a friend of classA. Similarly all implementing classes of friend interfaces are friends of the source class. Also all interfaces which contain the befriended interface as a component are friends of the source class.
 
<langsyntaxhighlight ABAPlang="abap">class friendly_class definition deferred.
 
class my_class definition friends friendly_class .
Line 74 ⟶ 53:
 
endclass.
</syntaxhighlight>
</lang>
 
=={{header|Ada}}==
 
One of the great criticisms of Pascal was "there is no escape". The reason was that sometimes you have to convert the incompatible. We start with a package, which defines the data type which holds the secret.
 
<langsyntaxhighlight Adalang="ada">package OO_Privacy is
 
type Confidential_Stuff is tagged private;
Line 89 ⟶ 67:
Password: Password_Type := "default!"; -- the "secret"
end record;
end OO_Privacy;</langsyntaxhighlight>
 
When we later define an object C of type Confidential_Stuff, we can't read read the password stored in C -- except by breaking / bypassing OO privacy rules.
Line 97 ⟶ 75:
One way to read the password is by using the generic function Unchecked_Conversion:
 
<langsyntaxhighlight Adalang="ada">with OO_Privacy, Ada.Unchecked_Conversion, Ada.Text_IO;
 
procedure OO_Break_Privacy is
Line 112 ⟶ 90:
begin
Ada.Text_IO.Put_Line("The secret password is """ & Hack(C).Password & """");
end OO_Break_Privacy;</langsyntaxhighlight>
 
The output shows that C holds, surprise, surprise, the default password:
Line 122 ⟶ 100:
Another way to bypass privacy is using a child package. Ada child packages have access to their parents' private data structures (somewhat similar to "friends" in C++"):
 
<langsyntaxhighlight Adalang="ada">package OO_Privacy.Friend is -- child package of OO.Privacy
function Get_Password(Secret: Confidential_Stuff) return String;
end OO_Privacy.Friend;</langsyntaxhighlight>
 
<langsyntaxhighlight Adalang="ada">package body OO_Privacy.Friend is -- implementation of the child package
function Get_Password(Secret: Confidential_Stuff) return String is
(Secret.Password);
end OO_Privacy.Friend;</langsyntaxhighlight>
 
Now here is the program that uses the child package, to read the secret:
 
<langsyntaxhighlight Adalang="ada">with OO_Privacy.Friend, Ada.Text_IO;
procedure Bypass_OO_Privacy is
Line 147 ⟶ 125:
OO_Privacy.Friend.Get_Password(C) &
"""");
end Bypass_OO_Privacy;</langsyntaxhighlight>
 
Once again, we have been too lazy to overwrite the default password:
Line 154 ⟶ 132:
 
In fact, the password has to be the default one, because we cannot change the password. For that purpose, we would have to write a setter procedure, and either include it in the package OO_Privacy, or in a child package of OO_Privacy. (Or we could use Unchecked_Conversion to overwrite the default password -- but that is bad style.)
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">using System;
using System.Reflection;
 
Line 173 ⟶ 150:
Console.WriteLine(answer);
}
}</langsyntaxhighlight>
{{out}}
<syntaxhighlight lang="text">42</langsyntaxhighlight>
 
=={{header|C++}}==
 
C++ has the 'friend' keyword to indicate that one class should have access to the private data of another. Here's a simple use case. (Please note that this code is not thread-safe.)
 
<langsyntaxhighlight lang="cpp">#include <iostream>
 
class CWidget; // Forward-declare that we have a class named CWidget.
Line 247 ⟶ 223:
delete pWidget3;
delete pWidget2;
}</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight lang="text">Widget spawning. There are now 1 Widgets instanciated.
Widget spawning. There are now 2 Widgets instanciated.
Widget dieing. There are now 1 Widgets instanciated.
Widget spawning. There are now 2 Widgets instanciated.
Widget dieing. There are now 1 Widgets instanciated.
Widget dieing. There are now 0 Widgets instanciated.</langsyntaxhighlight>
 
Without the "friend" mechanism, it's still possible to meaningfully modify any member in another class, as long as you know that member's address in memory, and its type. Here's the same program as above, but using a pointer to m_uicount, rather a reference to the factory:
 
<langsyntaxhighlight lang="cpp">#include <iostream>
 
class CWidget; // Forward-declare that we have a class named CWidget.
Line 323 ⟶ 299:
delete pWidget3;
delete pWidget2;
}</langsyntaxhighlight>
 
=={{header|Clojure}}==
You can use the var-quote macro to get values from private variables. Here's an example of a variable 'priv' marked private in namespace 'a':
<langsyntaxhighlight lang="clojure">
(ns a)
(def ^:private priv :secret)
Line 335 ⟶ 310:
user=> @#'a/priv ; succeeds
:secret
</syntaxhighlight>
</lang>
 
Clojure can also access Java private variables with the same strategy that Java uses. As a convenience, use the [http://clojuredocs.org/clojure_contrib/clojure.contrib.reflect/get-field get-field] function from clojure.contrib.reflect. Here's an example of grabbing the private field "serialVersionUID" from java.lang.Double:
<langsyntaxhighlight lang="clojure">
user=> (get-field Double "serialVersionUID" (Double/valueOf 1.0))
-9172774392245257468
</syntaxhighlight>
</lang>
 
=={{header|Common Lisp}}==
 
Line 358 ⟶ 332:
A symbol can be present in more than one package, such that it can be internal in some of them, and external in others.
 
<langsyntaxhighlight lang="lisp">(defpackage :funky
;; only these symbols are public
(:export :widget :get-wobbliness)
Line 402 ⟶ 376:
;; even read and evaluated. The symbol is internal and so cannot be used.
(format t "wobbliness: ~a~%" (slot-value *w* 'funky:wobbliness))
</syntaxhighlight>
</lang>
 
{{Out}} using CLISP:
Line 410 ⟶ 384:
*** - READ from #<INPUT BUFFERED FILE-STREAM CHARACTER #P"funky.lisp" @44>:
#<PACKAGE FUNKY> has no external symbol with name "WOBBLINESS"</pre>
 
=={{header|D}}==
 
Line 416 ⟶ 389:
 
breakingprivacy.d:
<langsyntaxhighlight Dlang="d">module breakingprivacy;
 
struct Foo
Line 426 ⟶ 399:
string str;
float f;
}</langsyntaxhighlight>
 
app.d:
<langsyntaxhighlight Dlang="d">import std.stdio;
import breakingprivacy;
 
Line 444 ⟶ 417:
__traits(getMember, foo, "str") = "Not so private anymore!";
writeln("Modified foo: ", foo);
}</langsyntaxhighlight>
 
{{out}}
<langsyntaxhighlight lang="text">Foo([1, 2, 3], 42, "Hello World!", 3.14)
foo.x = 42
Modified foo: Foo([1, 2, 3], 42, "Not so private anymore!", 3.14)</langsyntaxhighlight>
 
=={{header|E}}==
 
Line 456 ⟶ 428:
 
{{improve|E|Show an example of such an evaluator once it is available.}}
 
=={{header|Ecstasy}}==
In Ecstasy, using the keywords <span style="background-color: #e5e4e2"><tt>&nbsp;public&nbsp;</tt></span>, <span style="background-color: #e5e4e2"><tt>&nbsp;protected&nbsp;</tt></span>, and <span style="background-color: #e5e4e2"><tt>&nbsp;private&nbsp;</tt></span> to mark classes and class members is solely for the benefit of the developer, and not in any way related to security. These keywords help the developer to classify information among three categories: Things that are useful to everyone; things that are useful to further compositions (such as sub-classes and mixins); and things that, were they exposed, would likely to create an ugly mess. <b>Information hiding is about organization, and not about security.</b>
 
An Ecstasy reference contains both its type (the type of the <i>reference</i> itself, as opposed to the type of the referred-to object, a.k.a. the <i>referent</i>), and the means -- a "pointer" or a "value" -- to refer to the referent. By default, the type of a reference is of the <i>public type</i> of the referent, but it is possible to reveal the referent as any other legal type -- where <i>legal</i> simply means that strong type safety is enforced. This is <b>not</b> a type cast; it is a request to the runtime to provide a different reference to the same underlying object.
 
Ecstasy security is accomplished by the use of <i>software containers</i>. Code running in a container is always allowed reveal any legal type on any object created within that container, including any object created within sub-containers. However, the runtime will reject any reveal request on any object that was <i>not</i> created within that container.
<syntaxhighlight lang="java">
module BreakOO {
/**
* This is a class with public, protected, and private properties.
*/
class Exposed {
public String pub = "public";
protected String pro = "protected";
private String pri = "private";
 
@Override
String toString() {
return $"pub={pub.quoted()}, pro={pro.quoted()}, pri={pri.quoted()}";
}
}
 
void run() {
@Inject Console console;
 
Exposed expo = new Exposed();
console.print($"before: {expo}");
 
// you can only access public members from the default reference
expo.pub = $"this was {expo.pub}";
// expo.pro = $"this was {expo.pro}"; <- compiler error
// expo.pri = $"this was {expo.pri}"; <- compiler error
 
// but you can ask for the protected reference ...
assert (protected Exposed) expoPro := &expo.revealAs((protected Exposed));
expoPro.pro = $"this was {expoPro.pro}";
// expoPro.pri = $"this was {expoPro.pri}"; <- compiler error
 
// and you can ask for the private reference ...
assert (private Exposed) expoPri := &expo.revealAs((private Exposed));
expoPri.pri = $"this was {expoPri.pri}";
 
// or you can ask for the underlying struct, which is a passive
// object that contains only the field storage
assert (struct Exposed) expoStr := &expo.revealAs((struct Exposed));
expoStr.pub = $"{expoStr.pub}!!!";
expoStr.pro = $"{expoStr.pro}!!!";
expoStr.pri = $"{expoStr.pri}!!!";
 
console.print($"after: {expo}");
}
}
</syntaxhighlight>
 
{{out}}
<pre>
before: pub="public", pro="protected", pri="private"
after: pub="this was public!!!", pro="this was protected!!!", pri="this was private!!!"
</pre>
 
=={{header|F_Sharp|F#}}==
{{trans|C#}}
<langsyntaxhighlight lang="fsharp">open System
open System.Reflection
 
Line 474 ⟶ 506:
let answer = fieldInfo.GetValue(myInstance)
printfn "%s = %A" (answer.GetType().ToString()) answer
0</langsyntaxhighlight>
{{out}}
<pre>System.Int32 = 42</pre>
 
=={{header|Factor}}==
From the [http://docs.factorcode.org/content/article-word-search-private.html documentation for private words]: ''"Privacy is not enforced by the system; private words can be called from other vocabularies, and from the listener. However, this should be avoided where possible."''
Line 483 ⟶ 514:
This example uses the private word ''sequence/tester'' from the vocabulary ''sets.private''. It tries to count the elements in an intersection of two sets.
 
<langsyntaxhighlight lang="factor">( scratchpad ) USING: sets sets.private ;
( scratchpad ) { 1 2 3 } { 1 2 4 } sequence/tester count .
2</langsyntaxhighlight>
 
There is better way to do the same, without any private words.
 
<langsyntaxhighlight lang="factor">( scratchpad ) USE: sets
( scratchpad ) { 1 2 3 } { 1 2 4 } intersect length .
2</langsyntaxhighlight>
 
=={{header|Forth}}==
{{works with|Forth}}
Line 499 ⟶ 529:
Needs the FMS-SI (single inheritance) library code located here:
http://soton.mpeforth.com/flag/fms/index.html
<langsyntaxhighlight lang="forth">include FMS-SI.f
 
99 value x \ create a global variable named x
Line 516 ⟶ 546:
50 .. f1.x ! \ use the dot parser to access the private x without a message
f1 print \ 50
</syntaxhighlight>
</lang>
 
=={{header|FreeBASIC}}==
FreeBASIC generally does a good job of maintaining OO privacy as it doesn't support reflection and even its OffsetOf keyword cannot obtain the offset within a user defined type of a private or protected field. You therefore have to guess the offset of a non-public field in order to be able to access it using a raw pointer though this is not generally a difficult task.
 
However, as usual, macros come to the rescue and one can easily access non-public members by the simple expedient (or, if you prefer, 'dirty hack') of redefining the Private and Protected keywords to mean Public:
<langsyntaxhighlight lang="freebasic">'FB 1.05.0 Win64
 
#Undef Private
Line 542 ⟶ 571:
Print
Print "Press any key to quit"
Sleep</langsyntaxhighlight>
 
{{out}}
Line 548 ⟶ 577:
1 2 3
</pre>
 
=={{header|Go}}==
Go has a <code>reflect</code> and <code>unsafe</code> package that together can do this.
Line 555 ⟶ 583:
A relevant Go Blog article is
[http://blog.golang.org/laws-of-reflection The Laws of Reflection].
<langsyntaxhighlight lang="go">package main
 
import (
Line 642 ⟶ 670:
_, err := r.ReadByte()
fmt.Println("bufio.ReadByte returned error:", err)
}</langsyntaxhighlight>
{{out}}
<pre>
Line 658 ⟶ 686:
 
==Icon and {{header|Unicon}}==
{{omit from|Icon}}
Unicon implements object environments with records and supporting procedures for creation, initialization, and methods. The variables in the class environment can be accessed like any other record field. Additionally, with the ''fieldnames'' procedure you can obtain the names of the class variables.
 
Line 664 ⟶ 691:
 
Note: Unicon can be translated via a command line switch into icon which allows for classes to be shared with Icon code (assuming no other incompatibilities exist).
<langsyntaxhighlight lang="unicon">link printf
 
procedure main()
Line 680 ⟶ 707:
printf("foo var1=%i, var2=%i, var3=%i\n",var1,var2,var3)
end
end</langsyntaxhighlight>
 
{{libheader|Icon Programming Library}}
Line 689 ⟶ 716:
var 1 of foo x = 1
foo var1=-1, var2=2, var3=3</pre>
 
=={{header|J}}==
 
Line 697 ⟶ 723:
 
J does support a "[http://www.jsoftware.com/help/dictionary/dx003.htm Lock Script]" mechanism - to transform a J script so that it's unreadable. However, anyone with access to a machine running the code and ordinary developer tools or who understands the "locking" technique could unlock it.
 
=={{header|Java}}==
<p>
Private fields (and in general all members) of a Java class can be accessed via reflection, but must pass a security check in order to do so. There are two such security checks, one for discovering the field at all, and another for granting access to it in order to be able to read and write it. (This in turn means that trusted applications can do this — it is in fact a mechanism used by important frameworks like Spring — but untrusted applets cannot.)
In order the access a class member of another class which is marked as <code>private</code>, you'll need to use <kbd>reflection</kbd>.<br />
<lang java>import java.lang.reflect.*;
Java offers a collection of <kbd>reflection</kbd>-related utilities within the <code>java.lang.reflect</code> package.
</p>
<p>
In this example I'll use a class with two fields, <code>stringA</code> and <code>stringB</code>.
</p>
<syntaxhighlight lang="java">
class Example {
private String _namestringA = "rosetta";
private String stringB = "code";
public Example(String name) { _name = name; }
public String toString() { return "Hello, I am " + _name; }
}
</syntaxhighlight>
<p>
public class BreakPrivacy {
From another class, I'll instantiate <code>Example</code>, and use a <code>Field</code> object to return a specified, declared field.<br />
public static final void main(String[] args) throws Exception {
To do this you call the <code>getDeclaredField</code> on your <code>Class</code> object, supplying the <kbd>name</kbd> of the field.
Example foo = new Example("Eric");
</p>
<syntaxhighlight lang="java">
for (Field f : Example.class.getDeclaredFields()) {
Example example = new Example();
if (f.getName().equals("_name")) {
Field field = example.getClass().getDeclaredField("stringB");
// make it accessible
</syntaxhighlight>
f.setAccessible(true);
<p>
To allow access to <code>stringB</code> we'll need to use the <code>Field.setAccessible</code> method, and signify <kbd>true</kbd>.<br />
// get private field
This is essentially what the task is looking for, the ability to override the <kbd>access-modifier</kbd>.
System.out.println(f.get(foo));
</p>
<syntaxhighlight lang="java">
// set private field
field.setAccessible(true);
f.set(foo, "Edith");
</syntaxhighlight>
System.out.println(foo);
<p>
Now, we can access the data from the field by using the <code>Field.get</code> method, specifying the instance as the parameter.
break;
</p>
}
<syntaxhighlight lang="java">
}
String stringB = (String) field.get(example);
}
</syntaxhighlight>
}</lang>
<p>
{{out}}
So, all together our method would contain the following lines.
</p>
<syntaxhighlight lang="java">
Example example = new Example();
Field field = example.getClass().getDeclaredField("stringB");
field.setAccessible(true);
String stringA = example.stringA;
String stringB = (String) field.get(example);
System.out.println(stringA + " " + stringB);
</syntaxhighlight>
<p>
With an output of the following.
</p>
<pre>
rosetta code
Eric
Hello, I am Edith
</pre>
<p>
All classes are vulnerable to this, including <code>String</code> (and therefore, string literals). How long is the word "cat"?
If we hadn't used the <code>Field.setAccessible</code> method, we would get an <code>IllegalAccessException</code>.
</p>
<pre>
cannot access a member of class Example with modifiers "private"
</pre>
<p>
Additionally, you can do this with methods via the <code>Method</code> object.<br />
Consider the following class.
</p>
<syntaxhighlight lang="java">
class Example {
String stringA = "rosetta";
 
private String stringB() {
(Note: somewhere between Java 8 and Java 11 this stopped working because the <code>value</code> field of <code>String</code> is <code>final</code>. The reflective access is still possible, but changing a final field isn't.)
return "code";
<lang Java>import java.lang.reflect.*;
}
public class BreakString{
public static void main(String... args) throws Exception{
Field f = String.class.getDeclaredField("value");
f.setAccessible(true);
f.set("cat",new char[]{'t','i','g','e','r'});
System.out.println("cat");
System.out.println("cat".length());
}
}
</syntaxhighlight>
</lang>
<p>
{{out}}
The approach is the same, except we are using <code>getDeclaredMethod</code>, and instead of <code>get</code> we are using <code>invoke</code>.
</p>
<syntaxhighlight lang="java">
Example example = new Example();
Method method = example.getClass().getDeclaredMethod("stringB");
method.setAccessible(true);
String stringA = example.stringA;
String stringB = (String) method.invoke(example);
System.out.println(stringA + " " + stringB);
</syntaxhighlight>
<pre>
rosetta code
tiger
5
</pre>
 
Line 757 ⟶ 812:
 
Julia's object model is one of structs which contain data and methods which are just functions using those structs, with multiple dispatch, rather than object methods, used to distinguish similarly named calls for different object types. Julia does not therefore enforce any private fields in its structures, since, except for constructors, it does not distinguish object methods from other functions. If private fields or methods are actually needed they can be kept from view by placing them inside a module which cannot be directly accessed by user code, such as in a module within a module.
 
=={{header|Kotlin}}==
For tasks such as this, reflection is your friend:
<langsyntaxhighlight lang="scala">import kotlin.reflect.full.declaredMemberProperties
import kotlin.reflect.jvm.isAccessible
 
Line 775 ⟶ 829:
println("${prop.name} -> ${prop.get(tbb)}")
}
}</langsyntaxhighlight>
 
{{out}}
Line 781 ⟶ 835:
secret -> 42
</pre>
 
=={{header|Logtalk}}==
Logtalk provides a ''context switching call'' control construct that allows a call to be executed as from within an object. It's mainly used for debugging and for writing unit tests. This control construct can be disabled on a global or per object basis to prevent it of being used to break encapsulation.
 
In the following example, a prototype is used for simplicity.
<langsyntaxhighlight lang="logtalk">:- object(foo).
 
% be sure that context switching calls are allowed
Line 797 ⟶ 850:
bar(3).
 
:- end_object.</langsyntaxhighlight>
After compiling and loading the above object, we can use the following query to access the private method:
<langsyntaxhighlight lang="logtalk">| ?- foo<<bar(X).
X = 1 ;
X = 2 ;
X = 3
true</langsyntaxhighlight>
=={{header|Lua}}==
 
Lua doesn't have a native concept of classes, and the only custom data structure available is the table (in which all fields are always public).
However, it's pretty common for table/object creation functions in object-oriented code to return instances of other functions that uses local variables through lexical scoping, or their closure, which is not accessible to the outside code and could be considered a space with "private" fields.
These can be accessed indirectly through the [https://www.lua.org/manual/5.1/manual.html#5.9 debug library].
 
<syntaxhighlight lang="lua">local function Counter()
-- These two variables are "private" to this function and can normally
-- only be accessed from within this scope, including by any function
-- created inside here.
local counter = {}
local count = 0
 
function counter:increment()
-- 'count' is an upvalue here and can thus be accessed through the
-- debug library, as long as we have a reference to this function.
count = count + 1
end
 
return counter
end
 
-- Create a counter object and try to access the local 'count' variable.
local counter = Counter()
 
for i = 1, math.huge do
local name, value = debug.getupvalue(counter.increment, i)
if not name then break end -- No more upvalues.
 
if name == "count" then
print("Found 'count', which is "..tostring(value))
-- If the 'counter.increment' function didn't access 'count'
-- directly then we would never get here.
break
end
end</syntaxhighlight>
 
{{out}}
<pre>
Found 'count', which is 0
</pre>
 
Note that there's an infinite number of other more complex ways for functions to store values in a "private" manner, and the introspection functionality of the debug library can only get you so far.
=={{header|M2000 Interpreter}}==
We want to read two private variables, and change values without using a public method (a module or a function), and without attach a temporary method (we can do that in M2000). There is a variant in READ statemend to set references from group members (for variables and arrays, and objects) to names with a reference for each. So using these names (here in the exaample K, M) we can read and write private variables.
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module CheckIt {
Group Alfa {
Line 828 ⟶ 923:
}
CheckIt
</syntaxhighlight>
</lang>
 
=={{header|Nim}}==
File oo.nim:
<langsyntaxhighlight lang="nim">type Foo* = object
a: string
b: string
Line 838 ⟶ 932:
 
proc createFoo*(a, b, c): Foo =
Foo(a: a, b: b, c: c)</langsyntaxhighlight>
By not adding a <code>*</code> to <code>Foo</code>'s members we don't export them. When we import this module we can't use them directly:
<langsyntaxhighlight lang="nim">var x = createFoo("this a", "this b", 12)
 
echo x.a # compile time error</langsyntaxhighlight>
The easiest way to get a debug view of any data:
<syntaxhighlight lang ="nim">echo repr(x)</langsyntaxhighlight>
Output:
<pre>[a = 0x7f6bb87a7050"this a",
Line 850 ⟶ 944:
c = 12]</pre>
More fine-grained:
<langsyntaxhighlight lang="nim">import typeinfo
 
for key, val in fields(toAny(x)):
Line 860 ⟶ 954:
echo " is an integer with value: ", val.getBiggestInt
else:
echo " is an unknown with value: ", val.repr</langsyntaxhighlight>
Output:
<pre>Key a
Line 868 ⟶ 962:
Key c
is an integer with value: 12</pre>
 
=={{header|Objective-C}}==
In older versions of the compiler, you can simply access a private field from outside of the class. The compiler will give a warning, but you can ignore it and it will still compile. However, in current compiler versions it is now a hard compile error.
Line 875 ⟶ 968:
One solution is to use Key-Value Coding. It treats properties and instance variables as "keys" that you can get and set using key-value coding methods.
 
<langsyntaxhighlight lang="objc">#import <Foundation/Foundation.h>
 
@interface Example : NSObject {
Line 910 ⟶ 1,003:
}
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 920 ⟶ 1,013:
Another solution is to use a category to add methods to the class (you can have categories in your code modify any class, even classes compiled by someone else, including system classes). Since the new method is in the class, it can use the class's private instance variables with no problem.
 
<langsyntaxhighlight lang="objc">#import <Foundation/Foundation.h>
 
@interface Example : NSObject {
Line 969 ⟶ 1,062:
}
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 979 ⟶ 1,072:
Finally, you can access the instance variable directly using runtime functions.
 
<langsyntaxhighlight lang="objc">#import <Foundation/Foundation.h>
#import <objc/runtime.h>
 
Line 1,016 ⟶ 1,109:
}
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,022 ⟶ 1,115:
Hello, I am Edith
</pre>
 
=={{header|OCaml}}==
 
Line 1,033 ⟶ 1,125:
The reader is advised to stop reading here.
 
<langsyntaxhighlight lang="ocaml">class point x y =
object
val mutable x = x
Line 1,065 ⟶ 1,157:
Printf.printf "Broken coord: (%d, %d)\n" x y;
evil_reset p
p#print</langsyntaxhighlight>
 
{{out}}
Line 1,073 ⟶ 1,165:
Broken coord: (-1, -1)
(0, 0)</pre>
 
=={{header|Oforth}}==
 
Line 1,079 ⟶ 1,170:
 
There is no other way to access attributes values from outside but to call methods on the object.
 
=={{header|Perl}}==
Perl's object model does not enforce privacy. An object is just a blessed reference, and a blessed reference can be dereferenced just like an ordinary reference.
<langsyntaxhighlight lang="perl">package Foo;
sub new {
my $class = shift;
Line 1,096 ⟶ 1,186:
package main;
my $foo = Foo->new();
print "$_\n" for $foo->get_bar(), $foo->{_bar};</langsyntaxhighlight>
{{out}}
<pre>I am ostensibly private
I am ostensibly private</pre>
 
=={{header|Phix}}==
{{libheader|Phix/Class}}
Line 1,106 ⟶ 1,195:
We can easily break that privacy mechanism via low-level routines with the required simulated/fake context,<br>
and at the same time be reasonably confident that no-one is ever going to manage to achieve that by accident.
<!--<langsyntaxhighlight Phixlang="phix">(notonline)-->
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- (no class under p2js)</span>
<span style="color: #008080;">class</span> <span style="color: #000000;">test</span>
Line 1,123 ⟶ 1,212:
<span style="color: #000000;">structs</span><span style="color: #0000FF;">:</span><span style="color: #000000;">store_field</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"msg"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"this breaks privacy"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ctx</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">t</span><span style="color: #0000FF;">.</span><span style="color: #000000;">show</span><span style="color: #0000FF;">()</span>
<!--</langsyntaxhighlight>-->
<small>(Obviously you could inline the ctx values rather than create a separate constant.)</small>
{{out}}
Line 1,131 ⟶ 1,220:
"this breaks privacy"
</pre>
 
=={{header|PHP}}==
While normally accessing private variables causes fatal errors, it's possible to catch output of some debugging functions and use it. Known functions which can get private variables include: <code>var_dump()</code>, <code>print_r()</code>, <code>var_export()</code> and <code>serialize()</code>. The easiest to use is <code>var_export()</code> because it's both valid PHP code and doesn't recognize private and public variables.
 
{{works with|PHP|5.1}}
<langsyntaxhighlight lang="php"><?php
class SimpleClass {
private $answer = "hello\"world\nforever :)";
Line 1,154 ⟶ 1,242:
 
$new_class = eval($class_content);
echo $new_class['answer'];</langsyntaxhighlight>
 
Another way commonly used to access private and protected variables in PHP is to cast the object to an array. It's probably unintentional though looking on how casted array contains null bytes (probably "private" mark). This works unless a magic method for the cast operation is implemented:
Line 1,160 ⟶ 1,248:
{{works with|PHP|4.x}}
{{works with|PHP|5.x}}
<langsyntaxhighlight lang="php"><?php
class SimpleClass {
private $answer = 42;
Line 1,167 ⟶ 1,255:
$class = new SimpleClass;
$classvars = (array)$class;
echo $classvars["\0SimpleClass\0answer"];</langsyntaxhighlight>
 
{{works with|PHP|5.3}}
Since php 5.3, one can easily read and write any protected and private member in a object via reflection.
<langsyntaxhighlight lang="php"><?php
class fragile {
private $foo = 'bar';
Line 1,182 ⟶ 1,270:
$rp->setValue($fragile, 'haxxorz!');
var_dump($rp->getValue($fragile));
var_dump($fragile);</langsyntaxhighlight>
{{out}}
<pre>
Line 1,192 ⟶ 1,280:
}
</pre>
 
=={{header|PicoLisp}}==
PicoLisp uses [http://software-lab.de/doc/ref.html#transient "transient symbols"] for variables, functions, methods etc. inaccessible from other parts of the program. Lexically, a transient symbol is enclosed by double quotes.
The only way to access a transient symbol outside its namespace is to search for its name in other (public) structures. This is done by the '[http://software-lab.de/doc/refL.html#loc loc]' function.
<langsyntaxhighlight PicoLisplang="picolisp">(class +Example)
# "_name"
 
Line 1,207 ⟶ 1,294:
(====) # Close transient scope
 
(setq Foo (new '(+Example) "Eric"))</langsyntaxhighlight>
Test:
<langsyntaxhighlight PicoLisplang="picolisp">: (string> Foo) # Access via method call
-> "Hello, I am Eric"
 
Line 1,228 ⟶ 1,315:
 
: (get Foo (loc "_name" +Example))
-> "Edith"</langsyntaxhighlight>
 
=={{header|Python}}==
Python isn't heavily into private class names. Although private class names can be defined by using a double underscore at the start of the name, such names are accessible as they are mangled into the original name preceded by the name of its class as shown in this example:
<langsyntaxhighlight lang="python">>>> class MyClassName:
__private = 123
non_private = __private * 2
Line 1,247 ⟶ 1,333:
>>> mine._MyClassName__private
123
>>> </langsyntaxhighlight>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2015.12}}
We may call into the MOP (Meta-Object Protocol) via the <tt>.^</tt> operator, and the MOP knows all about the object, including any supposedly private bits. We ask for its attributes, find the correct one, and get its value.
<syntaxhighlight lang="raku" perl6line>class Foo {
has $!shyguy = 42;
}
my Foo $foo .= new;
 
say $foo.^attributes.first('$!shyguy').get_value($foo);</langsyntaxhighlight>
{{out}}
<pre>42</pre>
 
=={{header|Ruby}}==
Ruby lets you redefine great parts of the object model at runtime and provides several methods to do so conveniently. For a list of all available methods look up the documentation of <code>Object</code> and <code>Module</code> or call informative methods at runtime (<code>puts Object.methods</code>).
<langsyntaxhighlight lang="ruby">
class Example
def initialize
Line 1,282 ⟶ 1,366:
p example.instance_variable_set :@private_data, 42 # => 42
p example.instance_variable_get :@private_data # => 42
</syntaxhighlight>
</lang>
 
=={{header|Scala}}==
{{libheader|Scala}}<langsyntaxhighlight lang="scala">class Example(private var name: String) {
override def toString = s"Hello, I am $name"
}
Line 1,297 ⟶ 1,380:
field.set(foo, "Edith")
println(foo)
}</langsyntaxhighlight>
 
=={{header|Sidef}}==
Sidef's object model does not enforce privacy, but it allows storing private attributes inside the container of an object, which is an hash:
<langsyntaxhighlight lang="ruby">class Example {
has public = "foo"
method init {
Line 1,315 ⟶ 1,397:
 
# Access private attributes
say obj{:private}; #=> "secret"</langsyntaxhighlight>
 
=={{header|Swift}}==
Swift reflection provides a Collection of label-value pairs for struct properties
<langsyntaxhighlight Swiftlang="swift">struct Example {
var notSoSecret = "Hello!"
private var secret = 42
Line 1,330 ⟶ 1,411:
print("Value of the secret is \(secret)")
}
</syntaxhighlight>
</lang>
{{out}}
<pre>Value of the secret is 42</pre>
 
=={{header|Tcl}}==
Tcl's object properties are just variables in the a per-instance namespace; all that's required to get hold of them is to discover the name of the namespace concerned:
<langsyntaxhighlight lang="tcl">package require Tcl 8.6
 
oo::class create Example {
Line 1,346 ⟶ 1,426:
$e print
set [info object namespace $e]::name "Edith"
$e print</langsyntaxhighlight>{{out}}
Hello, I am Eric
Hello, I am Edith
 
=={{header|Visual Basic .NET}}==
{{trans|C#}}
Line 1,356 ⟶ 1,435:
Like the other .NET languages, VB can use Reflection ([https://docs.microsoft.com/en-us/dotnet/framework/reflection-and-codedom/reflection Microsoft docs]).
 
<langsyntaxhighlight lang="vbnet">Imports System.Reflection
 
' MyClass is a VB keyword.
Line 1,370 ⟶ 1,449:
Console.WriteLine(answer)
End Sub
End Class</langsyntaxhighlight>
 
{{out}}
<pre>42</pre>
 
=={{header|Wren}}==
In Wren all instance and static fields of a class are private and are prefixed by an underscore and a double underscore respectively. There is no way to break the privacy of such fields that I'm aware of (Wren doesn't have reflection). If one wants to provide public access to fields then this is done via getter and/or setter methods.
 
However, there is no such thing as a private method. Although conventionally methods which are not intended to be called from outside the class are suffixed with an underscore, this doesn't prevent anyone from accessing them as the following example shows.
<langsyntaxhighlight ecmascriptlang="wren">class Safe {
construct new() { _safe = 42 } // the field _safe is private
safe { _safe } // provides public access to field
Line 1,388 ⟶ 1,466:
var s = Safe.new()
var a = [s.safe, s.doubleSafe, s.notSoSafe_]
for (e in a) System.print(e)</langsyntaxhighlight>
 
{{out}}
Line 1,399 ⟶ 1,477:
=={{header|zkl}}==
In zkl, privacy is more convention than enforced (unlike const or protected).
<langsyntaxhighlight lang="zkl">class C{var [private] v; fcn [private] f{123} class [private] D {}}
C.v; C.f; C.D; // all generate NotFoundError exceptions
However:
Line 1,405 ⟶ 1,483:
C.fcns[1]() //-->123
C.classes //-->L(Class(D))
C.vars //-->L(L("",Void)) (name,value) pairs</langsyntaxhighlight>
In the case of private vars, the name isn't saved.
{{omit from|6502 Assembly}}
 
{{omit from|8086 Assembly}}
{{omit from|68000 Assembly}}
{{omit from|ARM Assembly}}
{{omit from|AWK}}
{{omit from|BASIC}}
{{omit from|BBC BASIC}}
{{omit from|C}}
{{omit from|Déjà Vu}}
{{omit from|GUISS}}
{{omit from|Haskell}}
{{omit from|Icon}}
{{omit from|Locomotive Basic}}
{{omit from|Lotus 123 Macro Scripting}}
{{omit from|Mathematica}}
{{omit from|Rust}}
{{omit from|R}}
{{omit from|SmileBASIC}}
{{omit from|Standard ML|not OO}}
{{omit from|x86 Assembly}}
{{omit from|Z80 Assembly}}
{{omit from|Zig}}
{{omit from|ZX Spectrum Basic}}
{{omit from|Insitux}}
28

edits