Export (0) Print
Expand All

Class Fundamentals (C# vs Java)

The following sections compare C# and Java modifiers.

Access Modifiers

C# modifiers are quite similar to those in Java, with several small differences. Each member of a class, or the class itself, can be declared with an access modifier to define the scope of permitted access. Classes that are not declared inside other classes can only specify the public or internal modifiers. Nested classes, like other class members, can specify any of the following five access modifiers:

  • public

    Visible to all.

  • protected

    Visible only from derived classes.

  • private

    Visible only within the given class.

  • internal

    Visible only within the same assembly.

  • protected internal

    Visible only to the current assembly or types derived from the containing class.

The public, protected, and private Modifiers

A public modifier makes the member available anywhere, both inside and outside of the class. A protected modifier indicates that access is limited to within the containing class or classes derived from it. A private modifier means that access is only possible from within the containing type. In C#, the default access modifier is private, while in Java, access defaults to anywhere from within the containing package.

The internal Modifier

An internal item may only be accessed within the current assembly. An assembly in the .NET Framework equates roughly to Java's JAR file; it represents the building blocks from which other programs can be constructed.

The protected internal Modifier

A protected internal item is visible only to the current assembly or types derived from the containing class.

The sealed Modifier

A class with the sealed modifier on its class declaration is the opposite of an abstract class: it cannot be inherited. You can mark a class as sealed to prevent other classes from overriding its functionality. Naturally, a sealed class cannot be abstract. Also note that a struct is implicitly sealed; therefore, it cannot be inherited. The sealed modifier is equivalent to marking a class with the final keyword in Java.

The readonly Modifier

To define a constant in C#, use the const or readonly modifier in place of Java's final keyword. The distinguishing factor between the two modifiers in C# is that const items are dealt with at compile-time, while the values of readonly fields are specified at run time. This means that assignment to readonly fields may occur in the class constructor as well as in the declaration. For example, the following class declares a readonly variable called IntegerVariable that is initialized in the class constructor:

public class SampleClass
{
    private readonly int intConstant;

    public SampleClass ()  //constructor
    {
        // You are allowed to set the value of the readonly variable 
        // inside the constructor
        intConstant = 5;
    }


    public int IntegerConstant
    {
        set
        {
            // You are not allowed to set the value of the readonly variable 
            // anywhere else but inside the constructor

            // intConstant = value;  // compile-time error
        }
        get
        {
            return intConstant;
        }
    }
}
class TestSampleClass
{
    static void Main()
    {
        SampleClass obj= new SampleClass();

        // You cannot perform this operation on a readonly field.
        obj.IntegerConstant = 100;

        System.Console.WriteLine("intConstant is {0}", obj.IntegerConstant);  // 5
    }
}

If a readonly modifier is applied to a static field, it should be initialized in the static constructor of the class.

See Also

Community Additions

ADD
Show:
© 2014 Microsoft