001 /** 002 * Exercise 5.5 from the book 003 * 004 * @author Kristian Torp, torp (at) cs (dot) aau (dot) dk 005 * @version 1.0 006 */ 007 /** 008 * A "friendly" class with some protected data 009 */ 010 class ProtectedData { 011 protected int i; 012 013 /** Default constructor */ 014 public ProtectedData() { 015 i = 0; 016 } 017 018 /** 019 * Show the instance variables 020 */ 021 public void showData() { 022 System.out.println("Protected: " + i); 023 } 024 } 025 026 /** 027 * The public class 028 */ 029 030 public class Exercise5_5 { 031 private ProtectedData pd; 032 033 /** 034 * Directly mainpulate the protected variables in the friendly class 035 */ 036 public Exercise5_5() { 037 pd = new ProtectedData(); 038 } 039 040 /** 041 * Default constructor that create an instance of the friendly class 042 */ 043 public void alterProtected(int i) { 044 pd.i = i; 045 } 046 047 /** 048 * Show content of friendly class 049 */ 050 public void showData() { 051 pd.showData(); 052 } 053 054 /** 055 * The main method. 056 */ 057 public static void main(String args[]) { 058 Exercise5_5 cl = new Exercise5_5(); 059 cl.showData(); 060 cl.alterProtected(89); 061 cl.showData(); 062 } 063 }