Programming in Swift: Functions & Types

Jan 4 2022 · Swift 5.5, iOS 15, Xcode 13

Part 5: Protocols & Inheritance

38. Inheritance

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

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

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Notes: 38. 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’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

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!")
      }
    }
  }