access-modifiers

Call protected method from a subclass of another instance of different packages

白昼怎懂夜的黑 提交于 2019-11-27 16:55:12
问题 I want to invoke a protected method of another instance from within a subclass of the class providing this protected method. See the following example: public class Nano { protected void computeSize() { } } public class NanoContainer extends Nano { protected ArrayList<Nano> children; } public class SomeOtherNode extends NanoContainer { // {Nano} Overrides protected void computeSize() { for (Nano child: children) { child.computeSize(); // << computeSize() has protected access in nanolay.Nano }

Using a private variable in a inherited class - Java

て烟熏妆下的殇ゞ 提交于 2019-11-27 14:10:40
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.? class A { private int a; public A(int a) { this.a = a; } public int getA() {return a;} } class B extends A { public B(int b) { super(b); } public int getB() {return getA();} } int

Can I override a private method in Java?

a 夏天 提交于 2019-11-27 14:05:42
I know I can use reflection to invoke a private method, and to get or set the value of a private variable, but I want to override a method. public class SuperClass { public void printInt() { System.out.println("I am " + getClass() + ". The int is " + getInt()); } private int getInt() { return 1; } } public class SubClass extends SuperClass { public static void main(String[] args) { (new SubClass()).printInt(); } public int getInt() { return 2; } } I want the main method in SubClass to print out 2 , but it prints out 1 . I've heard this can be done through reflection, but I can't figure out how

Modifier Keyword order in Java

℡╲_俬逩灬. 提交于 2019-11-27 11: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 ] ??? Nandkumar Tekale 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 your code more easily. Here is the preferred order: [ public | protected | private ] static

What is the meaning of the planned “private protected” C# access modifier?

微笑、不失礼 提交于 2019-11-27 10:24:29
As part of the Roslyn documentation on GitHub, there's a page called Language feature implementation status , with planned language features for C# and VB. One feature I couldn't wrap my head around was private protected access modifier: private protected string GetId() { … } There is also a page of C# Language Design Notes , which explains many new features, but not this one. Eric Lippert said in a comment : Your error is in thinking of the modifiers as increasing restrictions. The modifiers in fact always decrease restrictions. Remember, things are "private" by default; only by adding

Why can't a class or an interface receive private or protected access modifiers?

こ雲淡風輕ζ 提交于 2019-11-27 10:20:04
问题 I am reading some Java text and the text says that we can only apply public or default access modifier for class and interface. Therefore, it is a compiling error if we declare: private class A {} or protected class A{} I am just curious why a class or an interface cannot receive private or protected access modifiers? 回答1: private means "only visible within the enclosing class". protected means "only visible within the enclosing class and any subclasses, and also anywhere in the enclosing

MVC Set accessibility level on a method called from ajax

烈酒焚心 提交于 2019-11-27 09:38:26
I would like to protect my public method from being called by a user. Because I'm calling the action from an ajax script I can't use any access modifiers, (private, protected etc). Also, [HttpPost] doesn't stop the user from doing a fake request. Anyone got a solution? Thanks Create an action filter that allows action methods to be called by AJAX only namespace MyFilters { [AttributeUsage(AttributeTargets.Method)] public class AjaxOnlyAttribute : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { if (!filterContext.HttpContext.Request

Default access modifier for a Java constructor

旧城冷巷雨未停 提交于 2019-11-27 07:50:36
Can anybody explain what the default access modifier is for an explicit no-arg constructor (and other constructors)? Constructors are the same as methods in this respect - if you don't give an explicit public, private or protected then the constructor gets the default "package private" visibility. It can be called from within the same class or from any other class in the same package, but not from subclasses in a different package (so if a class has only package-visible constructors then any subclasses must be in the same package). A private constructor prevents any other class from

Why can't my subclass access a protected variable of its superclass, when it's in a different package?

别来无恙 提交于 2019-11-27 07:48:17
I have an abstract class, relation in package database.relation and a subclass of it, Join , in package database.operations . relation has a protected member named mStructure . In Join : public Join(final Relation relLeft, final Relation relRight) { super(); mRelLeft = relLeft; mRelRight = relRight; mStructure = new LinkedList<Header>(); this.copyStructure(mRelLeft.mStructure); for (final Header header :mRelRight.mStructure) { if (!mStructure.contains(header)) { mStructure.add(header); } } } On lines this.copyStructure(mRelLeft.mStructure); and for (final Header header : mRelRight.mStructure)

Accessing clone() from java.lang.Object

限于喜欢 提交于 2019-11-27 06:18:52
问题 Here is something that I cannot understand. In java.lang.Object the clone() is defined with protected modifier. By definition than it can be accessed by name inside its own class definition, by name inside any class derived from it, and by name in the definition of any class in the same package. Here the Sample class is in another package, and obviously it can't access clone() from the Object class. But as Sample derives implicitly from Object , why is it not able to access it? The definition