Why programming languages ​​do not have an "allow specific classes" access modifier

Today, programming languages ​​have these well-known access modifiers: private, public, internal, and protected. But sometimes, when I write a program in the OOP style, I think I need a “special” modifier that can manually specify which class I want to allow access to.

Now I want to know why language designers do not add such a modifier to programming languages, maybe a conflict with the concept of OOP, or about difficulties?

Thanks for the answer:)

ps Sorry if the same question has already been asked.

+5
source share
3 answers

C # and VB.Net have the concept of friend assemblies , where these assemblies can access the internal members of the specified assembly. This allows you to provide temporary access to specific callers; this is checked at compile time.

Only assemblies that you explicitly specify as friends can access Friend types and members (Visual Basic) or internal (C #). For example, if assembly B is a friend of assembly A and the assembly of C references in assembly B, C does not have access to the companion (Visual Basic) or internal (C #) types in A.

The reality is that for this function, the use of legitimate capabilities is limited (in these languages, at least), if you are not developing smelly code.

But, having said this, the idea of ​​a class that determines who can name it borders on a violation of the encapsulation and abstraction rules of OOP. By letting the class designate who it can be, you allow the class to have knowledge outside of its realm, and you throw a good design out of the window. A class can dictate how the caller should call, but not which he should call.

Hope this helps - I personally look forward to receiving answers from more academic, language-oriented people.

+2
source

This is exactly what the friend modifier does in C ++.

0
source

Source: https://habr.com/ru/post/1246521/


All Articles