access-modifiers

Concept of Private class in C#

回眸只為那壹抹淺笑 提交于 2019-12-03 05:36:13
问题 Can private classes exist in C#, other than in Inner classes? 回答1: Simply NO. Nothing unless its in a nested Class Classes and structs that are not nested within other classes or structs can be either public or internal . A type declared as public is accessible by any other type. A type declared as internal is only accessible by types within the same assembly. Classes and structs are declared as internal by default unless the keyword public is added to the class definition. Class or struct

Do constructors always have to be public? [duplicate]

房东的猫 提交于 2019-12-03 01:37:57
问题 This question already has answers here : What is the use of private constructor in java [closed] (10 answers) Closed 4 years ago . My first question is - class Explain() { public Explain() { } } Should Constructor always declared as public? What if I create a private constructor. I always seen constructors are implicitly public . So why private constructor is useful? Or is it not useful at all. Because nobody could ever call it, or never make an object(because of the private constructor) !

Initializer is inaccessable due to 'internal' protection level

血红的双手。 提交于 2019-12-03 00:53:30
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(_ viewController: UIViewController) { return self.strategy.login(viewController) } public func getUserInfo

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

杀马特。学长 韩版系。学妹 提交于 2019-12-02 22:53:04
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 so, add an exception-specification vouching for the fact that no exception will be thrown ( throw()

Are private methods really safe?

╄→尐↘猪︶ㄣ 提交于 2019-12-02 15:41:35
In Java the private access modifier consider as safe since it is not visible outside of the class. Then outside world doesn't know about that method either. But I thought Java reflection can use to break this rule. Consider following case: public class ProtectedPrivacy{ private String getInfo(){ return "confidential"; } } Now from another class I am going to get Info: public class BreakPrivacy{ public static void main(String[] args) throws Exception { ProtectedPrivacy protectedPrivacy = new ProtectedPrivacy(); Method method = protectedPrivacy.getClass().getDeclaredMethod("getInfo", null);

Which Java access modifier allows a member to be accessed only by the subclasses in other package? [duplicate]

早过忘川 提交于 2019-12-02 10:12:29
This question already has an answer here: Why does the “protected” modifier in Java allow access to other classes in same package? 6 answers I had to shorten the title a bit. Here's the full question: In Java which access modifier allows a member to be accessed only by the subclasses in other package or any class within the package of that member's class? I am thinking protected but my office mate says the answer is private . In Java which access modifier allows a member to be accessed only by the subclasses in other package or any class within the package of that member's class? see this

How does access modifier impact the performance in Java?

走远了吗. 提交于 2019-12-02 04:10:47
In java programming, method level access modifiers using protected or public where private can be used will affect the performance in any way? if so in what what way it affects the run time performance. lexicore Quoting erickson from the following answer : The access modifier on the field doesn't make any difference in speed, but invoking the accessor method does . ps. Please upvote the original answer instead of this one. Even if there is a theoretical difference, I doubt that there is a measurable diffrence. At least in any sensible Java runtime 来源: https://stackoverflow.com/questions

Scope of protected members

旧街凉风 提交于 2019-12-02 01:34:25
Iam preparing for SCJP , also i came to know that protected members scope is within the package as well as in other package with some conditions like possible only with inheritance. For example : i have three classes as Parentclass Childclass Friendclass package x.parent; class Parentclass{ protected int x=10; ............... } package x.child; class Childlass extends Parentclass{ super.x=20; ............... } package x.child; import x.parent.Parentclass; class Friendclass{ Parentclass pc = new Parentclass(); pc.x=30; ............... } Whats the reason behind that, in Friendclass the member x

Java - Class method can see private fields of same-class parameter

柔情痞子 提交于 2019-12-01 20:29:02
问题 I'm encountering a rather odd behavior, and not sure if this is a Java issue or just something with Eclipse. Take the following code: class Foo { private String text; public void doStuff(Foo f) { System.out.println(f.text); } } The problem here is, why is f.text accessible? It's a private field, so by my logic, it shouldn't be, but the IDE seems to think it is. 回答1: This is by design. Private fields are accessible within the same class, even if a different instance . See here for more details

Java - Class method can see private fields of same-class parameter

天涯浪子 提交于 2019-12-01 19:25:06
I'm encountering a rather odd behavior, and not sure if this is a Java issue or just something with Eclipse. Take the following code: class Foo { private String text; public void doStuff(Foo f) { System.out.println(f.text); } } The problem here is, why is f.text accessible? It's a private field, so by my logic, it shouldn't be, but the IDE seems to think it is. This is by design. Private fields are accessible within the same class, even if a different instance . See here for more details and an official statement from Oracle on this. Since doStuff is a member of Foo , any private fields of Foo