Learn Swift Fundamentals by preparing for Job Interview, Questions 79 ~ 82 For OOP
2 min readMay 20, 2022
These are the most basic concepts of OOP. I think most of you just know about it. But if you are a complete beginner, watch, study and prepare properly.
79) What is Base / Root class in Swift?
Answer:
- Swift has no base class as in Objective-C.
- Classes can be created without any base class / parent class. In such cases, the current class itself is the base class.
- If a class is derived from any class, then the super class is its base class.
80) What is self and super keywords?
Answer:
- self and super are keywords used in instance methods of a class.
- self refers to the current class object in which the method is present. To refer the current class properties and methods use self.
- super refers to the Parent class object of the current class. To refer its Parent class properties and methods use super.
81) Can we achieve Overriding without inheritance?
Answer:
- No, overriding needs at least two classes and those two must be in inheritance relationship.
82) When do you prefer struct over class?
Answer:
- In Swift, structs and classes give you value and reference-based constructs for your objects.
- struct is preferred for objects designed for data storage like Array.
- struct also helps remove memory issues when passing objects in a multi-threaded environment.
- class, unlike struct, supports inheritance and is used more for containing logic like UIViewController.
- Most standard library data objects in Swift, like String, Array, Dictionary, Int, Float, Boolean, are all structs, therefore value objects.