Why private variables in a class are accessible here? - oop

I have just started to use D language and I was trying out some object-oriented code.
I am trying following code:
import std.stdio;
class Testclass{
private int intvar = 5;
private string strvar = "testing";
}
void main(){
auto tc = new Testclass();
// check if private variables are accessible:
writeln(tc.intvar);
writeln(tc.strvar);
}
Running above code has following output:
$ rdmd soq_private.d
5
testing
I find that intvar and strvar variables are accessible from main fn. Should they not be inaccessible if they are declared private in their class?

See the "D Lang" Wiki:
"Private means that only members of the enclosing class can access the member, or members and functions in the same module as the enclosing class. Private members cannot be overridden."
https://wiki.dlang.org/Access_specifiers_and_visibility
Since you are in the same module as the enclosing class, this is allowed.

From the D spec:
Symbols with private visibility can only be accessed from within the same module.
https://dlang.org/spec/attribute.html#visibility_attributes
So private applies module-wide, not class-wide. D does not have an aggregate-only visibility attribute.

Related

Creating public instance of private inner class in Kotlin

Why Kotlin doesn't allow creation of public instances of private inner classes unlike Java?
Works in Java:
public class Test {
public A a = new A();
private class A {
}
}
Doesn't work in Kotlin (A class has to be public):
class Test {
var a = A()
// ^
// 'public' property exposes its private type 'A'
private inner class A
}
I would assume because there isn't really a case where it seems like the right thing to do. Any code accessing the property a would not have access to its type. You couldn't assign it to a variable. Test.A myVar declaration outside of the Test class would error out. By not allowing it, the code will be forced to be more consistent. A better question would be why would Java allow it? Other languages, such as swift, have the same restriction.
https://kotlinlang.org/docs/reference/visibility-modifiers.html#classes-and-interfaces
states:
NOTE for Java users: outer class does not see private members of its inner classes in Kotlin.
For your usecase, you can use Nested Classes
In private inner classes you are only able to access members of your outer class.
I think the kotlin team implemented it that way, so that is possible to reduce the scope of members in private inner classes to be accessible only inside the inner class. I think this is not possible in Java.

Property accessibility in Kotlin

Here is what we know from the docs: getter of public property can't be private (seems logical enough), so:
#Inject
var repository: MyExampleRepository? = null
private get
won't compile.
Ok, so maybe we can make property private and define setter public?
#Inject
private var repository: MyExampleRepository? = null
public set
This will compile and value will actually be injected, but I still can't use this in code, so:
service.repository = null
gives compilation error:
Kotlin: Cannot access 'repository': it is 'private' in 'MyService'
I wonder if it is possible to have private property with public setter.
It's a known issue: KT-10385:
Kotlin doesn't generate setter method for the following code:
private val someProperty: Integer
public set
The intention is to generate a set only property. Use case including
spring dependency injection.

how to mock a class's inner static class's private field

i'm test a MR class which has mapper/reducer as inner static classes. the mapper has a private field which consume too much memory to make the test failed, i want to use a mock object for that field, but not sure how to do that, here is my code:
public class Aggregator extends Configured implements Tool {
public static class AggregatorMapper extends Mapper<LongWritable, Text, GeneralKey, Text) {
private LookupService lookupService = null; <--- the object i want to mock
}
}
i tried to mockito but seems no way to mock it. any suggestions? thanks!
You can use reflection to access and modify any property of any object you want. There are several questions on SO that answer this quite well already, for example:
Change private static final field using Java reflection.
Accessing private variables in Java via reflection
Instantiate private inner class with java reflection

FxCop giving a warning on private constructor CA1823 and CA1053

I have a class that looks like the following:
Public Class Utilities
Public Shared Function blah(userCode As String) As String
'doing some stuff
End Function
End Class
I'm running FxCop 10 on it and it says:
"Because type 'Utilities' contains only 'static' (
'Shared' in Visual Basic) members, add a default private
constructor to prevent the compiler from adding a default
public constructor."
Ok, you're right Mr. FxCop, I'll add a private constructor:
Private Utilities()
Now I'm having:
"It appears that field 'Utilities.Utilities' is
never used or is only ever assigned to. Use this field
or remove it."
Any ideas of what should I do to get rid of both warnings?
In C# this problem would be handled by marking the class as static, e.g.
public static class Utilities
{
...
}
A static class can only contain static (in VB shared) members.
I believe the equivalent in VB.NET is to use a module.
See Marking A Class Static in VB.NET.

How do I access private constants in class attributes in VB.Net?

I have an API that I created and currently utilize successfully in C#. I am trying to create an example of interacting with the API in VB.NET (so that the QA without C# experience can still utilize it for creating automated tests).
In C# I do the following
[TestingForm(FormName= "Land Lines", CaseType= _caseType
, Application= ApplicationNameCodes.WinRDECode, HasActions= true)]
public class LandLines : RDEMainForm
{
// .. Irrelevant Code .. //
private const string _caseType = "Land Lines";
}
As someone with zero VB.Net experience, I created the following to try and mimic it
<TestingForm(Application:=ApplicationNames.WinRDE, FormName:=FORM_NAME, CaseType:=CASE_TYPE, HasActions:=True, IncludeBaseClassActions:=False)>
Public Class Permits
Inherits TestingBase
#Region "Constants"
Private Const FORM_NAME As String = "Permits" 'Display name for the test class (in the editor)
Private Const CASE_TYPE As String = "permits" 'Unique code for this test class, used when reading/saving test plans
#End Region
End Class
This gives me a compile time error because it claims that FORM_NAME and CASE_TYPE is not defined, even though the class has it defined inside.
How can I use the defined constants inside the class in the class attributes?
I'm actually quite surprised that the C# example compiles (but I checked it indeed does).
In VB.Net that type of access (a private member outside the type even in an attribute) is simply not legal. Instead you need to make it Friend and qualify it's access
<TestingForm(Application:=ApplicationNames.WinRDE, FormName:=Permits.FORM_NAME, CaseType:=Permits.CASE_TYPE, HasActions:=True, IncludeBaseClassActions:=False)>
Public Class Permits
Inherits TestingBase
Friend Const FORM_NAME As String = "Permits" 'Display name for the test class (in the editor)
FriendConst CASE_TYPE As String = "permits" 'Unique code for this test class, used when reading/saving test plans
End Class