Modifiers

C# and Java both use modifiers to specify the accessibility and behavior of program elements. Much commonality exists between Java and C# modifiers, but many differences and inconsistencies exist as well. The differences in usage and effect are often subtle and difficult to understand and remember when coming to C# from a Java background. In this section, we contrast the Java and C# modifiers. We group these modifiers into three categories: access, inheritance, and other.

Access Modifiers

C# includes all of the access modifiers available in Java and provides additional flexibility with the introduction of two new modifiers. However, accessibility based on package membership in Java doesn’t translate to the equivalent accessibility based on namespace membership in C#. In Java, a program element with no access modifier is accessible to all members of the same package. C# has no direct equivalent; the closest match is the internal modifier. Table 4-13 contrasts the Java and C# access modifiers.

Table 4-13. Access Modifiers

Java

C#

Accessibility of Program Element

public

public

No access restrictions are enforced. Any code can access a public element.

protected

protected

Accessible only to members of the containing class and members of derived classes.

  

This is notably different from the meaning of protected in Java, which permits access by all members of the containing package and only those members outside the package that are derived from the containing class.

private

private

Accessible only to members of the containing class or struct.

N/A

internal

Accessible only to program elements contained within the same assembly.

N/A

protected internal

The protected internal combination is the only valid combination of access modifiers.

  

Accessibility is equivalent to that granted by the protected or internal modifier (in other words, it is the union of the two, not the intersection), resulting in the element being accessible to any code within the containing assembly as well as any members of the containing class or classes derived from the containing class.

Inheritance Modifiers

C# provides more inheritance modifiers than Java to accommodate the additional control over member inheritance provided by C#. See the Versioning and Inheritance of Members section in Chapter 5 for more details of inheritance in C#. Table 4-14 contrasts the Java and C# inheritance modifiers.

Table 4-14. Inheritance Modifiers

Java

C#

Effect on Modified Member

abstract

abstract

The abstract modifier is applicable to both classes and members of classes.

Use of the abstract modifier on a class declaration identifies that the class cannot be directly instantiated. Abstract classes have a potentially incomplete implementation and are intended for use as base classes from which other classes will be derived.

Use of the abstract modifier on a class declaration identifies that the class cannot be directly instantiated. Abstract classes have a potentially incomplete implementation and are intended for use as base classes from which other classes will be derived.

  • The abstract and sealed modifiers are mutually exclusive.

  • Structs are not valid targets of the abstract modifier because they do not support inheritance.

  • When applied to members of abstract classes, abstract identifies an incomplete member implementation that must be provided by a derived class.

  • All abstract members are implicitly virtual.

  • The abstract modifier is mutually exclusive with the static, virtual, and override modifiers. A member marked as abstract cannot contain implementation code; an empty statement must follow the declaration.

  • The member in the derived class that provides the implementation of the abstract member must have the override modifier.

N/A

new

Explicitly confirms the intention to hide an inherited member of the same name. This behavior is not available in Java but is the default behavior applied to members in C#. Despite being the default behavior, the compiler will raise warnings if the new keyword is not used. Note that this should not be confused with the word new used in the context of object instantiation.

  • If the new modifier is used on a member that is not hiding an inherited member, the compiler will generate a warning.

  • The new and override modifiers are mutually exclusive.

  • The combination of the new and virtual keywords creates a new point of specialization.

N/A

override

Specifies that the member is overriding an inherited virtual member with the same signature. Only virtual inherited members can be overridden, including those with the virtual, abstract, or override modifier.

  • The overriding member must be declared with the same accessibility modifiers as the member being overridden.

  • The override modifier is mutually exclusive with the new, static, virtual, and abstract modifiers.

  • The member being overridden must be accessible to the overriding member.

  • A compiler warning is raised if a member attempts to override a nonvirtual member. The new modifier should be used to hide the nonvirtual member.

final

sealed

The sealed modifier can be applied to both class and member declarations with slightly different results.

Using the sealed modifier on a class declaration states that the class cannot be used as a base class for a derived class. A compile-time error will occur if this is attempted.

  • The sealed and abstract modifiers are mutually exclusive.

  • Structs are implicitly sealed and do not support inheritance.

  • The sealed modifier can be applied in conjunction with the override modifier on an inherited virtual method to ensure that the method cannot be overridden by derived classes.

  • A base class cannot simply define a method as sealed as with the final keyword in Java.

  • The sealed and abstract modifiers are mutually exclusive.

N/A

virtual

Identifies members as virtual and allows derived classes to override, as opposed to hide, inherited members. This is the default behavior in Java.

  • Derived classes use the override modifier to specialize the implementation of a virtual member.

  • The virtual modifier is mutually exclusive with static, abstract, and override.

Other Modifiers

Table 4-15 contrasts other modifiers available in Java and C# that are unrelated to program element accessibility and inheritance.

Table 4-15. Other Modifiers

Java

C#

Effect on Modified Member

native

extern

Indicates that the implementation of a member is external to the C# code. This usually means that the implementation is contained in a pre-.NET Microsoft Windows dynamic-link library (DLL).

  • If the external implementation is contained in a Windows DLL, members must be static and decorated with the DLLImport attribute. See Attributes in Chapter 6 for details.

  • The extern modifier is mutually exclusive with abstract.

  • The body of a member modified with extern must always be an empty statement.

final

readonly

Identifies a field as being read-only. Read-only fields are more flexible than constants because they can be initialized at declaration or within an instance constructor. Static read-only fields can be initialized at declaration or within a static constructor.

  

The ref and out parameter modifiers can be used only on readonly fields within the context of an instance constructor.

static

static

The modified member exists within the context of the type in which it’s declared, independently of a specific class or struct instance.

  

Significantly, C# static members cannot be referenced through an instance of the type, only through a reference to the containing type.

  

Static function members have no access to the operator this.

strictfp

N/A

C# has no equivalent for the strictfp modifier.

synchronized

N/A

The C# equivalent, lock, is not used as a member modifier. See the Statements section later in this chapter and Chapter 13, for complete details.

transient

N/A

C# does not provide a language equivalent of transient; however, the NonSerialized attribute provides comparable functionality. See Chapter 10 for full details.

volatile

volatile

Forces synchronization and ordering of access to a field without resorting to the explicit use of lock. The field modified with the volatile modifier must be one of the following types:

  • A reference type

  • A pointer type

  • One of byte, sbyte, short, ushort, int, uint, char, float, or bool

  • An enumeration with a base type of byte, sbyte, short, ushort, int, or uint.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset