listing 1 // A short package demonstration. package bookpack; class Book { private String title; private String author; private int pubDate; Book(String t, String a, int d) { title = t; author = a; pubDate = d; } void show() { System.out.println(title); System.out.println(author); System.out.println(pubDate); System.out.println(); } } class BookDemo { public static void main(String args[]) { Book books[] = new Book[5]; books[0] = new Book("Java: A Beginner's Guide", "Schildt", 2014); books[1] = new Book("Java: The Complete Reference", "Schildt", 2014); books[2] = new Book("The Art of Java", "Schildt and Holmes", 2003); books[3] = new Book("Red Storm Rising", "Clancy", 1986); books[4] = new Book("On the Road", "Kerouac", 1955); for(int i=0; i < books.length; i++) books[i].show(); } } listing 2 // Book recoded for public access. package bookpack; public class Book { private String title; private String author; private int pubDate; // Now public. public Book(String t, String a, int d) { title = t; author = a; pubDate = d; } // Now public. public void show() { System.out.println(title); System.out.println(author); System.out.println(pubDate); System.out.println(); } } listing 3 // This class is in package bookpackext. package bookpackext; // Use the Book Class from bookpack. class UseBook { public static void main(String args[]) { bookpack.Book books[] = new bookpack.Book[5]; books[0] = new bookpack.Book("Java: A Beginner's Guide", "Schildt", 2014); books[1] = new bookpack.Book("Java: The Complete Reference", "Schildt", 2014); books[2] = new bookpack.Book("The Art of Java", "Schildt and Holmes", 2003); books[3] = new bookpack.Book("Red Storm Rising", "Clancy", 1986); books[4] = new bookpack.Book("On the Road", "Kerouac", 1955); for(int i=0; i < books.length; i++) books[i].show(); } } listing 4 // Make the instance variables in Book protected. package bookpack; public class Book { // these are now protected protected String title; protected String author; protected int pubDate; public Book(String t, String a, int d) { title = t; author = a; pubDate = d; } public void show() { System.out.println(title); System.out.println(author); System.out.println(pubDate); System.out.println(); } } listing 5 // Demonstrate Protected. package bookpackext; class ExtBook extends bookpack.Book { private String publisher; public ExtBook(String t, String a, int d, String p) { super(t, a, d); publisher = p; } public void show() { super.show(); System.out.println(publisher); System.out.println(); } public String getPublisher() { return publisher; } public void setPublisher(String p) { publisher = p; } /* These are OK because subclass can access a protected member. */ public String getTitle() { return title; } public void setTitle(String t) { title = t; } public String getAuthor() { return author; } public void setAuthor(String a) { author = a; } public int getPubDate() { return pubDate; } public void setPubDate(int d) { pubDate = d; } } class ProtectDemo { public static void main(String args[]) { ExtBook books[] = new ExtBook[5]; books[0] = new ExtBook("Java: A Beginner's Guide", "Schildt", 2014, "McGraw-Hill"); books[1] = new ExtBook("Java: The Complete Reference", "Schildt", 2014, "McGraw-Hill"); books[2] = new ExtBook("The Art of Java", "Schildt and Holmes", 2003, "McGraw-Hill"); books[3] = new ExtBook("Red Storm Rising", "Clancy", 1986, "Putnam"); books[4] = new ExtBook("On the Road", "Kerouac", 1955, "Viking"); for(int i=0; i < books.length; i++) books[i].show(); // Find books by author System.out.println("Showing all books by Schildt."); for(int i=0; i < books.length; i++) if(books[i].getAuthor() == "Schildt") System.out.println(books[i].getTitle()); // books[0].title = "test title"; // Error -- not accessible } } listing 6 // Demonstrate import. package bookpackext; import bookpack.*; // Use the Book Class from bookpack. class UseBook { public static void main(String args[]) { Book books[] = new Book[5]; books[0] = new Book("Java: A Beginner's Guide", "Schildt", 2014); books[1] = new Book("Java: The Complete Reference", "Schildt", 2014); books[2] = new Book("The Art of Java", "Schildt and Holmes", 2003); books[3] = new Book("Red Storm Rising", "Clancy", 1986); books[4] = new Book("On the Road", "Kerouac", 1955); for(int i=0; i < books.length; i++) books[i].show(); } }