Access Specifiers in Swift
Access Specifiers in Swift
Concrete type refers to the specific type that can be directly instantiated to create objects.
We can define all properties and methods. Classes and structures are examples of concrete
types.
******
"Concrete type" বলতে সেই নির্দি ষ্ট type কে বোঝায় যেটি সরাসরি অবজেক্ট তৈরি করতে ব্যবহার হতে পারে।
আমরা এর সব গুণাবলি (properties) এবং মেথড (methods) সংজ্ঞায়িত করতে পারি। ক্লাস (class) এবং
স্ট্রাকচার (structure) হল concrete টাইপের উদাহরণ।
func startEngine() {
print("Engine started")
}
}
protocol Vehicle {
func startEngine()
}
এখানে Vehicle একটি protocol, এটা হলো abstract concept। এটিকে দিয়ে সরাসরি object তৈরি করা
❌
যাবে না:
// let v = Vehicle() এটা করা যাবে না
func read() {
print("Reading '\(title)' by \(author)")
}
}
Swift-এ class আর struct—দুটোই concrete type হতে পারে, যদি সব property আর method define
করা থাকে। কিন্তু যদি কেবল protocol বা abstract ধারণা থাকে, তখন সেটা concrete হয় না।
******
ConcreteTypeStructure.swift
struct Person {
var name: String
var age: Int
}
func empIsActive() {
print("Active")
}
}
Abstract Type
Abstract type is a type that defines a set of requirements (properties and methods) but does
not provide the actual implementation. Abstract types cannot directly instantiated
AbstractType.swift
protocol Shape {
fun carea( ) - > Double
}
Conclusion
Concrete types can be directly instantiated and used. Abstract type defines but does not
provide concrete implementation. Defines common interface with multiple concrete types
enabling OOP Paradigm Polymorphism.
Access Levels: Access Specifiers used to control the visibility and accessibility of classes,
structures, enums, properties and methods. Five different Access levels are
1. Open
2. Public
3. Internal
4. File-Private
5 . Private
Open
Most Permissive Access Level. Can be accessed from any source file from any module or
target or framework. Can be subclassed or overridden by external modules.
import UIKit
// Module
class SomeClass {
var tableView: UITableView = UITableView()
}
//Example
open class MyClass {
open var property: Int = 0
open func method () {}
}
//Only classes and overridable class members can be declared 'open'; use 'public'
//Replace 'open' with 'public'
open struct SomeStruct {
Public
Similar to Open Access level but with some limitations. Can be accessed from any source
file from any module or target or framework. Cannot be subclassed or overridden by external
modules.
Internal
Default Access level in Swift. Can be accessed from any source file from the same module
or target or framework but can not be done from external modules. Useful for defining
internal implementation details.
File-Private
Can be accessed from within the some source file. Useful for hiding implementation details
in a single source file.
Private
Most Restricted Access Level. Can be accessed from within the declaration or extensions of
the same source file. Used for Encapsulating the implementation details within a specific
scope.
extension Person {
fileprivate func modifyName() {
print("Fileprivate name is \(name)") // Accessible
print("private age is \(nickName)") // Accessible
}
}
//MainFile.swift
class Person {
fileprivate var name: String
private var nickName: String
func introduceMe() {
print("Hello, My name is \(name)")
}
}
//PrivateFile.swift
extension Person {
fileprivate func modifyName() {
print("Fileprivate name is \(name)") // name is not accessible because of
file-private
print("private age is \(nickName)") // 'nickName' is inaccessible due to 'private'
protection level
}
}
Open vs Public
Open - Applies to class and class members not structures. Can access, subclass and
override the class or class members outside the defined module. Public applies to class,
structures and enums and their members . Can access but not subclassing or overriding
outside the defined module .