views:

686

answers:

4

Hi All,

I understand that the rule of thumb in OOD is to minimize access to all members of a given object as best as can be reasonably accomplished.

C# and Java both seem to implement the same set of access modifiers; however, something which has bewildered me for some time now is why Java classes seem to be mostly declared as public while C# classes seem mostly to be declared as default. Is there some subtlety to these languages which imposes these differences, or is it simply a matter of convention or what?

I find myself frequently going through my C# code (I habitually make most classes public, excepting inner classes, anonymous classes, and other classes of narrow scope and usefulness) in an attempt to please the compiler, however I wonder if I may be missing something important.

Thanks, Brian

PS - Not sure how subjective this may be; would this make a better wiki question? PPS - I've already read Effective Java by Josh Bloch and understand that you should limit access to things as much as possible. That is not my question.

+1  A: 

The only things that I make public are static/final variables, which are generally constants. Everything else is private, and access is done through getXXX() and setXXX() methods, when appropriate. The setXXX() methods also perform any validation against the data. If I have to make something protected, I will, but I usually avoid it.

Thomas Owens
Apparently, the entire question has changed since I originally posted this...
Thomas Owens
yea i up voted since your answer was correct before i changed the whole thing... it actually saved accidentally before i was done typing. thanks tho!
sweeney
+5  A: 

I think you answered your question. As per Joshua Bloch, "The rule of thumb is simple, make each class or member as inaccessible as possible." Effective Java

Gren
A: 

Less "client" (other code) knows about inner-workings of your classes, he will benefit more ... Simple rule of abstraction, and a fundamental pillar of OOP. The right answer is already given above:

"The rule of thumb is simple, make each class or member as inaccessible as possible." ~ Joshua Bloch

Sapphire
+2  A: 

Java's scoping is slightly different than C#'s scoping.

This is talked about briefly in C# From a Java Developer's Perspective's The Same, But Different: Access Modifiers. This document is slightly dated now, but is still mostly relevant.

That list has two mistakes:

  1. C#'s internal is equivalent to Java's default scope (which is its own scope).
  2. C#'s internal protected is equivalent to Java's protected.

Additionally, the above document doesn't mention what the default access modifiers are for classes, only for methods and properties/variables.

For reference, the default scope for classes in c# is internal. Java's is its usual default scope, as described earlier.

R. Bemrose
so from what i gather internal is 1 step down from public in terms of accessibility. java doesnt really have internal so public makes sense for general purpose ood. public does not make sense for most c# development unless you are writing a class library for export. thus default or internal is suitable for most developers. does this sound correct?
sweeney
C#'s internal and internal protected are not equivalent to Java's default scope or Java's protected. C# internal classes are accessible from the same assembly. Assemblies are essentially .exes or .dlls. If Java allowed access modifiers by .jar file this would be similar, but is does not.
FkYkko