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.
//******************************************************************** // 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; } } }
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.