Programming in Swift: Functions & Types

Jan 4 2022 Swift 5.5, iOS 15, Xcode 13

Part 5: Protocols & Inheritance

38. Inheritance

Lesson Complete

Play Next Lesson
Next
Save for later
About this episode
See versions

See course reviews

See forum comments
Cinema mode Mark as Complete Download course materials
Previous episode: 37. Introduction Next episode: 39. Challenge: Inheritance

Update Notes: This course was originally recorded in 2019. It has been reviewed and all content and materials updated as of October 2021.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

You can unlock the rest of this video course, and our entire catalogue of books and videos, with a kodeco.com Professional subscription.

In the Swift Fundamentals course you learned some distinguishing characteristics between classes and structures: classes are reference types, and structures are value types.

48 class Student: Person {
48 class Student: Person {
  var grades: [Grade] = []
}
52 let jon = Person(firstName: "Jon", lastName: "Snon")
53 let jane = Student(firstName: "Jane", lastName: "Snane")
55 jon.firstName
jane.firstName
let historyGrade = Grade(letter: "B", points: 9, credits: 3)
jane.grades.append(historyGrade)
61 jon.grades
61 class SchoolBandMember: Student {
 }
62 var minimumPracticeTime = 2
class StudentAthlete: Student {
}
class StudentAthlete: Student {
  66 var isEligible: Bool {
    return grades.filter { $0.letter == "F" } .count < 3
  }
}
75 let jessy = SchoolBandMember(firstName: "Jessy", lastName: "Catterwaul")
let marty = StudentAthlete(firstName: "Marty", lastName: "McWolf") 
74 let array = [jon, jane, jessy, marty]
74 let array: [Student] = …
74 … = [jane, jessy, marty]
let array = 
76 let student = marty as Student
77 let athlete = student as! StudentAthlete
77 … student as! SchoolBandMember
79 func getEveningActivity(student: Student) -> String {

}
80 if let bandMember = student as? SchoolBandMember {
    
  } else {
    return "Hitting the books!"
  }
81 return "Practicing for at least \(bandMember.minimumPracticeTime) hours."
getEveningActivity(student: jessy)
getEveningActivity(student: jane)
70 gr
70 override var grades: [Grade]
70  override var grades: [Grade] {
    get {  }
    set {
    
    }
}
get { return super.grades }
set {
73      super.grades = newValue
}
74 if !isEligible {
        print("It's time to study!")
      }
89 let utterFailureGrade = Grade(letter: "F", points: 0, credits: 0)
90 athlete.grades.append(utterFailureGrade)
91 athlete.grades.append(utterFailureGrade)
92 athlete.grades.append(utterFailureGrade)
70 override var grades: [Grade] {
    didSet {
      if !isEligible {
        print("It's time to study!")
      }
    }
  }