6.9 Nested classes (Java inner classes)


C# allows you to write inner classes too. However, the rules are much simpler than in Java [16] when it comes to this. Java allows you to write four types of inner classes:

[16] Java has complex access privileges and scope for inner classes indeed.

  • static inner classes;

  • member inner classes (non-static inner classes “ also known as nested classes );

  • local inner classes (a class within a method);

  • anonymous inner classes [17] (a class within a method without a name ).

    [17] This type of Java inner class is very commonly used in AWT and Swing.

The first two are inner classes declared at class scope, while the others are inner classes declared at method scope. C# only supports the second type of inner classes (nested classes).

This fragment shows a simple nested class in C#:

 1: class MyClass{ 2:   class MyNestedClass{ 3:   } 4: } 

The fully qualified class name for the nested class is MyClass.MyInnerClass .

Like Java

  • A nested class has access to all members (external to its enclosing class) that are accessible to its enclosing class.

  • A nested class has access to all members of the enclosing class including those declared as private and protected.

Unlike Java

  • Only non-static nested classes are supported. Other forms of inner classes are not available in C#. You cannot have a static nested class, nor can you have a class declared within a method.

  • In addition to nested classes, you can have nested structs (see Chapter 26). A nested struct is a struct type declared within a class. The rules for nested structs is very much the same as for nested classes. The following shows an example of a nested struct:

     1: class MyClass{ 2:  struct  MyNestedStruct{ 3:  } 4:} 
  • A nested class (or struct) can have any of three [18] forms of declared accessibility:

    [18] A non-nested class can have any of five forms of declared accessibility: public , protected internal , protected , internal , or private .

     - public - internal - private 

    As with all other class members, if no access modifier is specified, the default accessibility of the nested class is private .

  • For Java (non-static) member inner classes, you can access the enclosing class's members directly. For C# nested classes, where a nested class needs access to the instance members of the enclosing class, access is provided by passing a reference of itself ( this ) as a constructor parameter when creating a new instance of the nested class. This example explains this.

     1: using System;  2:  3: class HumanBody{  4:   private int noOfArms = 2;  5:   private Arm a;  6:  7:   private void CreateArm(){  8:  a  =  new Arm(this);  9:   } 10: 11:   public static void Main(){ 12:     HumanBody humanBody = new HumanBody(); 13:     humanBody.CreateArm(); 14:     humanBody.a.ShowNoOfArms(); 15:   } 16: 17:   // nested class 18:   class Arm{ 19: 20:     HumanBody refToBody; 21: 22:     // constructor 23:     public Arm(HumanBody r){ 24:       refToBody = r; 25:     } 26: 27:     public void ShowNoOfArms(){ 28:       Console.WriteLine(refToBody.noOfArms); 29:     } 30:   } 31:  } 

    Output:

     c:\expt>test 2 

    The constructor of the nested class (lines 23 “ 25) takes in a reference to the enclosing class instance, so that it can invoke or access the members of the enclosing class (on line 28). Line 14 invokes a method of an instance of the nested class, which makes use of this reference to access the enclosing instance's private field. [19]

    [19] Of course you won't do this in a real application since this field of the enclosing class is easily accessible from the enclosing class instance itself. The aim of this example is to demonstrate how instances of a nested class should obtain a reference to the instance of its enclosing class.



From Java to C#. A Developers Guide
From Java to C#: A Developers Guide
ISBN: 0321136225
EAN: 2147483647
Year: 2003
Pages: 221
Authors: Heng Ngee Mok

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net