access-modifiers

Why can I call a private method of another instance of the same type outside of that instance? [duplicate]

痴心易碎 提交于 2019-12-04 03:27:15
问题 This question already has answers here : Why and how does C# allow accessing private variables outside the class itself when it's within the same containing class? (3 answers) Closed last year . If I have ObjectA, and it has a private method GetPrice() and also has a "parent" field of the same type, why am I able to call GetPrice() on the parent instance from within the child instance? Example: private decimal GetPrice() { ObjectA parent = Parent; if(parent != null) { return parent.GetPrice()

Signature Files and Access Modifers in F#

风流意气都作罢 提交于 2019-12-04 03:26:38
I've recently been trying to learn the Object-Oriented aspects of F#, and have become curious about how to restrict access to types/modules in the language. More specifically, I want to know the difference between writing this: Example.fsi module Stack = val foo : string Example.fs module Stack = let foo = "foo" let bar = "bar" and alternatively this: module Stack = let foo = "foo" let private bar = "bar" Do they not accomplish exactly the same thing in the end? Coming from a C# background, I'm much inclined just to use the access modifiers over signature (FSI) files. They seem to be more

Access modifier best practice in C# vs Java

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-04 03:11:01
问题 I understand that the rule of thumb in OOD is to minimize access to all members of a given object as best as can be reasonably accomplished. C# and Java both seem to implement the same set of access modifiers; however, something which has bewildered me for some time now is why Java classes seem to be mostly declared as public while C# classes seem mostly to be declared as default. Is there some subtlety to these languages which imposes these differences, or is it simply a matter of convention

Accessibility scope of Java access modifiers [duplicate]

青春壹個敷衍的年華 提交于 2019-12-03 23:16:02
This question already has answers here : Closed 6 years ago . What is the difference between public, protected, package-private and private in Java? (27 answers) Java has private, protected, and public access modifiers. Can you explain the accessibility scope of these modifiers. How can I access a protected member within a different package? Freak For better understanding you need to see this Access Modifiers Same Class Same Package Subclass Other packages public Y Y Y Y protected Y Y Y N no access modifier Y Y N N private Y N N N Here the important difference is between Default and protected

Protected Classes in .NET

无人久伴 提交于 2019-12-03 19:07:01
问题 Can a class be protected in.NET? Why is / isn't this possible? 回答1: Yes, you just cannot make them top level classes, they must be inner classes public class Outer { protected class Foo { } } This is fine, it means that the only classes allowed to see Foo are sub classes of Outer class X { // 'Outer.Foo' is inaccessible due to its protection level private void Flibble(Outer.Foo foo) { } } class X : Outer { // fine private void Flibble(Outer.Foo foo) { } } Note that you cannot declare any

Why does a Java constructor have to be public or protected for a class to be extended outside its package?

ⅰ亾dé卋堺 提交于 2019-12-03 14:47:46
The following is my ProtectedConstructor.java source code: package protectCon; public class ProtectedConstructor{ public int nothing; ProtectedConstructor(){ nothing = 0; } } And following is the UsingProtectedCon.java source: package other; import protectcon.ProtectedConstructor; public class UsingProtectedCon extends ProtectedConstructor{ //**Line 4** public static void main(String... a) { } } When I compile UsingProtectedCon.java , I get error at Line 4 shown above. It says that ProtectedConstructor() is not public ; so cannot be accessed outside package. However, since my class is public,

why python does not have access modifier?And what are there alternatives in python?

旧时模样 提交于 2019-12-03 12:04:37
why python does not have Access modifier like in c#, java i.e public, private etc.what are the alternative way of encapsulation and information hiding in python. From Wikipedia : [Python] has limited support for private variables using name mangling . See the "Classes" section of the tutorial for details . Many Python users don't feel the need for private variables, though. The slogan "We're all consenting adults here" is used to describe this attitude. Some consider information hiding to be unpythonic, in that it suggests that the class in question contains unaesthetic or ill-planned

Have you ever seen design with reasonable usage of protected internal access modifier?

浪尽此生 提交于 2019-12-03 11:34:58
I haven't, but I don't say there isn't one. All of the C# developers who read this probably do know what is protected internal and when to use it. My question is simple : did you actually ever use it or worked on a successfully designed project using protected internal access modifier? If yes, please share your knowledge and post nice sample, where I can finally appreciate clever usage of this tricky modifier. // I believe this isn't subjective, I am actually seeking for an answer ;-) I'm sure you can think of examples, but in five years of C# development, I've not seen a good case for it.

Initializer is inaccessable due to 'internal' protection level

时间秒杀一切 提交于 2019-12-03 10:22:51
问题 I have some protocols LoginStrategy public protocol LoginStrategy { func login(_ viewController: UIViewController) func getUserInfo(withCompletionHandler completionHandler: @escaping (_ userInfo: [String: Any]?) -> ()) func createLoginButton(_ frame: CGRect, withCompletionHandler completionHandler: @escaping (_ loginButton: UIView) -> ()) func getUserId() -> String } and two classes: LoginProvider public class LoginProvider { public let strategy: LoginStrategy public func login(_

Why must a base class destructor be accessible only when a custom constructor is declared?

£可爱£侵袭症+ 提交于 2019-12-03 08:54:00
问题 Comeau, g++ (ideone) and EDG accept the following code without diagnostic. Visual C++ compiles successfully, albeit with warning C4624. class indestructible_base { ~indestructible_base(); }; class T : indestructible_base { public: //T() {} }; int main(void) { new T(); } Uncomment the constructor and it no longer compiles. Perhaps it's the rule that if an exception occurs inside the constructor, subobjects must be destroyed? Seems odd, since the body is empty and can't cause an exception. Even