/** * Main class for testing inheritance */ public class PersonTest { public static void main( String[] args ) { // String p = "Preface"; Person person1 = new Person(); Student student1 = new Student(); Employee employee1 = new Employee(); Person p = new Student(); p.getFullName(); (p (Student)).getStudentID(); // Classes Person, Student, and Employee private data can't be accessed person1.setFullName( "Brad Rippe" ); student1.setStudentID( 99999999 ); employee1.setFullName( "Brad Rippe" ); // inherited method from the Person class // code reuse --- Ahhhhhhh! employee1.setEmployeeID( 77777777 ); /* Overridden methods */ person1.overridingTest( 0, person1 ); employee1.overridingTest( 0, person1 ); /* All class inherit implicitly from the Object class * Thus all classes inherit methods and attributes from the Object class */ System.out.println( "toString() method from the object class " + person1.toString() ); System.out.println( "toString() method overridden in the Student class " + student1.toString() ); System.out.println( "Person " + person1.getFullName() ); System.out.println( "Student " + student1.getStudentID() ); System.out.println( "Employee " + employee1.getEmployeeID() ); /* Test for the "is a" relationship */ if( person1 instanceof Person ) System.out.println( "Person1 is a Person" ); else System.out.println( "Person1 is not a Person" ); if( student1 instanceof Person ) System.out.println( "Student1 is a Person" ); else System.out.println( "Student1 is not a Person" ); if( employee1 instanceof Person ) System.out.println( "Employee1 is a Person" ); else System.out.println( "Employee1 is not a Person" ); /* Check if Person is a Student */ if( person1 instanceof Student ) System.out.println( "Person1 is a Student" ); else System.out.println( "Person1 is not a Student" ); } }