Answer:
- Access Specifiers are the way to specify the availability scope of Variables and Methods.
- final
- public
- private
- fileprivate
- open
- internal
Answer:
- Internal
Answer:
- private variables are accessible only within the class.
- fileprivate is accessible in the entire file. Mainly useful for extensions.
class A {
private var aPrivateVar: Int = 10
fileprivate var aFilePrivateVar: Int = 20
func aMethod() {
// private can be accessed within the same class
print(aPrivateVar)
}
}
class B: A {
func bMethod() {
// print(aPrivateVar) // Error!
print(aFilePrivateVar) // You can access fileprivate
}
}
Answer:
- Public method or variable can be accessible anywhere but public class defined in another module can’t be extended.
- Open method or variable can be accessible anywhere but open class defined in another module can be extended.
- An
open
class is accessible and subclassable outside of the defining module. - An
open
class member is accessible and overridable outside of the defining module. - A
public
class is accessible but not subclassable outside of the defining module. - A
public
class member is accessible but not overridable outside of the defining module.
Answer:
- A class defined as a final can’t be inherited but extended.
final class A {
}
// Error
class B: A {
}
Section 3, Conditional Statement
Section 10, static type vs dynamic type
Section 14, access specifier
Section 15, higher order function