Nested Classes: Basics

The goal for this exercise is to familiarize yourself with nested classes.

            When one thinks about the Linked List data structure that we've created previously, one realizes that

1.    we created two classes (the LinkedList class, and the LinkedListNode class) that are clearly connected.  As a matter of fact, the 'Node class is so closely connected to the LinkedList class that it makes sense that no one (other than the LinkedList class) should be allowed access to the 'Node class. 

2.    Furthermore, given how closely coupled those two classes are, it makes sense to let the LinkedList class have direct access the data members of the 'Node class. 

We can accomplish both of above goals by making a private nested class for the LinkedListNode class, as the code example in Figure 1, below, demonstrates. 

Key points about nested classes in C#:

·    Notice that the nested class IntListNode (highlighted in yellow in Figure 1) is located within the MyIntList class –IntListNode is nested within MyIntList.

·    Notice that the class IntListNode is declared to be private – this has the same meaning as declaring any other member of the MyIntList class to be private -  namely, that you're not allowed to access the IntListNode class (or any of the parts of the IntListNode class) outside of the MyIntList class.

o   Trying playing around with the commented-out line in main – if you uncomment it & recompile, you'll get an error message.  Leave it uncommented, but change the IntListNode class to being public, and you'll see that it compiles ok.

o   Technically, we don’t need to declare the IntListNode class to be private – if we leave off any sort of access specifier (private, public, protected), then it will default to private, just like any other method or instance variable inside a class.  We’ll use the private keyword here just to be extra clear.

·    Because the IntListNode class is private, we don’t have to worry about any other classes gaining access to it's fields.  Thus, it's safe for us to mark all three properties as being public – only the MyIntList class will be able to get to those fields, anyways

o   In a very real sense, we've created a helper class who's sole purpose is to serve as a class that simplifies the MyIntList class.

o   Using a nested class in this way is a 'typical use' of nested classes. 

o   Normal, top-level classes  like a LinkedList or a BinarySearchTree would not be nested.  They’re not helper classes, they’re not (just) part of another class – they really stand on their own, so they’re written to be their own classes.

·    Other than that, nested classes are normal classes – they can have constructors, methods, properties, etc, etc.

o   They can also implement interfaces.  What's really cool is that your private class can implement an interface (such as IEnumerator), and you can provide a public method on your MyIntList class which returns an instance of your private class to the outside world, as long as your code (maybe back in main) uses the publicly available interface to manipulate the class.

class MyIntList
{

    protected class IntListNode
    {

        public int m_data;
        public IntListNode m_next;

        public IntListNode(int data)
        {
            m_data = data;
            // m_next = null; // automatically done by CLR, so I don't have to
        } 

        public IntListNode(int data, IntListNode next)
            : this(data)
        {
            m_next = next;
        }
    }

     // From this point down, it’s just normal MyIntList stuff
     private IntListNode m_first; // first item in the list
                                  // automtically given the value null

Figure 1: Example Code 

What you need to do for this exercise: 

  1. While doing the remainder of the exercises for this lesson, you must use the nested linked list node class, instead of a non-nested, separate, top-level node class.  If you use a non-nested node class, you will lose points.
    1. Yes, the nested IntListNode is spelled out above.
    2. Yes, this should be really easy – basically just copy and paste
    3. You also need to look for opportunities to use nested classes during the rest of the term, and do so.  This applies particularly to Binary Search Trees.
  2. There is nothing else that this exercise requires you to do.  Because #1 places requirements on future exercises, this means that there are no concrete deliverables that you need to produce for this exercise.