Inheritance and Interfaces
Overview
Kotlin is an example of an ‘object-oriented programming’ language. You’ve used some objects before, but Kotlin forces you to really get into objects. Today, we’ll focus on the concepts core to “OOP”, which you’ll use heavily in Kotlin but apply across lots of other programming languages.
Basic Learning Objectives
Before class, you should be able to:
- Describe at a high level how classes can “inherit” code from another class
- Describe the difference between an interface and a class
Advanced Learning Objectives
After class, you should be able to:
- Create a subclass
- Implement an interface
- Explain how instance variables work with inheritance
Readings
You should read the following:
Checks
You should submit answers to the following on the Gradescope assignment linked through Moodle. You may upload your answers in one file or different files and the name of the files doesn’t matter.
-
Given the base class
Person
, complete the derived classStudent
below.Student
should have the following properties:classYear
: Student’s anticipated graduation yearmajor1
: Student’s first major.major2
: Student’s second major, if applicable.minor
: A list of a student’s minors (because apparently, there isn’t a limit to the number of minors at Carleton…)
Student
should have one member functiondeclareMajor
that:- sets
major1
to the declared major if the student’s first major is “Undeclared” - sets
major2
to the declared major if the student already has a first major but the student’s second major is “Undeclared” - prints “You have too many majors! Talk to your advisor.” if the student already two majors
-
Given the interface
RegistrarRecord
, implement the member functionprintCourses
forStudentRecord
andProfessorRecord
.- Both
StudentRecord
andProfessorRecord
should have the same propertycourses
, which is a list of courses. - However,
printCourses
forStudentRecord
first prints “I’m currently taking these courses:”, followed by the list of courses, whileprintCourses
forProfessorRecord
first prints “I’m currently teaching these courses:” followed by the list of courses instead.
- Both