/*********************************************** * CS2510 Fall 2011 * Lecture #7.1 * Interfaces, Classes, and Abstractions ***********************************************/ import tester.*; /** Components of Design Recipe * * 1. Purpose statements for each method * 2. Template with Fields and Methods for each Class * 3. Examples * 4. Tests * 5. Code */ /** * We start with the following class hierarchy * * +-------------+ * | IPerson | * +------+------+ * / \ * +-+-+ * +--------|-----------+-------------------+ * | | | * +---------+-------+ +--------+-------+ +-------+---------+ * | Faculty | | Staff | | Student | * +-----------------+ +----------------+ +-----------------+ * | String name | | String name | | String name | * | int id | | int id | | int id | * | boolean tenure | | double salary | | int credits | * +-----------------+ +----------------+ +-----------------+ * * But when we write the definitions, and possibly some methods, we * notice that their is lots and lots of repeat code. * * So we Abstract! */ /** * We end up with an "abstract" class that has the "name" and "id" * fields, and our "concrete" classes "extend" the abstract class. * Note that we use the same arrow for both "implements" and "extends". * * We refer to Faculty as a "subclass" of APerson, and APerson as the * "superclass" of Faculty (also Staff and Student) * * +------------------+ * | IPerson | * +------------------+ * | String nameTag() | * +------+-----------+ * / \ * +-+-+ * | * +------+-------+ * | APerson | * +--------------+ * | String name | * | int id | * +------+-------+ * / \ * +-+-+ * +--------|-----------+------------------+ * | | | * +---------+-------+ +--------+-------+ +------+-------+ * | Faculty | | Staff | | Student | * +-----------------+ +----------------+ +--------------+ * | boolean tenure | | double salary | | int credits | * +-----------------+ +----------------+ +--------------+ * */ // Represents a Person at Northeastern interface IPerson{ String nameTag(); } // Represents a Person with a name/id at Northeastern abstract class APerson implements IPerson{ String name; int id; APerson(String name, int id) { this.name = name; this.id = id; } public String nameTag() { return name + ": " + id; } } // Represents Northeastern Faculty class Faculty extends APerson{ boolean tenure; Faculty(String name, int id, boolean tenure) { super(name, id); this.tenure = tenure; } // Creates a specialized name tag for Faculty // with PhD appended public String nameTag() { return super.nameTag() + " PhD"; } } // Represents Northeastern Staff class Staff extends APerson{ double salary; Staff(String name, int id, double salary) { super(name, id); this.salary = salary; } } // Represents Northeastern Students class Student extends APerson{ int credits; Student(String name, int id, int credits) { super(name, id); this.credits = credits; } } // Examples/Tests for Person class hierarchy class PersonExamples{ IPerson spenc = new Student("Spencer", 1234, 120); IPerson clerk = new Staff("Clerk Clark", 7896, 31000); IPerson bryan = new Faculty("Bryan", 4321, false); boolean testNameTag(Tester t) { return (t.checkExpect(spenc.nameTag(),"Spencer: 1234") && t.checkExpect(clerk.nameTag(), "Clerk Clark: 7896") && t.checkExpect(bryan.nameTag(),"Bryan: 4321 PhD")); } }