encapsulation

Java overridable call in constructor

血红的双手。 提交于 2019-12-07 05:15:15
问题 I know it is a bad (security) practice to call overridable methods from an object constructor in Java. However, for example, if the constructor has to initialize some data, it seems reasonable to call the respective setter method so that I don't copy code. The setters are public and not final. Is there any standard way of dealing with this, like declaring private setter methods, that the public ones call? To illustrate, here is some code: class A { private double x,y; private privateSetX

Access Modifiers - what's the purpose?

廉价感情. 提交于 2019-12-07 05:00:47
问题 I'm relatively new to programming in general and I was wondering if anyone could help me understand the purpose of access modifiers? I understand the fact that they set different levels of access for classes and variables etc. but why would you want to limit what has access to these? What is the point in not allowing access for different things? why not just allow access for everything? Sorry if this is a stupid question! 回答1: There are thousands of reasons, but I'll quote a few from here and

Accessing package-private fields in Java

南楼画角 提交于 2019-12-07 01:30:49
问题 Poking around Android API sources. There's FileDescriptor with a data member descriptor with no access modifier: int descriptor; Then there's class FileOutputStream that constructs a new FileDescriptor and assigns to that field: fd = new FileDescriptor(); fd.descriptor = fileSystem.open(...); How is that compatible with the field access control model of Java? I thought package-private fields are not accessible from outside the declaring class, and there's no notion of friendship like in C++.

Is there a Tomcat-like classloader that can be used standalone?

萝らか妹 提交于 2019-12-06 11:28:40
I'm working with a Java sort-of-application-server (Smartfox) which can run multiple applications ("extensions") but has a very inconvenient classpath setup to go along with it, along with issues when trying to use SLF4J. To work around that I'd like to wrap my applications in their own classloaders. Such a containing classloader should be much like Tomcat's, in that it Can load classes from a directory containing JARs. Prefers classes from its own classpath over those from the parent Is there a library somewhere that has such a classloader I can just "drag and drop" in my project? If not,

Access-specifiers are not foolproof?

邮差的信 提交于 2019-12-06 11:09:24
If I've a class like this, class Sample { private: int X; }; Then we cannot access X from outside, so this is illegal, Sample s; s.X = 10; // error - private access But we can make it accessible without editing the class ! All we need to do is this, #define private public //note this define! class Sample { private: int X; }; //outside code Sample s; s.X = 10; //no error! Working code at ideone : http://www.ideone.com/FaGpZ That means, we can change the access-specifiers by defining such macros just before the class definition, or before #include <headerfile.h> , #define public private //make

Getters for Display

流过昼夜 提交于 2019-12-06 08:15:22
问题 I was researching on getters/setters , and the general idea is that they are evil and should be avoided. You should let the object do the work, and produce the result. Reading Material: Why getter and setter methods are evil Are getters and setters poor design? Contradictory advice seen Why use getters and setters? With all that in mind, suppose I have a Book class that looked like this: publc final Book{ private final String title; private final List<Authors> listofAuthors; private final int

Inheritance breaking encapsulation?

穿精又带淫゛_ 提交于 2019-12-06 03:15:54
问题 People say inheritance breaks encapsulation, which i agree with. They say delegation is better- although the modifiers in delegation can also be public/protected. So is the real reason why inheritance breaks encapsulation because of the "knock-on" effect of the public/protected modifiers from the super class being exposed to any new classes which extend the current subclass? 回答1: Yes. Since it gives the derived class access to members of the base class (depending on what language and which

How to encapsulate database access?

时间秒杀一切 提交于 2019-12-06 02:41:39
I am developing a transactional application in .NET and would like to get some input on how to properly encapsulate database access so that: I don't have connection strings all over the place Multiple calls to the same stored procedure from different functions or WORSE, multiple stored procedures that are different by a single column I am interested in knowing if using an ORM like NHibernate is useful, as it may just add another layer of complexity to a rapidly changing data model, and artifacts need to be produced on a tight schedule. I am more interested in methods or patterns OTHER than ORM

Javascript scope referencing outer object

不打扰是莪最后的温柔 提交于 2019-12-06 01:50:40
Basically, I use a meta-class framework called Joose for Javascript that allows me to make use of a more elegant class syntax - but I don't know how I might go about referencing the scope of the object from within deeper methods of the class declaration. I also use require.js for dependemcy management... Here's an example class definition: define([ 'jquery', 'handlebars', ], function($, Handlebars){ var MyClass = Class("MyClass", { //inheritance isa: SuperClass, //instance vars has: { hello:{ is: 'r', init: 'Hi There!', }, someVarToBeSetUsingAjax:{ is: 'rw', init: false, }, }, //methods

How to share package private data between two packages in Java?

依然范特西╮ 提交于 2019-12-06 01:38:43
问题 I have 2 Java packages, A & B. Let's say that some classes in package B want to use some classes in package A however, when a developer comes along and develops package C (or, say, application C), he/she will use my package B but I do not want him/her to be able to use the classes in A that B is using. That is to say, I want my classes in package A to be package private so that they are hidden from the application developer. However, I do want my own package B to be able to access those