'This is confusing, is there any tips so I can understand this better? (OOP Inheritances)
I am having difficulties understanding what to do here. Please bear with me I'm new to JavaScript and this section is giving me a hard time understanding how I can achieve this.
I have a total of 3 classes:
// This class represents all that is common between Student and Mentor
class Person {
// moved here b/c it was identical
constructor(name, quirkyFact) {
this.name = name;
this.quirkyFact = quirkyFact;
}
// moved here b/c it was identical
bio() {
return `My name is ${this.name} and here's my quirky fact: ${this.quirkyFact}`;
}
}
class Student extends Person {
// stays in Student class since it's specific to students only
enroll(cohort) {
this.cohort = cohort;
}
}
class Mentor extends Person {
// specific to mentors
goOnShift() {
this.onShift = true;
}
// specific to mentors
goOffShift() {
this.onShift = false;
}
}
Now there is a general Person class that contains the shared code. Student and Mentor inherit behaviour and state information from Person using the keyword extends. They also have their own code that reflects behaviour and information only pertaining to themselves.
Student and Mentor are subclasses of the Person class, since they are extensions of that class. Person is the superclass in this relationship.
I need to write out the three classes defined above into a new file. Add additional code that instantiates a Student and uses the enroll() method on it. Do the same for Mentor and its specific methods. Experiment with your code to further explore what is and is not possible here.
Now for the inheritance part:
Change the version of the Person class so that it contains another method. Can this method be called on in each of the two subclasses?
Change the constructor for Person by adding a new field to it (like email). How does this change the subclasses?
Solution 1:[1]
Some comments.
class Person {
constructor(name, quirkyFact) {
this.name = name;
this.quirkyFact = quirkyFact;
// Create new property - returns random boolean
this.isAGigaChad = !!Math.round(Math.random() * 1);
}
get bio() {
return `My name is ${this.name} and here's my quirky fact: ${this.quirkyFact}`;
}
speakProphecy() {
return `"PHP sucks" -${this.name}`;
}
}
class Student extends Person {
// Every single class should have a constructor
constructor(studentId, name, quirkyFact) {
// If a class is inheriting from another, ALWAYS include
// a super call. This will call the constructor of the
// parent class.
super(name, quirkyFact);
this.studentId = studentId;
// Initialize property
this.cohort = null;
}
enroll(cohort) {
this.cohort = cohort;
}
}
class Mentor extends Person {
constructor(name, quirkyFact) {
super(name, quirkyFact);
// By default, have them start off as not
// on shift. This value can never be "undefined"
this.onShift = false;
}
goOnShift() {
this.onShift = true;
}
goOffShift() {
this.onShift = false;
}
}
const johnStudent = new Student(123, 'John', 'Dislike PHP');
console.log(johnStudent.bio);
// Student has access to new speakProphecy method
console.log(johnStudent.speakProphecy());
// Student has isAGigaChad property
console.log(`${johnStudent.name} ${johnStudent.isAGigaChad ? 'is' : 'is not'} a gigachad`);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | mstephen19 |