protected

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

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

How can a derived class use a protected member of the base class?

三世轮回 提交于 2019-11-28 08:59:26
问题 Say that a base class A defines a protected member. A derived class B uses this member. class A { public: A(int v) : value(v) { } protected: int value; }; class B : public A { public: B(int v) : A(v) { } void print() const; void compare_and_print(const A& other) const; }; The function B::print just takes the value of the current member and prints it: void B::print() const { std::cout << "Value: " << value << "\n"; } The other member function, B::compare_and_print , takes an instance of A ,

Reasons to avoid access modifiers in php [closed]

六眼飞鱼酱① 提交于 2019-11-28 08:49:43
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 even harmful) in php. I'm aware that some similar topics already exist Importance of protected/private

Public and private inheritance in C++

巧了我就是萌 提交于 2019-11-28 07:40:26
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? Because you can't see it: class Base { public: virtual ~Base() {} }; class PublicDerived: public Base { }; class PrivateDerived: private Base { }; int main() { PublicDerived publicD; PrivateDerived privateD; Base& base1 = publicD; Base& base2 = privateD; // ERROR } So you can not use a PrivateDerived object

C# protected members accessed via base class variable [duplicate]

时光毁灭记忆、已成空白 提交于 2019-11-28 07:13:15
问题 This question already has an answer here: Why can't I access C# protected members except like this? 7 answers It may seems rather newbie question, but can you explain why method Der.B() cannot access protected Foo via Base class variable? This looks weird to me: public class Base { protected int Foo; } public class Der : Base { private void B(Base b) { Foo = b.Foo; } // Error: Cannot access protected member private void D(Der d) { Foo = d.Foo; } // OK } Thanks! 回答1: This is a frequently asked

C# accessing protected member in derived class [duplicate]

梦想的初衷 提交于 2019-11-28 07:12:41
问题 This question already has answers here : Why can't I access C# protected members except like this? (7 answers) Closed 6 years ago . I wrote the following code: public class A { protected string Howdy = "Howdy!"; } public class B : A { public void CallHowdy() { A a = new A(); Console.WriteLine(a.Howdy); } } Now, in VS2010 it results in the following compilation error: Cannot access protected member 'A.a' via a qualifier of type 'A'; the qualifier must be of type 'B' (or derived from it). This

Using make_shared with a protected constructor + abstract interface

倾然丶 夕夏残阳落幕 提交于 2019-11-28 04:08:44
问题 Given an abstract interface and an implementation derived from that interface, where constructors are protected (creation of these objects only being available from a class factory - to implement a DI pattern), how can I make use of make_shared in the factory function? For example: class IInterface { public: virtual void Method() = 0; }; class InterfaceImpl : public IInterface { public: virtual void Method() {} protected: InterfaceImpl() {} }; std::shared_ptr<IInterface> Create() { std:

Java: cannot access a protected member of the superclass in the extending subclass

戏子无情 提交于 2019-11-28 01:08:09
问题 I want some discussions about this, but I could not infer the answer for my case. Still need help. Here is my code: package JustRandomPackage; public class YetAnotherClass{ protected int variable = 5; } package FirstChapter; import JustRandomPackage.*; public class ATypeNameProgram extends YetAnotherClass{ public static void main(String[] args) { YetAnotherClass bill = new YetAnotherClass(); System.out.println(bill.variable); // error: YetAnotherClass.variable is not visible } } Some

Protected global variables in Fortran

▼魔方 西西 提交于 2019-11-28 01:03:25
问题 I wonder if there is a way of having a global variable in Fortran, which can be stated as some kind of 'protected'. I am thinking of a module A that contains a list of variables. Every other module or subroutine that uses A can use it's variables. If you know what the value of the variable is, you could use parameter to achieve that it can't be overwritten. But what if you have to run code first to determine the variables value? You could not state it as parameter since you need to change it.