protected

Protected member access from different packages in java - a curiosity [duplicate]

时间秒杀一切 提交于 2019-11-27 07:53:28
This question already has an answer here: Understanding java's protected modifier 6 answers package packageOne; public class Base { protected void display(){ System.out.println("in Base"); } } package packageTwo; public class Derived extends packageOne.Base { public void show(){ new Base().display(); //this is not working throws compilation error that display() from the type Base is not visible new Derived().display(); //is working display(); //is working } } The two packages are in two different files. But why this behaviour? irreputable http://java.sun.com/docs/books/jls/third_edition/html

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)

Access protected member of a class in a derived class

落花浮王杯 提交于 2019-11-27 06:47:01
问题 i have an old codebase here, where they used protected member variables. Whether or not this is a good idea can be discussed. However, the code must have compiled fine with gcc3. I have a derived template class Bar that uses protected member x from class template Foo like so template <class Something> class Foo { public: // stuff... protected: some::type x; } template <class Something> Bar : Foo<Something> { public: void cleanup(); } And in the method declaration of cleanup() there is

Why can a class not be defined as protected?

99封情书 提交于 2019-11-27 06:22:11
I know this is a stupid question, but I still have a doubt which needs to be cleared. My question is, why can we not define a class as protected ? I know that we can't, but why? There should be some specific reason. Because it makes no sense. Protected class member (method or variable) is just like package-private (default visibility), except that it also can be accessed from subclasses. Since there's no such concept as 'subpackage' or 'package-inheritance' in Java, declaring class protected or package-private would be the same thing. You can declare nested and inner classes as protected or

Protected access modifier in Java

一曲冷凌霜 提交于 2019-11-27 04:33:27
I am having a little trouble understanding the protected access modifier in java (or the design behind it). I thought it meant package access and access through objects that inherit the class containing an abstract member. I wrote the following sample code. I see that the commented out line produces a compilation error if uncommented. Why can I access pro through a Second object in Second but not through a First object in Second? package first; public class First { protected void pro(){ System.out.println("Can see protected method"); } } package first; public class InFirst { public static void

What does the protected modifier mean? [closed]

为君一笑 提交于 2019-11-27 03:45:28
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 7 years ago . 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

Reasons to avoid access modifiers in php [closed]

我怕爱的太早我们不能终老 提交于 2019-11-27 02:34:11
问题 What are valid reasons NOT to use keywords public, private, protected in php? The story: I've started a project with a team that actively uses access modifiers in their code (even "public" explicitly) and wants to convince me to do the same. I always find this kind of stuff totally useless in a dynamic language like php, but I realize that my gut feeling is hardly an argument in a technical discussion. Therefore I'm looking for a solid, clear explanation why access modifiers are useless (or

Public and private inheritance in C++

别说谁变了你拦得住时间么 提交于 2019-11-27 01:55:10
问题 As we know from the literature for the public inheritance the object of child class (sub-class) also can be considered as the object of base class (super-class). Why the object of the sub-class can’t be considered as an object of super-class, when the inheritance is protected or private? 回答1: Because you can't see it: class Base { public: virtual ~Base() {} }; class PublicDerived: public Base { }; class PrivateDerived: private Base { }; int main() { PublicDerived publicD; PrivateDerived

Why is protected constructor raising an error this this code?

ぃ、小莉子 提交于 2019-11-26 22:44:09
One question about protected constructor. I learnt that the protected constructor can be used in the derived class. How ever, I found the code below has an error. Why does it happen like this? class A { protected: A(){} }; class B: public A { public: B() { A* f=new A(); // Why it is not working here } }; This has nothing to do with constructors specifically. This is just how protected access works. The way protected access specifier works, it allows the derived class B to access the contents of an object of base class A only when that object of class A is a subobject of class B . That means

Access to protected member through member-pointer: is it a hack?

时光毁灭记忆、已成空白 提交于 2019-11-26 22:35:34
问题 We all know members specified protected from a base class can only be accessed from a derived class own instance. This is a feature from the Standard, and this has been discussed on Stack Overflow multiple times: Cannot access protected member of another instance from derived type's scope ; Why can't my object access protected members of another object defined in common base class? And others. But it seems possible to walk around this restriction with member pointers, as user chtz has shown