Category Archives: Assessment

Chapter 5: Inner Classes – Nested Classes

January 12th, 2017

Classwork:

Inner Classes – Nested Classes
A class can be declared inside another class. Just as a loop written inside another loop is called a nested loop, a class written inside another class is called a nested class. The nested class is considered a member of the enclosing class, just like a variable or method.

  • Just like any other class, a nested class produces a separate bytecode file.
  • The name of the bytecode file is the name of the enclosing class followed by the $ character followed by the name of the nested class.
  • Like any other bytecode file, it has an extension of .class. A class called Nested that is declared inside a class called Enclosing will result in a compiled bytecode file called Enclosing$Nested.class.
  • Because it is a member of the enclosing class, a nested class has access to the enclosing class’s instance variables and methods, even if they are declared with private visibility.
  • The enclosing class can directly access data in the nested class only if the data is declared public.
  • Data should always be kept private to follow the rules of encapsulation. However, the exception applies to nested class so the enclosing class can get to the data.
  • Such a privileged relationship should be reserved for appropriate situations.
  • The static modifier can be applied to a class, but only if the class is nested inside another.
  • Like static methods, a static nested class cannot reference instance variables or methods defined in its enclosing class.
  • //********************************************************************
    //  TestInner.java       Author: Lewis/Loftus
    //
    //  Demonstrates the access capabilities of inner classes.
    //********************************************************************
       public class TestInner
       {
          //-----------------------------------------------------------------
          //  Creates and manipulates an Outer object.
          //-----------------------------------------------------------------
          public static void main (String[] args)
          {
             Outer out = new Outer();
             System.out.println (out);
             System.out.println();
             out.changeMessages();
             System.out.println (out);
          } 
       }
    

    Output
    Copy these files to your project.
    Show and explain the output on edmodo.com

    
    //********************************************************************
    //  Outer.java       Author: Lewis/Loftus
    //
    //  Represents a class that encapsulates an inner class.
    //********************************************************************
    public class Outer
    {
       private int num;
       private Inner in1, in2;
       //-----------------------------------------------------------------
       //  Sets up this object, initializing one int and two objects
       //  created from the inner class.
       //-----------------------------------------------------------------
       public Outer()
       {
         num = 9876;
         in1 = new Inner ("Half of the problem is 90% mental.");
         in2 = new Inner ("Another deadline. Another miracle.");
       }
       //-----------------------------------------------------------------
       //  Changes the messages in the Inner objects (directly).
       //-----------------------------------------------------------------
       public void changeMessages()
       {
          in1.message = "Life is uncertain. Eat dessert first.";
          in2.message = "One seventh of your life is spent on Mondays.";
       }
       //*****************************************************************
       //  Returns this object                
       //*****************************************************************
    
       public String toString()
       {
          return in1 + "\n" + in2;
       }
    
          //*****************************************************************
          //  Represents an inner class.
          //*****************************************************************
          private class Inner
          {
             public String message;
             //--------------------------------------------------------------
             //  Sets up this Inner object with the specified string.
             //--------------------------------------------------------------
             public Inner (String str)
             {
                message = str;
             }
             //--------------------------------------------------------------
             //  Returns this object as a string, including a value from
             //  the outer class.
             //--------------------------------------------------------------
             public String toString()
             {
                num++;
                return message + "\nOuter num = " + num;
             }
           }
       }
        
    
  • Each Inner object contains a public String called message.
  • Because it is public, the changeMessages of the Outer class can reach in and modify the contents.
  • As the authors’ve stressed many times, giving data public access should be avoided in general. However, in this case, since Inner is a private class, no class other than Outer can refer to it. Therefore no class other than Outer can directly access the public data inside it either.
    Using inner classes with public data should be done only in situations in which the outer class is completely dependent on the inner class for its existence.

    If designed properly, inner classes preserve encapsulation while simplifying the implementation of related classes

    Preparing for chapter 5 test on Wednesday and getting ready for Midterm.

    Homework:
    Study for chapter 5 test.

    Chapter 2: Programming: Telephone Numbers with PDT and ADT

    Chapter 2: Objects and Primitive Data Types

    1. Write a java application, TelephoneNumV1_YI.java that creates and prints a random phone number of the form XXX-XXX-XXXX. Include the dashes in the output. Do not let the first three digits contain an 8 or 9 (but don’t be more restrictive than that), and make sure that the second set of three digits is not greater than 742.
      Hint: Think through the easiest way to construct the phone number. Each digit does not have to be determined separately.

    2. Write an Object Oriented Application, TelephoneV2_YI.java with the same functionality as the previous version but using Object Oriented Design.