Inheritance. The text file. Working class dogs are now added to the text file Working dogs have a trainer and therefore have an extra field Butch#2#false April#1#true Shaka#2#true#Thabiso Morgan#4#false Ratty#9#false Ricky#1#false#Khutso Tut#2#true Seal#1#false#Marge =========================================================== The UI class that will call the methods in the manager class /* * Super class: Dogs. Subclass: Working dogs * Read in from a text file */ package inheritance; public class DogsUI { public static void main(String[] args) { DogManager dm = new DogManager(); String text = dm.displayAll(); System.out.println("Hello Dogs"); System.out.println(text); } } =============================================================== The constructor method will read in the text file and instantiate Dog objects and Working Dog objects. Note however that we only use one array, and that array if of the parent class ie Dog (not Working Dog) /* * Dog Manager with methods to create object arrays */ package inheritance; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; import java.util.logging.Level; import java.util.logging.Logger; public class DogManager { // global variables including arrays // The array is of the parent class ie only one array type private Dogs[] dogArray = new Dogs[20]; private int size = 0; DogManager() { try { Scanner scFile = new Scanner(new File("dogs.txt")); while(scFile.hasNext()) { String line = scFile.nextLine(); Scanner scLine = new Scanner(line).useDelimiter("#"); String name = scLine.next(); String ageSt = scLine.next(); int age = Integer.parseInt(ageSt); String pedigreeSt = scLine.next(); boolean pedigree = Boolean.parseBoolean(pedigreeSt); //Separate working dogs from dogs // Each class has a different constructor method if(scLine.hasNext()) { String trainer = scLine.next(); dogArray[size] = new WorkingDog (name, age, pedigree,trainer); } else { dogArray[size] = new Dogs (name, age, pedigree); } size = size + 1; } // end while System.out.println("File Found"); } catch (FileNotFoundException ex) { System.out.println("Error. File not found."); Logger.getLogger(DogManager.class.getName()).log(Level.SEVERE, null, ex); } } public String displayAll() { String s = ""; for(int i = 0; i < size; i++) { s = s + dogArray[i] + "\n"; } // end for return s; } } ============================================================================== The Dog class - the super class. The dog class constructor must match the working dog constructor ie the first three fields must match each other /* * Super class: Dogs. Subclass: Working dogs */ package inheritance; public class Dogs { String name = null; int age = 0; boolean pedigree = false; Dogs(String n, int a, boolean p) { name = n; age = a; pedigree = p; } public String toString() { return name + "\t" + age + "\t" + pedigree; } } =============================================================================== /* * Extends the dog class */ package inheritance; public class WorkingDog extends Dogs{ // Working dog has an additional field String trainer = "Nil"; // Sub-class constructor uses the super-class constructor, just adding the // extra field WorkingDog(String n, int a, boolean p, String t) { super(n,a,p); trainer = t; } // end constructor public String toString() { // returns Dog toString with the extra field return super.toString() + "\t" + trainer; } } ================================================================================ OUTPUT run: Butch 2 false April 1 true Shaka 2 true Thabiso Morgan 4 false Ratty 9 false Ricky 1 false Khutso Tut 2 true Seal 1 false Marge BUILD SUCCESSFUL (total time: 0 seconds) =================================================================================