Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/org/gh/McdWork.java
Original file line number Diff line number Diff line change
@@ -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
10 changes: 8 additions & 2 deletions src/org/gh/RunMe.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you rename "Student" to Stud, you'll need to update this (Same with next line for "Teacher" vs "Teach"

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
Expand Down
35 changes: 35 additions & 0 deletions src/org/gh/Teach.java
Original file line number Diff line number Diff line change
@@ -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();
}
}