Junior — Middle
Is the object-oriented programming principle supported in Swift?
sobes.tech AI
Answer from AI
Yes, Swift supports the principles of object-oriented programming (OOP). In Swift, you can create classes that encapsulate data and methods, and inheritance, polymorphism, and encapsulation are supported. For example, you can define a class with properties and methods:
class Animal {
var name: String
init(name: String) {
self.name = name
}
func speak() {
print("Animal sound")
}
}
class Dog: Animal {
override func speak() {
print("Woof!")
}
}
let dog = Dog(name: "Buddy")
dog.speak() // Outputs: Woof!