Skip to content

Instantly share code, notes, and snippets.

@hitendradeveloper
Last active July 14, 2018 08:22
Show Gist options
  • Save hitendradeveloper/faff9ea5bb0664bdaf8ab1959804b665 to your computer and use it in GitHub Desktop.
Save hitendradeveloper/faff9ea5bb0664bdaf8ab1959804b665 to your computer and use it in GitHub Desktop.
Inheritance Example 2
import Foundation
//MARK:- Person, Super class
class Person {
var firstName: String = "Hitendra"
var lastName: String = "Solanki"
var displayString: String {
return "Name of person is \(firstName) \(lastName)."
}
init(){ } //just one constructor to make it simple
}//End: Person
//MARK:- Employee, sub class of Person
class Employee: Person {
var designation: String = "Team Lead - Mobile Appliations"
override var displayString: String {
return super.displayString + "\nHe is a \(designation)"
}
}//End: Employee
//MARK:- Professor, sub class of Person
class Professor: Person {
var subjectTeaching: String = "Swift & Blockchain development"
override var displayString: String {
return super.displayString + "\nHe is teaching \(subjectTeaching)"
}
}//End: Professor
//Global Function that can print information of any person
func displayInformation(of person: Person){
print(person.displayString)
}
//creating instances | objects
let person = Person()
let employee = Employee()
let professor = Professor()
//displaying created instances
displayInformation(of: person)
displayInformation(of: employee)
displayInformation(of: professor)
@hitendradeveloper
Copy link
Author

This is just a simple class example for the medium blog - https://medium.com/@hitendra.hckr/protocol-the-power-of-swift-5dfe9bc41a99

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment