access-specifier

Why make private inner class member public in Java?

∥☆過路亽.° 提交于 2019-11-27 18:45:36
What is the reason of declaring a member of a private inner class public in Java if it still can't be accessed outside of containing class? Or can it? public class DataStructure { // ... private class InnerEvenIterator { // ... public boolean hasNext() { // Why public? // ... } } } Gursel Koca If the InnerEvenIterator class does not extend any class or implement any interface, I think it is nonsense because no other class can access any instance of it. However, if it extends or implements any other non private class or interface, it makes sense. An example: interface EvenIterator { public

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

試著忘記壹切 提交于 2019-11-27 17:41:23
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 me : struct Base { protected: int value; }; struct Derived : Base { void f(Base const& other) { //int n

Private module methods in Ruby

血红的双手。 提交于 2019-11-27 16:55:13
I have a two part question Best-Practice I have an algorithm that performs some operation on a data structure using the public interface It is currently a module with numerous static methods, all private except for the one public interface method. There is one instance variable that needs to be shared among all the methods. These are the options I can see, which is the best?: Module with static ('module' in ruby) methods Class with static methods Mixin module for inclusion into the data structure Refactor out the part of the algorithm that modifies that data structure (very small) and make

Why can I access private variables in the copy constructor?

眉间皱痕 提交于 2019-11-27 16:54:29
I have learned that I can never access a private variable, only with a get-function in the class. But then why can I access it in the copy constructor? Example: Field::Field(const Field& f) { pFirst = new T[f.capacity()]; pLast = pFirst + (f.pLast - f.pFirst); pEnd = pFirst + (f.pEnd - f.pFirst); std::copy(f.pFirst, f.pLast, pFirst); } My declaration: private: T *pFirst,*pLast,*pEnd; IMHO, existing answers do a poor job explaining the "Why" of this - focusing too much on reiterating what behaviour's valid. "access modifiers work on class level, and not on object level." - yes, but why? The

Why is it allowed to call derived class' private virtual method via pointer of base class?

无人久伴 提交于 2019-11-27 14:49:48
问题 # include <iostream> using namespace std; class A { public: virtual void f() { cout << "A::f()" << endl; } }; class B:public A { private: virtual void f() { cout << "B::f()" << endl; } }; int main() { A *ptr = new B; ptr->f(); return 0; } This code works correctly and prints B::f(). I know how it works, but why is this code allowed? 回答1: Access control is performed at compile time, not runtime. There's no way in general for the call to f() to know the runtime type of the object pointed to by

How to Access package private Class from a Class in some other package?

一笑奈何 提交于 2019-11-27 11:48:23
问题 I have following classses Hello.java package speak.hello; import java.util.Map; import speak.hi.CustomMap; import speak.hi.Hi; public class Hello { private Hi hi; Hello(Hi hi) { this.hi = hi; } public String sayHello() { return "Hello"; } public String sayHi() { return hi.sayHi(); } public Map<String, Object> getMap() { return hi.getMap(); } public void clearMap() { hi.getMap().clear(); } public void discardMap() { CustomMap map = (CustomMap) hi.getMap(); map.discard(); } public static void

Meaning of “Cannot reduce the visibility of the inherited method” with an interface

帅比萌擦擦* 提交于 2019-11-27 09:18:11
I have two files: public interface PrintService { void print(PrintDetails details); class PrintDetails { private String printTemplate; } public interface Task { String ACTION = "print"; } } and public class A implements PrintService { void print(PrintDetails details) { System.out.println("printing: " + details); } String action = PrintService.Task.ACTION; } I thought the code looks okay, but I am getting an error in the second file for the line void print(PrintDetails details) { that states: Cannot reduce the visibility of the inherited method from PrintService . Can someone explain what this

Interfaces in Java: cannot make implemented methods protected or private

余生颓废 提交于 2019-11-27 01:30:08
问题 I know that an interface must be public. However, I don't want that. I want my implemented methods to only be accessible from their own package, so I want my implemented methods to be protected. The problem is I can't make the interface or the implemented methods protected. What is a work around? Is there a design pattern that pertains to this problem? From the Java guide, an abstract class wouldn't do the job either. 回答1: read this. "The public access specifier indicates that the interface

How to set private instance variable used within a method test?

雨燕双飞 提交于 2019-11-27 01:19:40
问题 Given a class with a couple of instance variables and some methods. Some instance variables are set accessible via attr_reader and attr_accessor . Thus the others are private. Some of the private instance variables get set within one of the instance methods and read/utilized within another method. For testing I'm using RSpec. As I'm still new to Ruby and want to get all things right, I defined my tests being rather fine-grained. Thus I've got one describe block for each instance method, which

Understanding private methods in Ruby

笑着哭i 提交于 2019-11-27 00:21:19
class Example private def example_test puts 'Hello' end end e = Example.new e.example_test This of course will not work, because we specified explicit receiver - instance of Example ( e ), and that is against a "private rule". But I cannot understand, why one cannot do in Ruby this: class Foo def public_m self.private_m # <= end private def private_m puts 'Hello' end end Foo.new.public_m The current object inside public_m method definition (i.e. self ) is the instance of Foo. So why it is not allowed? To fix that I have to change self.private_m to just private_m . But why this differ, isn't