access-modifiers

Why the protected modifier behave differently here in Java subclass?

独自空忆成欢 提交于 2019-12-11 04:55:49
问题 I have the following two class in two different packages. my access modifier for instance method is protected which means any subclass in same or different package has access to it right?. however, in Eclipse I see the following message on my subclass Cat on line 17 The method testInstanceMethod() from the type Animal is not visible My code for super and subclass are below. package inheritance; public class Animal { public static void testClassMethod() { System.out.println("The class" + "

Change the access modifier in runtime

我们两清 提交于 2019-12-11 04:40:54
问题 Solely for the sake of example I've written this: public class MyClass { private int MyMethod() { return 1; } } Is there any way to change access modifier of MyClass::MyMethod() in runtime to public using Reflection or something else and then invoke it? 回答1: Not really sure if you can modify the access specifier, but you can call the private method through reflection like: MyClass instance = new MyClass(); MethodInfo yourMethod = instance .GetType() .GetMethod("MyMethod", BindingFlags

Java default access level (package private access). Why it is used for? [duplicate]

一世执手 提交于 2019-12-11 02:15:25
问题 This question already has answers here : Pros and cons of package private classes in Java? (8 answers) Closed 4 years ago . Although I know how default (package) access level works in java, I can't imagine its real use in applications except for those with unique package. What do you use default access level for in business (usually multipackage) java applications? 回答1: A very common pattern for using package-private classes is to create shared implementations of interfaces or shared

Is there a C# equivalent to access-modifier regions in C++

不羁的心 提交于 2019-12-10 18:29:10
问题 It's possible to declare variables with the below structure in C++ private: public: protected: float bla1; float bla2; float bla3; Is there an equivalent in C#? It seems rather tedious having to repeat yourself; protected float bla1; protected float bla2; protected float bla3; 回答1: No there isn't such a thing. In fact, it's designed to be like that to make code more readable. This applies to both C# and Java. 回答2: No. The access is specified on each declaration. The benefit of this is that a

How is Groovy able to access private methods of a Java class?

試著忘記壹切 提交于 2019-12-10 17:27:30
问题 Groovy can access private methods and variables of a Java class. How does Groovy do this behind the scene? Is it because of the use of invokedynamic bytecode instruction which is used by MethodHandle class? I think Java uses invokespecial instruction for calling private methods and invokevirtual for public right which respects access modifiers? 回答1: Groovy is written in Java, so it hopefully doesn't rely on the byte code directly, it doesn't it using the Reflection API. For more details check

How can I find out what a method's visibility is via reflection?

走远了吗. 提交于 2019-12-10 16:55:05
问题 Context: I'm trying to learn/practice TDD and decided I needed to create an immutable class. To test the 'immutability invariant' (can you say that?) I thought I would just call all the public methods in the class via reflection and then check that the class had not changed afterwards. That way I would be unlikely to break the invariant carelessly later on. This may or may not be practical/valid in itself but I thought it would also be an exercise in reflection for me. Strategies: Use

How to make a field read only outside class

不羁岁月 提交于 2019-12-10 15:26:37
问题 I have the following class (example): public class Dog { int numberOfTeeth; public Dog() { countTeeth(); } private void countTeeth() { this.numberOfTeeth = 5; //this dog has seen better days, apparently } } After I create the dog object, it should have the number of teeth calculated. I'd like to be able to access that value without being able to modify it outside the class itself. Dog d = new Dog(); int dogTeeth = d.numberOfTeeth; //this should be possible d.numberOfTeeth = 10; //this should

extern access modifiers don't work

别等时光非礼了梦想. 提交于 2019-12-10 15:21:03
问题 I'm trying to hide my P/Invoke functions, like this one: [<DllImport("kernel32.dll", SetLastError=true)>] extern bool private CreateTimerQueueTimer(IntPtr& phNewTimer, nativeint TimerQueue, WaitOrTimerDelegate Callback, nativeint Parameter, uint32 DueTime, uint32 Period, ExecuteFlags Flags) Strangely, though, the private gets ignored -- which is really annoying, because I want to hide all the unwieldy structs and enums associated with these functions. I guess I could put everything in a

Where can I read more about D's class access modifiers?

回眸只為那壹抹淺笑 提交于 2019-12-10 14:50:12
问题 I can't seem to find a good reference for the D programming language class access modifiers. I know that public and private are pretty much guaranteed but i'm guessing there are more, i just can't seem to find a good source of information. Can anyone point me to a good reference please? 回答1: You can check the official reference at dlang.org in short: private , is only accessible in the same module (file) package , is only accessible in the same package protected , is module + inherited

Force the use of interface instead of concrete implementation in declaration (.NET)

牧云@^-^@ 提交于 2019-12-10 13:18:03
问题 In C++, you can do the following: class base_class { public: virtual void do_something() = 0; }; class derived_class : public base_class { private: virtual void do_something() { std::cout << "do_something() called"; } }; The derived_class overrides the method do_something() and makes it private . The effect is, that the only way to call this method is like this: base_class *object = new derived_class(); object->do_something(); If you declare the object as of type derived_class , you can't