OOPS with Swift
OOPS applied to Swift with examples and references.
Basics
- Polymorphism
— The word ‘polymorphism’ literally means ‘a state of having many shapes’ or ‘the capacity to take on different forms’.
— Type casting is an example of polymorphism
— Ref: https://www.hackingwithswift.com/read/0/20/polymorphism-and-typecasting - Inheritance
— A class can inherit methods, properties, and other characteristics from another class.
— When one class inherits from another, the inheriting class is known as a subclass, and the class it inherits from is known as its superclass. Inheritance is a fundamental behavior that differentiates classes from other types in Swift.
— Ref: https://docs.swift.org/swift-book/LanguageGuide/Inheritance.html - Abstraction
— Swift programming language doesn’t support abstract classes. Fortunately, there are workarounds by using;
- Protocols
- Abstract classes
Ref: https://cocoacasts.com/how-to-create-an-abstract-class-in-swift
Advanced
- Dynamic Dispatch
- Method Dispatch is the mechanism that helps to decide which operation should be executed, or more specifically, which method implementation should be used.
- Static dispatch is supported by both reference & value types, whereas dynamic dispatch is supported only by classes
- Ref: https://cocoacasts.com/how-to-create-an-abstract-class-in-swift