access-modifiers

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

情到浓时终转凉″ 提交于 2019-12-01 19:20:19
This question already has an answer here: Why and how does C# allow accessing private variables outside the class itself when it's within the same containing class? 3 answers 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(); // Why is this OK? } return 0; } Because private means "not accessible to other types ", not "not accessible to other

Access modifier best practice in C# vs Java

僤鯓⒐⒋嵵緔 提交于 2019-12-01 16:54:58
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 or what? I find myself frequently going through my C# code (I habitually make most classes public,

Go to Definition of class only showing public members

≯℡__Kan透↙ 提交于 2019-12-01 13:49:00
When I right click on a class (that is part of an third party assembly - not code I have written) in Visual Studio and select "Go to Definition", I can see all the methods, properties etc of the class. I notice all these are all public and no private (or any other level of access) members are shown. Does this feature only show public members of the class? You can find the answer on the doc page : When you try to run the Go To Definition or Peek Definition command for types or members that are marked as internal, Visual Studio does not display their metadata as source code, regardless of

What does the “private” modifier do?

杀马特。学长 韩版系。学妹 提交于 2019-12-01 13:44:22
问题 Considering "private" is the default access modifier for class Members, why is the keyword even needed? 回答1: It's for you (and future maintainers), not the compiler. 回答2: There's a certain amount of misinformation here: "The default access modifier is not private but internal" Well, that depends on what you're talking about. For members of a type, it's private. For top-level types themselves, it's internal. "Private is only the default for methods on a type" No, it's the default for all

How do I mimic access modifiers in JavaScript with the Prototype library?

旧城冷巷雨未停 提交于 2019-12-01 10:47:17
I've been working with the prototype library for some time now and occasionally find myself wishing I had multiple access levels (public, private, and protected). The closest I've come so far is the following: SampleBase = Class.create({ /* virtual public constructor */ initialize: function(arg1, arg2) { // private variables var privateVar1, privateVar2; // private methods var privateMethod1 = function() { } function privateMethod2() { } // public (non virtual) methods this.publicNonVirtual1 = function() { return privateVar1; } this.publicNonVirtual2 = function() { return privateVar2; } }, //

Go to Definition of class only showing public members

北城以北 提交于 2019-12-01 09:56:06
问题 When I right click on a class (that is part of an third party assembly - not code I have written) in Visual Studio and select "Go to Definition", I can see all the methods, properties etc of the class. I notice all these are all public and no private (or any other level of access) members are shown. Does this feature only show public members of the class? 回答1: You can find the answer on the doc page: When you try to run the Go To Definition or Peek Definition command for types or members that

How can i hide “setters” from all but one assembly?

帅比萌擦擦* 提交于 2019-12-01 09:40:28
I have alluded to this issue in my other question , but i think it's worthwhile breaking it out into its own question, as it's not really dependant on the other scenarios i mentioned. Anyways - onto the Q, don't know if this is possible. Looking for a solution/workaround. I have a Class Library, with nothing but POCO's: MyCompany.MyProject.Domain.POCO This assembly has a POCO like this: public class Post { public int PostId { get; set; } public int Name { get; set; } ... } Now, i have another Class Library, which is my DAL/Repository, and uses Entity Framework 4.0 for the persistence:

How can i hide “setters” from all but one assembly?

大城市里の小女人 提交于 2019-12-01 08:50:17
问题 I have alluded to this issue in my other question, but i think it's worthwhile breaking it out into its own question, as it's not really dependant on the other scenarios i mentioned. Anyways - onto the Q, don't know if this is possible. Looking for a solution/workaround. I have a Class Library, with nothing but POCO's: MyCompany.MyProject.Domain.POCO This assembly has a POCO like this: public class Post { public int PostId { get; set; } public int Name { get; set; } ... } Now, i have another

Java constructor having broader access level than its class

戏子无情 提交于 2019-12-01 08:00:19
Java specification allows a class with default access to have its constructor public access, what is the purpose of it, since it cannot be referenced outside its package? I wanted to make this a comment, but since no code tags are allowed in comments.... In regards to your comment on CristopheDs answer: package bob; class MySuperHiddenClass { public MySuperHiddenClass() { System.out.println("bob"); } } And package bob; public class MyPublicClass extends MySuperHiddenClass { } No constructor was declared in MyPublicClass, but you can still call new MyPublicClass from any package. If you are

Java constructor having broader access level than its class

Deadly 提交于 2019-12-01 07:10:17
问题 Java specification allows a class with default access to have its constructor public access, what is the purpose of it, since it cannot be referenced outside its package? 回答1: I wanted to make this a comment, but since no code tags are allowed in comments.... In regards to your comment on CristopheDs answer: package bob; class MySuperHiddenClass { public MySuperHiddenClass() { System.out.println("bob"); } } And package bob; public class MyPublicClass extends MySuperHiddenClass { } No