access-modifiers

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

邮差的信 提交于 2019-11-28 14:17:17
This question already has an answer here: What is the difference between public, protected, package-private and private in Java? 25 answers 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 Msg: Change visibility of fun() to public } } I have read from oracle doc and here as well, that protected members are visible

Package and visibility

亡梦爱人 提交于 2019-11-28 12:37:10
I'm making an SDK and I'm trying to separate classes to different packages, those classes use some other shared classes. The issue is if I made the shared classes public everyone will be able to see them, not only my classes. What's the right way to make them only accessible by my application? Example : Package a MyClass1 Package b MyClass2 Package c public MySharedClass Because c is public MySharedClass will be able to access it, but the issue is that it will also will be visible to the world, how could I prevent that? Create a package that is documented as an internal package, not to be used

Accessing clone() from java.lang.Object

前提是你 提交于 2019-11-28 11:37:21
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 doesn't say that it HAS to satisfy both conditions (inside same package AND also to be a subclass).

When should [assembly: InternalsVisibleTo()] be used?

不问归期 提交于 2019-11-28 10:55:53
I understand that the InternalVisibleTo attribute is used to expose types and methods with the internal access modifier to a specified assembly. I have only ever used this for exposing internal methods to a separate assembly containing a suite of unit tests. I am struggling to think of another scenario when this should be used. Was this attribute introduced specifically to aid unit testing or was there another reason? A scenario can be that you have separation of logic between assemblies (like internal data objects and the logic layer). You don't want to expose the classes to your users, but

What does the protected modifier mean? [closed]

心已入冬 提交于 2019-11-28 10:42:25
I am reading the book The Java Programming Language, 3rd edition . In chapter 3.5 , it illustrates the protected modifier with the following words: More precisely, beyond being accessible within the class itself and to code within the same package, a protected member can also be accessed from a class through object references that are of at least the same type as the class that is, references of the class's type or one its subtypes. The words makes me confused, in two aspects: 1. protected member can be accessed by code within the same package ? What I knew before is protected member can only

Access modifiers on interface members in C#

∥☆過路亽.° 提交于 2019-11-28 09:07:12
I am getting a compile error from the following property. The error is: "The modifier 'public' is not valid for this item" public System.Collections.Specialized.StringDictionary IWorkItemControl.Properties { get { return properties; } set { properties = value; } } but if I remove the IWorkItemControl it compiles fine. Why am I getting this error and what is the difference of having / not having the interface name in the signature? Explicit interface implementation does not let you specify any access modifiers. When you implement an interface member explicitly (by specifying the interface name

Is there anything like an Internal class in Java?

荒凉一梦 提交于 2019-11-28 07:11:00
In C# you can mark a class as internal so that it is only accessible from within the same package. Is there anything similar in Java? You can create package-private classes by omitting the security modifier (public, private) from the class's declaration. package com.sample; class MyPackagePrivateClass { ... } Dropping the access modifier is similar to internal in C#. C# public class A { public static int X; internal static int Y; private static int Z; } internal class B { public static int X; internal static int Y; private static int Z; public class C { public static int X; internal static int

What is the difference between access modifiers in php? [closed]

…衆ロ難τιáo~ 提交于 2019-11-28 06:56:25
问题 I am totally confused with access modifiers in php. Is there any difference regarding memory utilization for access modifiers or only difference of accessibility..Please suggest. If i have following code: public Class Employee { public $emp_name='xyz'; protected $emp_phone='1234567891'; private $emp_code='101'; public function getName($name) { return 'Employee name is :' . $name; } protected function getPhone($ph) { return 'Employee contact number is :' . $ph; } private function getCode($id)

Why is internal protected not more restrictive than internal?

萝らか妹 提交于 2019-11-28 04:55:58
I'd like to create an internal auto-property: internal bool IP { get; protected internal set; } I thought it would be possible to make the setter protected or protected internal - but I always get the error accessibility modifier must be more restrictive than the property . Isn't that the case? Private does not help me, here. EDIT: The question is: How do I implement an auto-property with a internal getter and a protected setter? It's effectively protected or internal , not and . It's accessible both by derived classes and types in the same assembly. It's a common misconception to think

Why can I access a derived private member function via a base class pointer to a derived object?

扶醉桌前 提交于 2019-11-28 04:31:41
#include<iostream> using namespace std; class base { public: virtual void add() { cout << "hi"; } }; class derived : public base { private: void add() { cout << "bye"; } }; int main() { base *ptr; ptr = new derived; ptr->add(); return 0; } Output is bye I dont have a problem with how this is implemented. I understand you use vtables and the vtable of derived contains the address of the new add() function. But add() is private shouldn't compiler generate an error when I try to access it outside the class? Somehow it doesn't seem right. add() is only private in derived , but the static type you