Intro to Inheritance Lab
Objectives
Define classes that inherit from a shared parent, or super, class.
Define methods unique to those classes.
Overview
In this lab, we'll be working with a school domain model. Our application has users that are either teachers or students. Teachers and students will share certain attributes and have certain behaviors that are unique to them. You'll be defining a User class that both students and teachers inherit from and you'll be writing methods within both the Teacher and Student class that are unique to that class.
Instructions
Run the test suite to get started. This is a test-driven lab.
Define the
Userclass such that a user can have a first and last name. You'll need both a setter and a getter for first and last name.We've given you a barebones
Teacherclass inlib/teacher.rb. Change the class definition so that theTeacherclass inherits from theUserclass. Run the test suite and notice that you are passing some tests for theTeacherclass, even without writing any code inside that class. That is because it will inherit the#first_nameand#last_namemethods from theUserclass you told it to inherit from.We've given you a class constant
KNOWLEDGE, that points to an array of knowledge strings. Write a method,#teachthat returns a random element from that array. You might want to look up theArray#samplemethod.We've given you a barebones
Studentclass. Change the class definition so that it inherits from theUserclass. Run the test suite and notice that you are passing some tests for theStudentclass, even without writing any code inside that class. That is because it will inherit the#first_nameand#last_namemethods from theUserclass you told it to inherit from.Individual students should initialize with an instance variable,
@knowledge, that points to an empty array.Define a method,
#learn, that takes in a string and adds that string to the student's@knowledgearray.Define a method,
#knowledge, that returns that student's knowledge array.
Bonus
We've set up an executable file for you in bin/time_for_school. Check out the code there and run the file with ruby bin/time_for_school in your terminal. Get a feel for how the code we wrote works and what methods are available to our student and teacher instances thanks to inheritance.
Play around with it a bit more by trying out the following in this file:
We have a new user, Jim, who is just here to audit a few classes. He is neither a teacher nor a student. Create a local variable,
jim, and point it equal to an instance of theUserclass. Then, givejima first and last name. What happens when you try to call#learnonjim?Continue to operate on our student,
steve. Haveaviteach him some more important knowledge.Create a new student and have that student learn a few things from
avias well.Then,
putsout the existing knowledge of each student.
View Ruby Inheritance Lab on Learn.co and start learning to code for free.
View Intro to Inheritance Lab on Learn.co and start learning to code for free.
Last updated