Last active
July 14, 2018 08:22
-
-
Save hitendradeveloper/faff9ea5bb0664bdaf8ab1959804b665 to your computer and use it in GitHub Desktop.
Inheritance Example 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is just a simple class example for the medium blog - https://medium.com/@hitendra.hckr/protocol-the-power-of-swift-5dfe9bc41a99