access-modifiers

protected/public Inner Classes

两盒软妹~` 提交于 2019-12-18 03:24:19
问题 Can someone please explain to me what is the difference between protected / public Inner classes? I know that public inner classes are to avoid as much as possible (like explained in this article). But from what I can tell, there is no difference between using protected or public modifiers. Take a look at this example: public class Foo1 { public Foo1() { } protected class InnerFoo { public InnerFoo() { super(); } } } ... public class Foo2 extends Foo1 { public Foo2() { Foo1.InnerFoo innerFoo

Protected method in python [duplicate]

有些话、适合烂在心里 提交于 2019-12-18 03:02:58
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Making a method private in a python subclass Private Variables and Methods in Python How can I define a method in a python class that is protected and only subclasses can see it? This is my code: class BaseType(Model): def __init__(self): Model.__init__(self, self.__defaults()) def __defaults(self): return {'name': {}, 'readonly': {}, 'constraints': {'value': UniqueMap()}, 'cType': {} } cType = property(lambda

Why have class-level access modifiers instead of object-level?

心不动则不痛 提交于 2019-12-17 23:23:48
问题 While using C#, I recently realised that I can call a Foo object's private functions from Foo 's static functions, and even from other Foo objects. After everything I have learned about access modifiers, this sounds very strange to me. As far as I know, you make a function private when it does something that's part of some kind of internal process. Only the object itself knows when to use those functions, because other objects shouldn't/can't control the object's flow. Is there any reason why

Is protected method in super class visible in sub class in a different package? [duplicate]

送分小仙女□ 提交于 2019-12-17 21:12:31
问题 This question already has answers here : What is the difference between public, protected, package-private and private in Java? (28 answers) Closed 2 years ago . It seems very silly, but I am really confused. Please see below code: package com.one; public class SuperClass { protected void fun() { System.out.println("base fun"); } } ---- package com.two; import com.one.SuperClass; public class SubClass extends SuperClass{ public void foo() { SuperClass s = new SuperClass(); s.fun(); // Error

Using a private variable in a inherited class - Java

人盡茶涼 提交于 2019-12-17 11:03:54
问题 Need to have more understanding about the private variables and inheritance. Earlier my understanding was if there is field in a class and when I'm inheriting the class, the fields that is not restricted by access(private variables) will be there in the inherited class. But I'm able use the private variables in base class if there is a public g/setter method. How can I imagine a private variable in a base class.? 回答1: class A { private int a; public A(int a) { this.a = a; } public int getA()

Using a private variable in a inherited class - Java

不问归期 提交于 2019-12-17 11:02:56
问题 Need to have more understanding about the private variables and inheritance. Earlier my understanding was if there is field in a class and when I'm inheriting the class, the fields that is not restricted by access(private variables) will be there in the inherited class. But I'm able use the private variables in base class if there is a public g/setter method. How can I imagine a private variable in a base class.? 回答1: class A { private int a; public A(int a) { this.a = a; } public int getA()

Modifier Keyword order in Java

僤鯓⒐⒋嵵緔 提交于 2019-12-17 10:26:14
问题 Every time I write method in Java with more keywords than public void , every time I write it another way. Sometimes " static public void " sometimes " public static void " etc. What is the best order (best practices) for these keywords? [ abstract/static ] [ final ] [ synchronized ] [ public/private/protected ] [ result_type ] ??? 回答1: In theory it does not matter if you say public static final or final static public, but if you follow the usual convention, other people will able to read

Are there any reasons to use private properties in C#?

孤街浪徒 提交于 2019-12-17 03:24:07
问题 I just realized that the C# property construct can also be used with a private access modifier: private string Password { get; set; } Although this is technically interesting, I can't imagine when I would use it since a private field involves even less ceremony : private string _password; and I can't imagine when I would ever need to be able to internally get but not set or set but not get a private field: private string Password { get; } or private string Password { set; } but perhaps there

Access package-private variables using reserved package name

时光毁灭记忆、已成空白 提交于 2019-12-13 14:22:31
问题 I was studying the source code of java.util.HashMap and I wanted to see how well the hashcode function distributes the key's in the internal array of Entry 's( which is a package private variable ).So I made a package named java.util in my project just to check weather I can fool the compiler to think it's the same package.Surprisingly it worked and I wrote the following code: package java.util; public class HashMapExt<K, V> extends HashMap<K, V> implements Map<K, V> { public static void main

hiding internal services from outside world to ensure the correct high-level service is being used [closed]

前提是你 提交于 2019-12-13 07:59:54
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 2 months ago . I am working on an e-commerce website. I have advertisement entity which include both properties and photos. Properties are written to DB and photos are stored in file system. I have created a WriterService in my infrastructure project, this service is responsible to save an ad... under the hood