diff --git a/src/org/gh/McdWork.java b/src/org/gh/McdWork.java new file mode 100644 index 0000000..8c12714 --- /dev/null +++ b/src/org/gh/McdWork.java @@ -0,0 +1,35 @@ +// Think of package as a directory, we can import packages for reuse +package org.gh; + +// extends Parent means we inherit from Parent +// This gives Student the ability to use Parent methods and attributes +// implements Printable means we must override the methods in that interface +public class McdWork extends Person implements Printable{ + + // attribute to store students grade + // NOTE: it's private, so we provide a getter and setter + private String pay; + + // set grade + public void setPay(String pay){ + this.pay = pay; + } + // get grade + public String getPay(){ + return this.pay; + } + + // constructor called to create a new student object + // See it's usage in RunMe + public McdWork(String name, String grade){ + super(name); + this.setPay(pay); + } + + // Because this class implements Printable, I must override the printMe() method + public String printMe(){ + // For a student, I want the name and grade to print + return "McdWork details from PrintMe() - " + this.getName()+ " - "+this.getPay(); + } +} +//1 diff --git a/src/org/gh/RunMe.java b/src/org/gh/RunMe.java index cb08158..9b97513 100644 --- a/src/org/gh/RunMe.java +++ b/src/org/gh/RunMe.java @@ -12,28 +12,34 @@ public static void main(String args[]){ System.out.println("Creating a new student and Teacher"); Student objStudent = new Student("Student 1", "GradeA"); Teacher objTeacher = new Teacher("Teacher 1", "Class#1, Class#2"); - + McdWork objMcdWork = new McdWork ("Worker 1", "Position, Salary"); // Print details of a student we created above (name - grade) System.out.println("Print with name (from person) and grade (from student)"); System.out.println(objStudent.getName() + " - " + objStudent.getGrade()); - + // Print details of a Teacher we created above (name - classes) System.out.println("Print with name (from person) and Classes (from Teacher)"); System.out.println(objTeacher.getName() + " - " + objTeacher.getClasses()); + System.out.println("Print with name (from person) and Position (from McdWork)"); + System.out.println(objMcdWork.getName() + " - " + objMcdWork.getPay()); + + // This uses the polymorphism to call printMe and see different results System.out.println("Print the Student and teacher using the printMe method from Printable"); System.out.println(objStudent.printMe()); System.out.println(objTeacher.printMe()); + System.out.println(objMcdWork.printMe()); // Use the toString method of Student (not overriden from the Parent definition) System.out.println("Use the toString method in person since it's not overridden in the subclasses"); System.out.println(objStudent); System.out.println(objTeacher); + System.out.println(objMcdWork); // Create a new object and print it diff --git a/src/org/gh/Teach.java b/src/org/gh/Teach.java new file mode 100644 index 0000000..c46eab4 --- /dev/null +++ b/src/org/gh/Teach.java @@ -0,0 +1,35 @@ +// Think of package as a directory, we can import packages for reuse +package org.gh; + +// extends Parent means we inherit from Parent +// This gives Teacher the ability to use Parent methods and attributes +// implements Printable means we must override the methods in that interface +public class Teach extends Person implements Printable{ + + // attribute to store teacher's class + private String classes; + + // Constructor of tacher taking a name and class as input + public Teach(String name, String classes){ + // Call superclass constructor (in this case Person) + super (name); + + // Set the class for this teacher + this.setClasses(classes); + } + + // get Class + public String getClasses(){ + return this.classes; + } + + // Set class + public void setClasses(String classes){ + this.classes = classes; + } + + // Because this class implements Printable, I must override the printMe() method + public String printMe(){ + return "Teacher details from printMe() = " + this.getName()+ " - "+this.getClasses(); + } +}