encapsulation

Can I access private members from outside the class without using friends?

Disclaimer Yes, I am fully aware that what I am asking about is totally stupid and that anyone who would wish to try such a thing in production code should be fired and/or shot. I'm mainly looking to see if can be done. Now that that's out of the way, is there any way to access private class members in C++ from outside the class? For...

How much low-level stuff to expose in an API?

When designing the public API of a generic library, how much of the low-level stuff that's used internally should be exposed? On the one hand, users should not depend too heavily on implementation details, and too many low-level functions/classes might clutter the API. Therefore, the knee-jerk response might be "none". On the other ha...

C++ lifetime management of encapsulated objects

What is the best approach to encapsulate objects and manage their lifetime? Example: I have a class A, that contains an object of type B and is solely responsible for it. Solution 1, clone b object to ensure that only A is able to clean it up. class A { B *b; public: A(B &b) { this->b = b.clone(); } ~A() ...

Access levels of java class members

I realise that this is a very basic question, but it is one which has always bothered me. As I understand things, if you declare a field private in Java then it is not visible outside of that class. If it is protected then it is available to inherited classes and anything in the same package (correct me if either of those definitions is ...

Encapsulating common logic (domain driven design, best practices)

Updated: 09/02/2009 - Revised question, provided better examples, added bounty. Hi, I'm building a PHP application using the data mapper pattern between the database and the entities (domain objects). My question is: What is the best way to encapsulate a commonly performed task? For example, one common task is retrieving one or mor...

How can I enforce the use of factory methods on a strongly typed DataSet?

Without breaking the VS2008 designer, can I set my DataTable.New*Row methods internal while leaving the overall DataSet and its tables and classes public? I'm using the VS2008 designer to create an ADO.NET strongly typed DataSet for my application. Some columns refer to external tables. I'd like to check those references at creation ti...

protected/private - why bother?

DUPE: http://stackoverflow.com/questions/99688/private-vs-public-members-in-practice-how-important-is-encapsulation In the course of writing a program in Java, I have abstracted out some libraries that I can see a possible use for in future projects. Why should I bother with setting restricted access (private/protected) on any of the...

Is a function an example of encapsulation?

By putting functionality into a function, does that alone constitute an example of encapsulation or do you need to use objects to have encapsulation? I'm trying to understand the concept of encapsulation. What I thought was if I go from something like this: n = n + 1 which is executed out in the wild as part of a big body of code and ...

Preventing Virtual Method Implementation in C++

I have the following class hierarchy in C++: class Base { virtual void apply() = 0; }; class Derived : public Base { virtual void apply() { // implementation here that uses derived_specialty } virtual void derived_specialty() = 0; }; class Implementation : public Derived { virtual void derived_specialt...

Make methods/properties visible to one class, hidden to others

I have a class Server which talks to a server connection for IRC. It holds a list of known Users, and creates them as necessary. I have two problems involving the User class: Anyone can create an instance of a User. I only want the Server class to be able to do this. If a user (the thing User describes) changes his/her name (or othe...

In Java, is it ever a bad idea to make an object's members publicly available?

I have a data class in my application. My application will never be used as a public API and I will be the only person developing code within my project. I am trying to save every ounce of processor and memory power I can. Is it a bad idea to make my data members in my data class to have public/protected/default protection so that...

Encapsulation in the age of frameworks

At my old C++ job, we always took great care in encapsulating member variables, and only exposing them as properties when absolutely necessary. We'd have really specific constructors that made sure you fully constructed the object before using it. These days, with ORM frameworks, dependency-injection, serialization, etc., it seems like...

When should a class use its own getters/setters vs accessing the members directly?

When generating setters and getters in Eclipse one of the options is to use the getters and setters within the class rather than accessing the class members directly. Is this level of class internal encapsulation useful or is it taking a good idea one step too far? DUPE: http://stackoverflow.com/questions/476021/should-you-use-access...

Doesn't Spring's dependency injection break information hiding?

Coming from a C++ background I have to master the complexity of the Java world and its frameworks. Looking at the spring framework for DI I am finding it difficult to believe that I have to make each setter function which will be subject for DI public. Doesn't that requirement break the principle of information hiding? Of course I...

A brilliant example of effective encapsulation through information hiding?

"Abstraction and encapsulation are complementary concepts: abstraction focuses on the observable behavior of an object... encapsulation focuses upon the implementation that gives rise to this behavior... encapsulation is most often achieved through information hiding, which is the process of hiding all of the secrets of object that do no...

Acessing the backing field in an auto property

Is there any way to access the backing field for a property in order to do validation, change tracking etc.? Is something like the following possible? If not is there any plans to have it in .NET 4 / C# 4? public string Name { get; set { if (value != <Keyword>) { RaiseEvent(); } <...

WPF - Can I stop UserControl exposing its named child elements?

This is a bit of a noob question but I've just realised that if i create a UserControl and choose to name some of its child elements a la - <UserControl x:Class="UserControls.uControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="300" Width="300"> <Grid...

Encapsulation within class definitions

For example, do you use accessors and mutators within your method definitions or just access the data directly? Sometimes, all the time or when in Rome? ...

Is OO design's strength in semantics or encapsulation?

Object-oriented design (OOD) combines data and its methods. This, as far as I can see, achieves two great things: it provides encapsulation (so I don't care what data there is, only how I get values I want) and semantics (it relates the data together with names, and its methods consistently use the data as originally intended). So where...

How to make a reference type property "readonly"

I have a class Bar with a private field containing the reference type Foo. I would like to expose Foo in a public property, but I do not want the consumers of the property to be able to alter Foo... It should however be alterable internally by Bar, i.e. I can't make the field readonly. So what I would like is: private _Foo; ...